1.for 循环,格式:
for 控制变量:=初值 to 终值 then
begin
end;
应用,
计算 0+1+2+3+4.....+100的值,函数如下:
或者如下:
function totalAdd(int1,int2:integer):integer;
var
i : Integer;
begin
Result := 0;
if (int1>int2) then
begin
for i:=int2 to int1 do
begin
Result := Result + i;
end;
end
else
begin
for i:=int1 to int2 do
begin
Result := Result + i;
end;
end;
end;
var
i : Integer;
begin
Result := 0;
if (int1>int2) then
begin
for i:=int2 to int1 do
begin
Result := Result + i;
end;
end
else
begin
for i:=int1 to int2 do
begin
Result := Result + i;
end;
end;
end;
统计字符窜 '23:88:28:0:8:35:85:33:34,55,78:64,98:100' 在0到10的数有几个,10到20的有多少个;
函数提供2个:
function tongjiA(temp,sec:string;Int1,int2:integer):Integer;
var
s,STemp : array of string;
ITemp : array of Integer;
I,j,k,t : Integer;
begin
j := 1;
k := 0;
Result := 0;
SetLength(s,Length(temp)+1); //设置数组的长度,都是从1开始的 ,为什么要加1
SetLength(stemp,Length(temp));
SetLength(itemp,Length(temp));
for I := 1 to Length(temp) do
begin
s[I] := Copy(temp,I,1);
if s[I]<>sec then
begin
stemp[j] := stemp[j]+s[I];
ITemp[j] := StrToInt(stemp[j]);
end
else
begin
inc(j);
end;
end;
for t := 1 to j do
begin
if ((ITemp[t]>=int1) and (ITemp[t]<=int2)) then
inc(k);
end;
Result := k;
end;
var
s,STemp : array of string;
ITemp : array of Integer;
I,j,k,t : Integer;
begin
j := 1;
k := 0;
Result := 0;
SetLength(s,Length(temp)+1); //设置数组的长度,都是从1开始的 ,为什么要加1
SetLength(stemp,Length(temp));
SetLength(itemp,Length(temp));
for I := 1 to Length(temp) do
begin
s[I] := Copy(temp,I,1);
if s[I]<>sec then
begin
stemp[j] := stemp[j]+s[I];
ITemp[j] := StrToInt(stemp[j]);
end
else
begin
inc(j);
end;
end;
for t := 1 to j do
begin
if ((ITemp[t]>=int1) and (ITemp[t]<=int2)) then
inc(k);
end;
Result := k;
end;
function tongjiB(temp,sec:string;Int1,int2:integer):Integer;
var
I,j:Integer;
s:string;
begin
Result:=0;
temp:=temp+':'; //在字符串后面加:,不然行如:1:2:3得到的数就不正确,
j:=1;
for i := 1 to length(temp) do
begin
if temp[I]=sec then
begin
s:=Copy(temp,j,i-j);
j:=i+1;
if (StrToInt(s)>=int1) and (StrToInt(s)<=int2) then
Inc(Result);
end;
end;
end;
var
I,j:Integer;
s:string;
begin
Result:=0;
temp:=temp+':'; //在字符串后面加:,不然行如:1:2:3得到的数就不正确,
j:=1;
for i := 1 to length(temp) do
begin
if temp[I]=sec then
begin
s:=Copy(temp,j,i-j);
j:=i+1;
if (StrToInt(s)>=int1) and (StrToInt(s)<=int2) then
Inc(Result);
end;
end;
end;
调用如下:
procedure TForm1.Button1Click(Sender: TObject);
var
i : Integer;
s : string;
begin
s := Edit1.Text;
i := tongjiA(s,':',0,20);
// i := tongjiB(s,':',10,20);
ShowMessage(IntToStr(i));
end;
var
i : Integer;
s : string;
begin
s := Edit1.Text;
i := tongjiA(s,':',0,20);
// i := tongjiB(s,':',10,20);
ShowMessage(IntToStr(i));
end;
浙公网安备 33010602011771号