如何完成这个怪代码? ( 积分: 100 )

  • 主题发起人 主题发起人 huxhang
  • 开始时间 开始时间
H

huxhang

Unregistered / Unconfirmed
GUEST, unregistred user!
"10BET","01",,"1","0"
"10BET","02","3",,"0"
"10BET","03","3",,"0"
"10BET","04",,"1","0"
"10BET","05","3",,"0"
"10BET","06",,"1","0"
"10BET","07","3",,"0"
"10BET","08","3",,"0"
"10BET","09","3",,"0"
"10BET","10","3",,"0"
"10BET","11","3","1",
"10BET","12",,"1","0"
"10BET","13",,"1","0"
"10BET","14","3",,"0"
"24hPoker","01","3",,"0"
"24hPoker","03","3","1",
"24hPoker","04","3","1",
"24hPoker","05","3",,"0"
"24hPoker","06",,"1","0"
"24hPoker","07",,"1","0"
"24hPoker","08","3",,"0"
"24hPoker","09","3","1",
"24hPoker","10","3","1",
"24hPoker","11","3","1",
"24hPoker","12","3",,"0"
"24hPoker","13",,"1","0"
"24hPoker","14",,"1","0"
"5 Dimes","02",,"1","0"
"5 Dimes","03","3","1",
"5 Dimes","04","3",,"0"
"5 Dimes","05","3","1",
"5 Dimes","06","3",,"0"
"5 Dimes","07","3",,"0"
(还有1000多行,一般都每14行第一列的内容相同,第2列从01-14, 第3,4,5列的内容是3或1或0或空)

以上的text文件内容如何变成下面这样?

10,30,30,10,30,10,30,30,30,30,31,10,10,30 //注意这一行是10BET的14行演变的
30,*,31,31,30,10,10,30,31,31,31,30,10,10 //注意这一行是24hPoker的14行演变的
*,10,31,30,31,30,30,*,*,*,*,*,10,* //注意这一行是5 Dimes的6行演变的:其中01,08,09,10,11,12,14行没有就用*代替,100分求源代码
 
规律、系统责任分析:
第一字段连续的一段变成一行
第二字段取值范围是01-14,跳过的编号在行中插入“*”符号
第三、四字段去首尾引号,并连接一起

算法分析:
按第一字段分段,可用跟踪法来配合第二字段的取值确定何时换行

Delphi交流群:33783080
 
你的数据是否有问题,怎么分隔符还这么乱,有的1个逗号,有的2个逗号,位置还不固定
 
select a,
sum(case cast(b as int) when 1 then cast(c + isnull(d,'0') as int) else 0 end) as n1
sum(case cast(b as int) when 2 then cast(c + isnull(d,'0') as int) else 0 end) as n2
sum(case cast(b as int) when 3 then cast(c + isnull(d,'0') as int) else 0 end) as n3
...
sum(case cast(b as int) when 14 then cast(c + isnull(d,'0') as int) else 0 end) as n14
from t

GROUP by a
 
procedure FormatStrs(Source, Dest: TStrings);

procedure InitiStrs(Strs: TStringList);
var
I: Integer;
begin
for I := 0 to Strs.Count - 1 do
Strs := '*';
end;

function StrsToStr(Strs: TStringList): string;
begin
Result := Format('%s%s%s', [Strs[2], Strs[3], Strs[4]]);
if Result = '' then Result := '*';
end;

var
SStrs, TStrs: TStringList;
S: string;
I: Integer;
begin
S := '';
TStrs := TStringList.Create;
try
for I := 0 to 13 do
TStrs.Add('*');

SStrs := TStringList.Create;
try
for I := 0 to Source.Count - 1 do
begin
SStrs.CommaText := Source;
if SStrs[0] <> S then
begin
Dest.Add(TStrs.CommaText);
S := SStrs[0];
InitiStrs(TStrs);
end;
//TStrs[StrToInt(SStrs[1]) - 1] := Format('%s%s%s', [SStrs[2], SStrs[3], SStrs[4]]);
TStrs[StrToInt(SStrs[1]) - 1] := StrsToStr(SStrs);
end;
Dest.Add(TStrs.CommaText);
finally
SStrs.Free;
end;
finally
TStrs.Free;
end;
if Dest.Count > 0 then
Dest.Delete(0);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
//Memo1.Lines.LoadFromFile(...);
//Memo2.Lines.Clear;
FormatStrs(Memo1.Lines, Memo2.Lines);
//Memo2.Lines.SaveToFile(...);
end;
 
发现了高手呀,ANiDelphi,你的代码太完美了
可以交个朋友吗
 
ANiDelphi,你的代码真不错
但测试后,有多处出现下面的问题:
,30,30,30,30,30,,,10,,30,10,1,


*,30,30,30,30,30,*,*,10,*,30,10,1,* {我期待的:每行共14个结果,前后用“,”隔开,空的结果用“*”代替,尤其在行的首和尾不能出现空格,行的首和尾不能出现“,”}
 
ANiDelphi,你的代码真不错
但测试后,有多处出现下面的问题:
,30,30,30,30,30,,,10,,30,10,1,


*,30,30,30,30,30,*,*,10,*,30,10,1,* {我期待的:每行共14个结果,前后用“,”隔开,空的结果用“*”代替,尤其在行的首和尾不能出现空格,行的首和尾不能出现“,”}
 
一定是你的数据不规范,你帮我找下这个ANiDelphi的联系方式,我可以给你改代码
还有,自己的贴贴,都不知道订阅回复通知,真笨哦
 
我找不到她的联系方式,你的联系方式呢?
我的qq:305698256
 
请用 edocu@163.com 这个信箱
把你的那一千多行的东东放在记事本以附件发过来吧
 
是啊,出现这样的情况,可能是文件中有这样的行
"10BET","01",,"","" //即后面三个均为空,这新Format后TStrs[StrToInt(SStrs[1]) - 1]就为空了
解决也很简单,为节约空间,已在原帖修改
*修改后的代码未测试,可能有打字错误:)
 
帮顶,接分
 
anidelphi高手!!!可以加我吗?my qq:305698256
 
procedure FormatStrs(AListSource, AListTarget: TStrings);
const
Cons_Space = '*';
var
SListSrcWord, SListTargetWord: TStringList;
Sect: string;
i, j: Integer;
KeyWord: string;
begin
Sect := '';
SListTargetWord := TStringList.Create;
SListSrcWord := TStringList.Create;
AListTarget.BeginUpdate;
AListTarget.Clear;
try
for i := 0 to 13 do
begin
SListTargetWord.Add(Cons_Space);
end;
for i := 0 to AListSource.Count - 1 do
begin
SListSrcWord.CommaText := AListSource;
if SListSrcWord[0] <> Sect then
begin
AListTarget.Add(SListTargetWord.CommaText);
Sect := SListSrcWord[0];
for j := 0 to SListTargetWord.Count - 1 do
begin
SListTargetWord[j] := Cons_Space;
end;
end;
KeyWord := Format('%s%s%s', [SListSrcWord[2], SListSrcWord[3], SListSrcWord[4]]);
if (KeyWord = '') then
begin
KeyWord := Cons_Space;
end;
SListTargetWord[StrToInt(SListSrcWord[1]) - 1] := KeyWord;
end;
AListTarget.Add(SListTargetWord.CommaText);
finally
SListTargetWord.Free;
SListSrcWord.Free;
end;
if AListTarget.Count > 0 then
begin
AListTarget.Delete(0);
end;
AListTarget.EndUpdate;
end;
 
多人接受答案了。
 

Similar threads

后退
顶部