合肥绿地中央广场:c++算圆周率1万位的问题

来源:百度文库 编辑:高校问答 时间:2024/04/19 03:17:43
详情请见http://www.sallcn.com/html/jiaoyu/0591209434575668.htm

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< 200000000))
{
//z*=a;
d = 0;
for(int i=ARRSIZE-1; i>0; i--)
{
c = z[i]*a + d;
z[i] = c % 10;
d = c / 10;
}
//z/=b;
d = 0;
for(int i=0; i< ARRSIZE; i++)
{
c = z[i]+d*10;
z[i] = c / b;
d = c % b;
}
//x+=z;
Run = 0;
for(int i=ARRSIZE-1; i>0; i--)
{
c = x[i] + z[i];
x[i] = c%10;
x[i-1] += c/10;
Run |= z[i];
}
a++;
b+=2;
}
Memo1->Text = AnsiString().sprintf("计算了 %d 次\r\n",Cnt);
Memo1->Text = Memo1->Text + AnsiString().sprintf("Pi=%d%d.\r\n", x[0],x[1]);
for(int i=0; i< DISPCNT; i++)
{
if(i && ((i%100)==0))
Memo1->Text = Memo1->Text + "\r\n";
Memo1->Text = Memo1->Text + (int)x[i+2];
}
}

按 Button2 执行结果:
这是程序的关键代码,是我看不懂的地方,希望高手讲解一下,尤其是Run |= z[i];中的|=运算符是什么意思?谢谢
while 中间的几个for循环分别都是什么功能呢?

Run = Run|z[i];