假定存储在一个String数组里 var MaxRightCount, MaxErrorCount, OldCount, i: Integer; OldString: string; begin MaxErrorCount := 0; MaxRightCount := 0; OldCount := 0; OldString := ''; for i := Low(StrArray) to High(StrArray) do begin if StrArray[i] = OldString then Inc(OldCount) else begin if OldString = '正确' then begin if OldCount > MaxRightCount then MaxRightCount := OldCount; end else begin if OldCount > MaxErrorCount then MaxErrorCount := OldCount; end; OldCount := 0; OldString := StrArray[i]; end; end; if OldString = '正确' then begin if OldCount > MaxRightCount then MaxRightCount := OldCount; end else begin if OldCount > MaxErrorCount then MaxErrorCount := OldCount; end; end;
好像有问题? OldString := ''; for i := Low(StrArray) to High(StrArray) do begin if StrArray[i] = OldString then Inc(OldCount) 还有怎么都是判断正确?
procedure TForm1.Button1Click(Sender: TObject); function SchStr(s: string): integer; var i, startS, endS: integer; begin startS := -1; endS := -1; for i:=0 to (Memo1.Lines.Count-1) do begin if (Trim(Memo1.Lines[i])=s) then begin if startS<0 then startS := i; end else begin if (endS<0) and (startS>=0) then endS := i; end; end; Result := endS - startS; end; begin ShowMessage(IntToStr(SchStr('23'))); end;
什么问题? 怎么会都判断正确?不是有else吗?
假设所有字符保存在tmpList:TStringList中 var i,j,max1,max2:integer; LastStr : string; begin LastStr :=''; max1 :=0; max2:=0; j:=1; for i:=0 to tmpList.Count-1 do begin if LastStr = tmpList.Strings[i] then inc(j) else begin if tmpList.Strings[i] = '正确' then max2 := max(max2,j) else max1 := max(max1,j); j :=1; LastStr := tmpList.Strings[i] end; end;
其中max1为正确的最大个数,max2为错误的最大个数
function FindMaxStringCnt(strToken:string;strs:TStrings):integer; var i,iPos,maxCnt,cnt:integer; LastStr:string; begin maxCnt:=0; cnt:=1; LastStr:=strToken; iPos:=strs.IndexOf(strToken); for i:=iPos+1 to strs.Count-1 do begin if (LastStr=strs.Strings[i]) then inc(cnt) else cnt:=1; maxCnt:=max(cnt,maxCnt); LastStr:=strs.Strings[i]; end; Result:=maxCnt; end;procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption:=IntToStr(FindMaxStringCnt('错误',Memo1.Lines)); Label2.Caption:=IntToStr(FindMaxStringCnt('正确',Memo1.Lines)); end;
程序运行后,TCount存放的是正确的个数,FCount存放的是错误的个数 procedure GetCount(ss:TStringList;var TCount,Fcount:integer); var i:integer; begin TCount:=0; Fcount:=0; for i:=0 to ss.Count-1 do begin if trim(ss[i])='正确' then TCount:=TCount+1; if trim(ss[i])='错误' then FCount:=FCount+1; end; end;
var
MaxRightCount, MaxErrorCount, OldCount, i: Integer;
OldString: string;
begin
MaxErrorCount := 0;
MaxRightCount := 0;
OldCount := 0;
OldString := '';
for i := Low(StrArray) to High(StrArray) do
begin
if StrArray[i] = OldString then
Inc(OldCount)
else
begin
if OldString = '正确' then
begin
if OldCount > MaxRightCount then
MaxRightCount := OldCount;
end
else
begin
if OldCount > MaxErrorCount then
MaxErrorCount := OldCount;
end;
OldCount := 0;
OldString := StrArray[i];
end;
end;
if OldString = '正确' then
begin
if OldCount > MaxRightCount then
MaxRightCount := OldCount;
end
else
begin
if OldCount > MaxErrorCount then
MaxErrorCount := OldCount;
end;
end;
OldString := '';
for i := Low(StrArray) to High(StrArray) do
begin
if StrArray[i] = OldString then
Inc(OldCount)
还有怎么都是判断正确?
function SchStr(s: string): integer;
var
i, startS, endS: integer;
begin
startS := -1;
endS := -1;
for i:=0 to (Memo1.Lines.Count-1) do
begin
if (Trim(Memo1.Lines[i])=s) then
begin
if startS<0 then
startS := i;
end
else
begin
if (endS<0) and (startS>=0) then
endS := i;
end;
end;
Result := endS - startS;
end;
begin
ShowMessage(IntToStr(SchStr('23')));
end;
怎么会都判断正确?不是有else吗?
var
i,j,max1,max2:integer;
LastStr : string;
begin
LastStr :='';
max1 :=0;
max2:=0;
j:=1;
for i:=0 to tmpList.Count-1 do
begin
if LastStr = tmpList.Strings[i] then
inc(j)
else
begin
if tmpList.Strings[i] = '正确' then
max2 := max(max2,j)
else
max1 := max(max1,j);
j :=1;
LastStr := tmpList.Strings[i]
end;
end;
function FindMaxStringCnt(strToken:string;strs:TStrings):integer;
var
i,iPos,maxCnt,cnt:integer;
LastStr:string;
begin
maxCnt:=0;
cnt:=1;
LastStr:=strToken;
iPos:=strs.IndexOf(strToken);
for i:=iPos+1 to strs.Count-1 do
begin
if (LastStr=strs.Strings[i]) then
inc(cnt)
else
cnt:=1;
maxCnt:=max(cnt,maxCnt);
LastStr:=strs.Strings[i];
end;
Result:=maxCnt;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption:=IntToStr(FindMaxStringCnt('错误',Memo1.Lines));
Label2.Caption:=IntToStr(FindMaxStringCnt('正确',Memo1.Lines));
end;
procedure GetCount(ss:TStringList;var TCount,Fcount:integer);
var
i:integer;
begin
TCount:=0;
Fcount:=0;
for i:=0 to ss.Count-1 do
begin
if trim(ss[i])='正确' then
TCount:=TCount+1;
if trim(ss[i])='错误' then
FCount:=FCount+1;
end;
end;