首页 > 代码库 > [C语言]计算圆周率(Pi-X)

[C语言]计算圆周率(Pi-X)

Code:

 1 #include <stdio.h> 2 int main(){ 3     const ARRSIZE=1010, DISPCNT=1000; //定义数组大小,显示位数 4     //const ARRSIZE=10100, DISPCNT=10000; 5     char x[ARRSIZE], z[ARRSIZE]; //x[0] x[1] . x[2] x[3] x[4] .... x[ARRSIZE-1] 6     int a=1, b=3, c, d, Run=1, Cnt=0,i; 7     for (i=0;i<ARRSIZE;i++){ 8         x[i]=0;z[i]=0; 9     }10     //memset(x,0,ARRSIZE);11     //memset(z,0,ARRSIZE);12     x[1] = 2;13     z[1] = 2;14     while(Run && (++Cnt<200000000)){15     //z*=a;16     d = 0;17     for(i=ARRSIZE-1; i>0; i--)18     {19         c = z[i]*a + d;20         z[i] = c % 10;21         d = c / 10;22     }23     //z/=b;24     d = 0;25     for(i=0; i<ARRSIZE; i++){26         c = z[i]+d*10;27         z[i] = c / b;28         d = c % b;29     }30     //x+=z;31     Run = 0;32     for(i=ARRSIZE-1; i>0; i--){33         c = x[i] + z[i];34         x[i] = c%10;35         x[i-1] += c/10;36         Run |= z[i];37     }38     a++;39     b+=2;40     //printf("计算了 %d 次\n",Cnt);41     }42     printf("计算了 %d 次\r\n",Cnt);43     printf("Pi=%d%d.\r\n", x[0],x[1]);44     for(i=0; i<DISPCNT; i++){45         if(i && ((i%100)==0))46         printf("\r\n");47         printf("%d",(int)x[i+2]);48     }49 }

 

[C语言]计算圆周率(Pi-X)