function Calc(mWidth,mHeight : Integer; mDest: TStrings): Boolean; var R:array[0..20,0..20] of String; I, J :Integer; begin Result := False; if (mHeight <= 0) or (mWidth <= 0) then Exit; if not Assigned(mDest) then Exit; {TODO : 字符棋盘 } mDest.Clear; for I := 0 to mHeight - 1 do begin begin //*************************对每一行进行处理 开始 for J := 0 to mWidth - 1 do begin ///////////对每一行下的列进行处理; 开始; if J = 0 then begin R[I][J] := '├'; end else if J = (mWidth-1) then begin R[I][J] := '┤' ; end else R[I][J] := '┼' ;//else if J<> 0 then R[0][J] := '┬'; if J<>mWidth-1 then R[mHeight-1][J] := '┴' ; end; ///////////对每一行下的列进行处理; 结束 end; //*************************对每一行进行处理 结束 R[0,0] := '┌'; R[mHeight-1,mWidth-1] := '┘'; R[mHeight-1,0] := '└'; R[0,mWidth-1] := '┐'; end; for I := 0 to mHeight -1 do begin for J := 0 to mWidth - 1 do begin if J<>mWidth - 1 then mDest.Text := mDest.Text+R[I][J] else mDest.Text := mDest.Text + R[I][J] + #13#10; end; end; Result := True; end; ==================================================================== 输入 10,10 输出: ┌┬┬┬┬┬┬┬┬┐ ├┼┼┼┼┼┼┼┼┤ ├┼┼┼┼┼┼┼┼┤ ├┼┼┼┼┼┼┼┼┤ ├┼┼┼┼┼┼┼┼┤ ├┼┼┼┼┼┼┼┼┤ ├┼┼┼┼┼┼┼┼┤ ├┼┼┼┼┼┼┼┼┤ ├┼┼┼┼┼┼┼┼┤ └┴┴┴┴┴┴┴┴┘
function Calc(mWidth,mHeight : Integer; mDest: TStrings): Boolean; var R:array[0..20,0..20] of String; I, J :Integer; begin Result := False; if (mHeight <= 0) or (mWidth <= 0) then Exit; if not Assigned(mDest) then Exit; {TODO : 字符棋盘 } mDest.Clear; for I := 0 to mHeight - 1 do begin begin //*************************对每一行进行处理 开始 for J := 0 to mWidth - 1 do begin ///////////对每一行下的列进行处理; 开始; if J = 0 then begin R[I][J] := '├'; end else if J = (mWidth-1) then begin R[I][J] := '┤' ; end else R[I][J] := '┼' ;//else if J<> 0 then begin R[0][J] := '┬'; R[4][J] := '┴'; R[4][mWidth-1] := '┤' ; end; if J<>mWidth-1 then begin R[mHeight-1][J] := '┴' ; R[5][J] := '┬' ; R[5][0] :='├'; end; end; ///////////对每一行下的列进行处理; 结束 end; //*************************对每一行进行处理 结束 R[0,0] := '┌'; R[mHeight-1,mWidth-1] := '┘'; R[mHeight-1,0] := '└'; R[0,mWidth-1] := '┐'; end; for I := 0 to mHeight -1 do begin for J := 0 to mWidth - 1 do begin if J<>mWidth - 1 then mDest.Text := mDest.Text+R[I][J] else mDest.Text := mDest.Text + R[I][J] + #13#10; end; end; Result := True; end; 输入 9,10 输出 ┌┬┬┬┬┬┬┬┐ ├┼┼┼┼┼┼┼┤ ├┼┼┼┼┼┼┼┤ ├┼┼┼┼┼┼┼┤ ├┴┴┴┴┴┴┴┤ ├┬┬┬┬┬┬┬┤ ├┼┼┼┼┼┼┼┤ ├┼┼┼┼┼┼┼┤ ├┼┼┼┼┼┼┼┤ └┴┴┴┴┴┴┴┘
嗯,我也 不知道,但是,如果是一个超大的话,比如说是 10000 X 10000 X 10000 那么执行的是100000000 可能是2分钟左右,还有就是,如果是一个绘图的话,那么大,就不可能用Memo;
To declare multidimensional dynamic arrays ... var A : array of array of string; ... SetLength(A, YourLength); ... SetLength(A[I], YourOtherlength); ...
有一点不太明白: NO.1: for I := 0 to 20 do begin if I > 10 then begin ... // do end; end;NO.2: for I := 11 to 20 do begin ...// do end;它们的速度差的有多少?
procedure TForm1.Button1Click(Sender: TObject); var T: DWORD; I: Integer; begin T := GetTickCount; for I := 0 to 2000000 do begin if I > 10 then begin end; end; ShowMessage(IntToStr(GetTickCount - T)); //9 T := GetTickCount; for I := 11 to 2000000 do begin end; ShowMessage(IntToStr(GetTickCount - T)); //5 end;
function Calc(mWidth,mHeight : Integer; mDest: TStrings): Boolean; var R:array [0..20,0..20] of String; I, J :Integer; begin Result := False; if (mHeight <= 0) or (mWidth <= 0) then Exit; if not Assigned(mDest) then Exit; {TODO : 字符棋盘 } mDest.Clear; for I := 1 to mHeight - 2 do for J := 1 to mWidth - 2 do //*************************对每一行进行处理 开始 R[I][J] := '┼' ; //对每一行下的列进行处理; (没有第一得和最后一行); for I := 1 to mHeight do // 是对第一列和最后一列的处理; begin R[I][0] := '├'; R[I][mWidth-1] := '┤' end; for J := 1 to mWidth-2 do // 是对第一行和最和一行的处理; begin R[0][J] := '┬'; R[mHeight-1][J] := '┴' ; end; R[0,0] := '┌'; R[mHeight-1,mWidth-1] := '┘'; R[mHeight-1,0] := '└'; R[0,mWidth-1] := '┐'; for I := 0 to mHeight -1 do begin for J := 0 to mWidth - 1 do begin if J<>mWidth - 1 then mDest.Text := mDest.Text+R[I][J] else mDest.Text := mDest.Text + R[I][J] + #13#10; end; end; Result := True; form1.Edit4.Text:=floattostr(GetTickCount); end; procedure TForm1.Button2Click(Sender: TObject); begin Calc(StrToIntDef(Edit1.Text, 0), StrToIntDef(Edit2.Text, 0), Memo1.Lines); end; 我问个问题,如何得到一个函数的执行时间? ===============================下边这个不是的======================= function Calc2(mWidth,mHeight : Integer; mDest: TStrings): Boolean; var R:array of array of Integer; R1 :Tstrings; R2 :String; I, J :Integer; begin Result := False; if (mHeight <= 0) or (mWidth <= 0) then Exit; if not Assigned(mDest) then Exit; {TODO : 字符棋盘 } mDest.Clear; R1 := mDest; R1.Clear; //SetLength(R1,wHeight, mWidth); SetLength(R1,3,3);//这句为什么不能运行? for I := 0 to mHeight do R1.Append('www'); R2 := R1[1]; R1[1] := 'a'; for I := 1 to mHeight - 2 do for J := 1 to mWidth - 2 do //*************************对每一行进行处理 开始 R[I][J] := '┼' ; //对每一行下的列进行处理; (没有第一得和最后一行); for I := 1 to mHeight do // 是对第一列和最后一列的处理; begin R[I][0] := '├'; R[I][mWidth-1] := '┤' end; for J := 1 to mWidth-2 do // 是对第一行和最和一行的处理; begin R[0][J] := '┬'; R[mHeight-1][J] := '┴' ; end; R[0,0] := '┌'; R[mHeight-1,mWidth-1] := '┘'; R[mHeight-1,0] := '└'; R[0,mWidth-1] := '┐'; for I := 0 to mHeight -1 do begin for J := 0 to mWidth - 1 do begin if J<>mWidth - 1 then mDest.Text := mDest.Text+R[I][J] else mDest.Text := mDest.Text + R[I][J] + #13#10; end; end; Result := True; form1.Edit5.Text:=floattostr(GetTickCount); //这个不能得到函数的执行时间,好像是程序的执行时间; end;
var
R:array[0..20,0..20] of String;
I, J :Integer;
begin
Result := False;
if (mHeight <= 0) or (mWidth <= 0) then Exit;
if not Assigned(mDest) then Exit;
{TODO : 字符棋盘 }
mDest.Clear;
for I := 0 to mHeight - 1 do
begin
begin //*************************对每一行进行处理 开始
for J := 0 to mWidth - 1 do
begin ///////////对每一行下的列进行处理; 开始;
if J = 0 then
begin
R[I][J] := '├';
end else
if J = (mWidth-1) then
begin
R[I][J] := '┤' ;
end else
R[I][J] := '┼' ;//else
if J<> 0 then
R[0][J] := '┬';
if J<>mWidth-1 then
R[mHeight-1][J] := '┴' ;
end; ///////////对每一行下的列进行处理; 结束
end; //*************************对每一行进行处理 结束
R[0,0] := '┌';
R[mHeight-1,mWidth-1] := '┘';
R[mHeight-1,0] := '└';
R[0,mWidth-1] := '┐';
end;
for I := 0 to mHeight -1 do
begin
for J := 0 to mWidth - 1 do
begin
if J<>mWidth - 1 then
mDest.Text := mDest.Text+R[I][J] else
mDest.Text := mDest.Text + R[I][J] + #13#10;
end;
end;
Result := True;
end;
====================================================================
输入 10,10
输出:
┌┬┬┬┬┬┬┬┬┐
├┼┼┼┼┼┼┼┼┤
├┼┼┼┼┼┼┼┼┤
├┼┼┼┼┼┼┼┼┤
├┼┼┼┼┼┼┼┼┤
├┼┼┼┼┼┼┼┼┤
├┼┼┼┼┼┼┼┼┤
├┼┼┼┼┼┼┼┼┤
├┼┼┼┼┼┼┼┼┤
└┴┴┴┴┴┴┴┴┘
var
R:array[0..20,0..20] of String;
I, J :Integer;
begin
Result := False;
if (mHeight <= 0) or (mWidth <= 0) then Exit;
if not Assigned(mDest) then Exit;
{TODO : 字符棋盘 }
mDest.Clear;
for I := 0 to mHeight - 1 do
begin
begin //*************************对每一行进行处理 开始
for J := 0 to mWidth - 1 do
begin ///////////对每一行下的列进行处理; 开始;
if J = 0 then
begin
R[I][J] := '├';
end else
if J = (mWidth-1) then
begin
R[I][J] := '┤' ;
end else
R[I][J] := '┼' ;//else
if J<> 0 then
begin
R[0][J] := '┬';
R[4][J] := '┴';
R[4][mWidth-1] := '┤' ;
end;
if J<>mWidth-1 then
begin
R[mHeight-1][J] := '┴' ;
R[5][J] := '┬' ;
R[5][0] :='├';
end;
end; ///////////对每一行下的列进行处理; 结束
end; //*************************对每一行进行处理 结束
R[0,0] := '┌';
R[mHeight-1,mWidth-1] := '┘';
R[mHeight-1,0] := '└';
R[0,mWidth-1] := '┐';
end;
for I := 0 to mHeight -1 do
begin
for J := 0 to mWidth - 1 do
begin
if J<>mWidth - 1 then
mDest.Text := mDest.Text+R[I][J] else
mDest.Text := mDest.Text + R[I][J] + #13#10;
end;
end;
Result := True;
end;
输入 9,10
输出
┌┬┬┬┬┬┬┬┐
├┼┼┼┼┼┼┼┤
├┼┼┼┼┼┼┼┤
├┼┼┼┼┼┼┼┤
├┴┴┴┴┴┴┴┤
├┬┬┬┬┬┬┬┤
├┼┼┼┼┼┼┼┤
├┼┼┼┼┼┼┼┤
├┼┼┼┼┼┼┼┤
└┴┴┴┴┴┴┴┘
10000 X 10000 X 10000
那么执行的是100000000 可能是2分钟左右,还有就是,如果是一个绘图的话,那么大,就不可能用Memo;
做什么?找我的错,呵呵,五一快乐
我也要上你的课!可不可以?!
我没有用过动态数组!
也就是说,声明时,不指定数组的上下界;
而在程序中动态的读取传过来的 mWidth 和 mHeight
如何用啊?请各位帮帮我,如果解决了,我另开贴子给分也可以啊
//#但是速度不是很理想,如果高度、宽度大于21怎么办?
//#考虑不要用数组//#用字符串不就可以了!
//#不要画蛇添足
//##更正一条语句<if (mHeight <= 1) or (mWidth <= 1) then Exit;>
//##如果高、宽是1,也没有什么意义{}function Calc(mWidth,mHeight : Integer; mDest: TStrings): Boolean;
//function Calc(mWidth, mHeight: Integer; mDest: TStrings): Boolean;//#修改
{}var
{} R:array[0..20,0..20] of String;
// R: array[0..20, 0..20] of string;//#修改
{} I, J :Integer;
// I, J: Integer;
{}begin
{} Result := False;
{} if (mHeight <= 0) or (mWidth <= 0) then Exit;
{} if not Assigned(mDest) then Exit;
{} {TODO : 字符棋盘 }
{} mDest.Clear;
{} for I := 0 to mHeight - 1 do
{} begin
{} begin //*************************对每一行进行处理 开始
{} for J := 0 to mWidth - 1 do
{} begin ///////////对每一行下的列进行处理; 开始;
{} if J = 0 then
{} begin
{} R[I][J] := '├';
{} end else
{} if J = (mWidth-1) then
// if J = (mWidth - 1) then//#修改
{} begin
{} R[I][J] := '┤' ;
{} end else
{} R[I][J] := '┼' ;//else
{} if J<> 0 then
// if J <> 0 then//#修改
{} R[0][J] := '┬';
{} if J<>mWidth-1 then
// if J <> mWidth - 1 then//#修改
{} R[mHeight-1][J] := '┴' ;
// R[mHeight - 1][J] := '┴' ;//#修改
{} end; ///////////对每一行下的列进行处理; 结束
{} end; //*************************对每一行进行处理 结束
{} R[0,0] := '┌';
// R[0, 0] := '┌';//#修改
{} R[mHeight-1,mWidth-1] := '┘';
// R[mHeight - 1, mWidth - 1] := '┘';//#修改
{} R[mHeight-1,0] := '└';
// R[mHeight - 1, 0] := '└';//#修改
{} R[0,mWidth-1] := '┐';
// R[0,mWidth-1] := '┐';//#修改
{} end;
{} for I := 0 to mHeight -1 do
{} begin
{} for J := 0 to mWidth - 1 do
{} begin
{} if J<>mWidth - 1 then
// if J <> mWidth - 1 then//#修改
{} mDest.Text := mDest.Text+R[I][J] else
// mDest.Text := mDest.Text + R[I][J] else//#修改
{} mDest.Text := mDest.Text + R[I][J] + #13#10;
{} end;
{} end;
{} Result := True;
{}end;
字符串也是一种数组的形式啊;
我有提会的,呵呵(不可以笑的),我昨天用字符数据组,可是它老给我提示
char 和 string 不匹配:)
...
var
A : array of array of string;
...
SetLength(A, YourLength);
...
SetLength(A[I], YourOtherlength);
...
............
├...┼...┤
............
└...┴...┘如果每一个位置都遍历,就要循环 mWidth * mHeight 次
可是中间的字符都重复的......
├...┼...┤
├...┼...┤
├...┼...┤
......所以最终只用循环 mWidth * 3 + (mHeight - 2)次就可以解决
CSDN上要的就是如伴水一们的人
NO.1:
for I := 0 to 20 do
begin
if I > 10 then
begin
... // do
end;
end;NO.2:
for I := 11 to 20 do
begin
...// do
end;它们的速度差的有多少?
var
T: DWORD;
I: Integer;
begin
T := GetTickCount;
for I := 0 to 2000000 do
begin
if I > 10 then
begin
end;
end;
ShowMessage(IntToStr(GetTickCount - T)); //9
T := GetTickCount;
for I := 11 to 2000000 do
begin
end;
ShowMessage(IntToStr(GetTickCount - T)); //5
end;
var
R:array [0..20,0..20] of String;
I, J :Integer;
begin
Result := False;
if (mHeight <= 0) or (mWidth <= 0) then Exit;
if not Assigned(mDest) then Exit;
{TODO : 字符棋盘 }
mDest.Clear;
for I := 1 to mHeight - 2 do
for J := 1 to mWidth - 2 do //*************************对每一行进行处理 开始
R[I][J] := '┼' ; //对每一行下的列进行处理; (没有第一得和最后一行);
for I := 1 to mHeight do // 是对第一列和最后一列的处理;
begin
R[I][0] := '├';
R[I][mWidth-1] := '┤'
end;
for J := 1 to mWidth-2 do // 是对第一行和最和一行的处理;
begin
R[0][J] := '┬';
R[mHeight-1][J] := '┴' ;
end;
R[0,0] := '┌';
R[mHeight-1,mWidth-1] := '┘';
R[mHeight-1,0] := '└';
R[0,mWidth-1] := '┐';
for I := 0 to mHeight -1 do
begin
for J := 0 to mWidth - 1 do
begin
if J<>mWidth - 1 then
mDest.Text := mDest.Text+R[I][J] else
mDest.Text := mDest.Text + R[I][J] + #13#10;
end;
end;
Result := True;
form1.Edit4.Text:=floattostr(GetTickCount);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Calc(StrToIntDef(Edit1.Text, 0), StrToIntDef(Edit2.Text, 0), Memo1.Lines);
end;
我问个问题,如何得到一个函数的执行时间?
===============================下边这个不是的=======================
function Calc2(mWidth,mHeight : Integer; mDest: TStrings): Boolean;
var
R:array of array of Integer;
R1 :Tstrings;
R2 :String;
I, J :Integer;
begin
Result := False;
if (mHeight <= 0) or (mWidth <= 0) then Exit;
if not Assigned(mDest) then Exit;
{TODO : 字符棋盘 }
mDest.Clear;
R1 := mDest;
R1.Clear;
//SetLength(R1,wHeight, mWidth);
SetLength(R1,3,3);//这句为什么不能运行?
for I := 0 to mHeight do
R1.Append('www');
R2 := R1[1];
R1[1] := 'a';
for I := 1 to mHeight - 2 do
for J := 1 to mWidth - 2 do //*************************对每一行进行处理 开始
R[I][J] := '┼' ; //对每一行下的列进行处理; (没有第一得和最后一行);
for I := 1 to mHeight do // 是对第一列和最后一列的处理;
begin
R[I][0] := '├';
R[I][mWidth-1] := '┤'
end;
for J := 1 to mWidth-2 do // 是对第一行和最和一行的处理;
begin
R[0][J] := '┬';
R[mHeight-1][J] := '┴' ;
end;
R[0,0] := '┌';
R[mHeight-1,mWidth-1] := '┘';
R[mHeight-1,0] := '└';
R[0,mWidth-1] := '┐';
for I := 0 to mHeight -1 do
begin
for J := 0 to mWidth - 1 do
begin
if J<>mWidth - 1 then
mDest.Text := mDest.Text+R[I][J] else
mDest.Text := mDest.Text + R[I][J] + #13#10;
end;
end;
Result := True;
form1.Edit5.Text:=floattostr(GetTickCount);
//这个不能得到函数的执行时间,好像是程序的执行时间;
end;
{ 执行的代码 }
ShowMessage(IntToStr(GetTickCount - T)); //GetTickCount - T(单位微妙)
uses StrUtils;
{$ELSE }
function DupeString(mText: string; mCount: Integer): string; { 复制字符 }
var
I: Integer;
begin
Result := '';
for I := 1 to mCount do Result := Result + mText;
end; { DupeString }
{$ENDIF}function Calc(mWidth, mHeight: Integer; mDest: TStrings): Boolean;
const
cGridTopChars: WideString = '┌┬┐';
cGridCenterChars: WideString = '├┼┤';
cGridBottomChars: WideString = '└┴┘';
var
vStrTop: string; //定部字符
vStrCenter: string; //中部字符
vStrBottom: string; //底部字符
I: Integer;
begin
Result := False;
if (mWidth <= 1) or (mHeight <= 1) then Exit;
if not Assigned(mDest) then Exit;
mDest.Clear;
vStrTop := cGridTopChars[1] +
DupeString(Copy(cGridTopChars, 2, 1), mWidth - 2) + cGridTopChars[3];
vStrCenter := cGridCenterChars[1] +
DupeString(Copy(cGridCenterChars, 2, 1), mWidth - 2) + cGridCenterChars[3];
vStrBottom := cGridBottomChars[1] +
DupeString(Copy(cGridBottomChars, 2, 1), mWidth - 2) + cGridBottomChars[3];
mDest.Add(vStrTop);
for I := 1 to mHeight - 2 do mDest.Add(vStrCenter);
mDest.Add(vStrBottom);
Result := True;
end;
是程序执行的时间还是它所在过程执行的时间啊?
为什么几次的执行结果不一样?
所以每一次的 执行速度 都不统一 但有一定的范围
参考答案我已经公布了
先看看我的思路、技巧
练习题命题:天体运动说明:模拟天体的运动要求:代码风格和Delphi Source保持一致输入:
mX0, mY0: Integer; //旋转的中心坐标
mSpeed: Real; //天体的速度
mRadii: Integer; //天体的旋转半径
var nAngle: Real; //当前的旋转角度输出:
var nAngle: Real; //当前的旋转角度
var nX, nY: Integer; //当前天体的坐标调试代码:
*)function Calc(mX0, mY0: Integer; mSpeed: Real; mRadii: Integer;
var nAngle: Real; var nX, nY: Integer): Boolean;
begin
Result := False;
if mSpeed = 0 then Exit;
{ TODO : 天体运动 }
Result := True;
end;procedure TForm1.Timer1Timer(Sender: TObject);
{$J+}
const
cSpeed1 = 0.023;
cSpeed2 = 0.147;
cRadii1 = 100;
cRadii2 = 30;
cAngle1: Real = 0;
cAngle2: Real = 0;
var
vX0, vY0: Integer;
vX1, vY1: Integer;
vX2, vY2: Integer;
begin
vX0 := ClientWidth div 2;
vY0 := ClientHeight div 2;
Calc(vX0, vY0, cSpeed1, cRadii1, cAngle1, vX1, vY1);
Canvas.Pixels[vX1, vY1] := Canvas.Pixels[vX1, vY1] xor $FFFFF;
Calc(vX1, vY1, cSpeed2, cRadii2, cAngle2, vX2, vY2);
Canvas.Pixels[vX2, vY2] := Canvas.Pixels[vX2, vY2] xor $FFFFF;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Interval := 1;
end;
其他的代码是方便你调试的,现在不用管
伴水兄,救救我吧,谢谢了。。呜呜
你好,问你一个问题:
var nAngle: Real; //当前的旋转角度
这个如何理解?
var nAngle: Real; var nX, nY: Integer): Boolean;
//#*******************新增加的常量、变量-开始;
Const
P = 3.1415926; //#圆周率;
Var
L : Real; //#当前天体的预计周长;
JD : Real; //角度
//#*******************新增加的常量、变量-结束;
begin
Result := False;
if mSpeed = 0 then Exit;
{ TODO : 天体运动 }
//#************************增加的代码-开始;
L := 2 * mRadii * P ;
nAngle :=mSpeed + nAngle;// := nAngle + L div ( L div nAngle);
if nAngle > 2*P then
begin
nAngle := nAngle - 2*P;
end;
{$X-}
JD := Round((360 * nAngle )) div Round((2 * P));
nX := mX0 + Round(mRadii * Cos(JD)) ;
nY := mY0 + Round(mRadii * Sin(JD)) ;
{$X+}
Form1.Edit1.Text := FloatToStr(L);
Form1.Edit2.Text := IntToStr(nX);
Form1.Edit3.Text := IntToStr(nY);
Form1.Edit4.Text := FloatToStr(nAngle);
//#************************增加的代码-结束;
Result := True;
end;
调试成功,真的很漂亮啊,真不知道你是如何想出来的:)