终于完成一万位“圆周率:的计算程序(修正 C程序错误)(50分)

  • 主题发起人 凤冠坡
  • 开始时间

凤冠坡

Unregistered / Unconfirmed
GUEST, unregistred user!
原C程序部分:
void __fastcall TForm1::Button2Click(TObject *Sender)
{Const ARRSIZE=1010, DISPCNT=1000;
//定义数组大小,显示位数
char x[ARRSIZE], z[ARRSIZE];
//x[0] x[1] . x[2] x[3] x[4] .... x[ARRSIZE-1]
int a=1, b=3, c, d, Run=1, Cnt=0;
memset(x,0,ARRSIZE);
memset(z,0,ARRSIZE);
x[1] = 2;z[1] = 2;
while(Run && (++Cnt<200000))
d = 0;
for(int i=ARRSIZE-1;
i>0;
i--)
{c = z*a + d;
z = c % 10;
d = c / 10;
}
d = 0;
for (int i = 0;
i < ARRSIZE;
i + +)
{c = z+d*10;
z = c / b;
d = c % b;
}
//x+=z;
Run = 0;
for (int i = ARRSIZE - 1;
i > 0;
i - -)
{
c = x + z;
x = c%10;
x[i-1] += c/10;
Run |= z;
}
a + +;
b + = 2;
}
Memo1 - > Text = AnsiString().sprintf("计算了 %d 次rn", Cnt);
Memo1 - > Text = Memo1 - > Text + AnsiString().sprintf("Pi = %d%d.rn", x[0], x[1]);
for (int i = 0;
i < DISPCNT;
i + +)
{if(i && ((i%100)==0))
Memo1->Text = Memo1->Text + "rn";
Memo1->Text = Memo1->Text + (int)x[i+2];
}}
Delphi 过程部分:
........................
//公式:π = 2 + 2/3 + 2/3*2/5 + 2/3*2/5*3/7 + ...
const
ASize = 1010;
DCount = 1000;
var
A, B, C, D: integer;
I, Run, Cnt: integer;
S: string;
X, Z: array[0..ASize] of integer;
begin
S := '';
Cancel := False;
A := 1;
B := 3;
C := 0;
D := 0;
Run := 1;
Cnt := 0;
for i := 0 to ASize - 1do
begin
X := 0;
Z := 0;
end;
X[1] := 2;
Z[1] := 2;
while (Run > 0) and (Cnt < 2000000)do
begin
D := 1;
for i := ASize - 1do
wnto 1do
begin
c := Z * a + d;
Z := c mod 10;
d := c div 10;
end;
d := 0;
for i := 0 to ASize - 1do
begin
c := Z + d * 10;
Z := c div b;
d := c mod b;
end;
Run := 0;
for i := ASize - 1do
wnto 1do
begin
c := X + Z;
X := c mod 10;
x[i - 1] := x[i - 1] + c div 10;
Run := Run + Z mod 10;
end;
A := A + 1;
B := B + 2;
Cnt := Cnt + 1;
Application.ProcessMessages;
if Cancel then
break;
end;
Memo1.Text := Format('计算了 %d 次', [Cnt]);
Memo1.Text := Memo1.Text + Format('Pi =%d%d.'#13#10, [X[0], x[1]]);
for i := 0 to DCount - 1do
begin
if (i) mod 10 = 0 then
if I > 0 then
S := S + #32;
if (i) mod 50 = 0 then
if I > 0 then
S := S + #13#10;
S := S + Format('%d', [x[i + 2]]);
end;
Memo1.Text := Memo1.Text + S;
end;
 

凤冠坡

Unregistered / Unconfirmed
GUEST, unregistred user!
是千位,但是很容易修改成任意多位的。
C程序错误地方:
while(Run && (++Cnt<200000))
d = 0;
for(int i=ARRSIZE-1;
i>0;
i--)
{c = z*a + d;
z = c % 10;
d = c / 10;
}
d = 0;
--> D = 1 错误之一
for (int i = 0;
i < ARRSIZE;
i + +)
{c = z+d*10;
z = c / b;
d = c % b;
}
//x+=z;
Run = 0;
for (int i = ARRSIZE - 1;
i > 0;
i - -)
{
c = x + z;
x = c%10;
x[i-1] += c/10;
Run |= z;
--> Run +=(Z | 10);错误之二
}
气死人,到处都转贴那个错误,没有人指正。
 

凤冠坡

Unregistered / Unconfirmed
GUEST, unregistred user!
要进行万位计算,只要修改常数部分:
const
ASize = 1010;
--> 10100;
DCount = 1000;
--> 10000;
就可以了。
 
D

dey-999

Unregistered / Unconfirmed
GUEST, unregistred user!
还是要靠自己啊,别人哪会有切肤之痛!
 

凤冠坡

Unregistered / Unconfirmed
GUEST, unregistred user!
确实如此,就好象那个汉字拼音首字母的函数,
也是有错误,竟然转贴了几万次也没有人能够指出。
 

凤冠坡

Unregistered / Unconfirmed
GUEST, unregistred user!
多人接受答案了。
 

Similar threads

I
回复
0
查看
548
import
I
I
回复
0
查看
718
import
I
I
回复
0
查看
821
import
I
I
回复
0
查看
680
import
I
I
回复
0
查看
610
import
I
顶部