用C语言编程求圆周率,要求用命令行实现,计算到小数点后2000位

2024-11-20 13:40:30
推荐回答(5个)
回答(1):

/*
* 编译成可执行文件后,在DOS提示符下执行:
* 如果带一个命令行参数,则该值为要计算的位数;
* 如果还带有第二个举乎告正明参数,则使用stomer公式计算。
* 代码如下:
*/

#include
#include

void main(int argc,char *argv[])
{
long *pi,*t,m,n,r,s;
int t0[][3] = {48,32,20,24,8,4},k0[][3] = {1,1,0,1,1,1},n0[][3] = {18,57,239,8,57,239};
int d,i,j,k,p,q;
d=(argc>1)?(((i = atoi(argv[1]))<0)?0:i):1000;
q=(argc>2)?1:0;
printf("%s\n\n", "Nature (R) Pi value compute Program (C) Tue 2005.11.10");
printf("pi= %s%d * arctg(1/%d) %s %d * arctg(1/%d) %s %d * arctg(1/%d) [%s]\n",
k0[q][0] ? "" : "-", t0[q][0], n0[q][0], k0[q][1] ? "+" : "-"顷汪, t0[q][1],
n0[q][1], k0[q][2] ? "+" : "-", t0[q][2], n0[q][2], q ? "Stomer" : "Gauss");
if((t = (long *)calloc((d += 5) + 1, sizeof(long))) == NULL)
return 1;
if((pi = (long *)calloc(d + 1, sizeof(long))) == NULL)
return 2;
for(i = d; i >= 0; i--) pi[i] = 0;
for(p = 0; p < 3; p++)
{
for(k=k0[q][p], n=n0[q][p], t[i=j=d]=t0[q][p], i--; i >= 0; i--)
t[i] = 0;
for(r = 0, i = j; i >= 0; i--)
{
r = (m = 10 * r + t[i]) % n;
t[i] = m / n;
k ? (pi[i] += t[i]) : (pi[i] -= t[i]);
}
while (j > 0 && t[j] == 0)
j--;
for (k = !k, s = 3, n *= n; j > 0; k = !k, s += 2)
{
for (r = 0, i = j; i >= 0; i--)
{
r = (m = 10 * r + t[i]) % n;
t[i] = m / n;
}
while (j > 0 && t[j] == 0)
j--;
for (r = 0, i = j; i >= 0; i--)
{
r = (m = 10 * r + t[i]) % s;
m /= s;
k ? (pi[i] += m) : (pi[i] -= m);
}
}
}
for (n = i = 0; i <= d; pi[i++] = r)
{
n = (m = pi[i] + n) / 10;
if ((r = m % 10) < 0)
r += 10, n--;
}
printf("pi= %ld.", pi[d]);
for (i = d - 1; i >= 5; i--)
printf("%ld%s", pi[i], ((m = d - i + 5) % 65) ? ((m % 5) ? "" : " ") : "\n");
printf("%sDIGITS: %d\n", (m % 65) ? "\n" : "", d - 5);
return 2;getch();
}

回答(2):

不过用tc编译的时候会提示错误 因为数组太长
最好找别的编译器
#include "stdio.h"
#include "conio.h"
long a=10000, b, c=28000, d, e, f[28010], g;

void main()
{
for( ;b-c; ) f[b++] =a/5;
for( ; d=0, g=c*2; c-=14,printf("%.4d",e+d/a),e=d%a)
for(b=c; d+=f[b]*a,f[b] =d%--g,d/=g--,--b; d*=b ) ;
getch( );
return;
}

/*
* 编译成可执行文件后,在DOS提示符下执行:
* 如果带一个命令行参数,则该值为要计算的位数;
* 如果还带有第二个参数,则使用stomer公式计算。
* 代码如下:
*/

#include
#include

void main(int argc,char *argv[])
{
long *pi,*t,m,n,r,s;
int t0[][3] = {48,32,20,24,8,4},k0[][3] = {1,1,0,1,1,1},n0[][3] = {18,57,239,8,57,239};
int d,i,j,k,p,q;
d=(argc>1)?(((i = atoi(argv[1]))<0)?0:i):1000;
q=(argc>2)?1:0;
printf("%s\n\n", "Nature (R) Pi value compute Program (C) Tue 2005.11.10");
printf("pi= %s%d * arctg(1/%d) %s %d * arctg(1/%d) %s %d * arctg(1/%d) [%s]\n",
k0[q][0] ? "" : "-", t0[q][0], n0[q][0], k0[q][1] ? "+" : "-", t0[q][1],
n0[q][1], k0[q][2] ? "+" : "-", t0[q][2], n0[q][2], q ? "Stomer" : "Gauss");
if((t = (long *)calloc((d += 5) + 1, sizeof(long))) == NULL)
return 1;
if((pi = (long *)calloc(d + 1, sizeof(long))) == NULL)
return 2;
for(i = d; i >= 0; i--) pi[i] = 0;
for(p = 0; p < 3; p++)
{
for(k=k0[q][p], n=n0[q][p], t[i=j=d]=t0[q][p], i--; i >= 0; i--)
t[i] = 0;
for(r = 0, i = j; i >= 0; i--)
{
r = (m = 10 * r + t[i]) % n;
t[i] = m / n;
k ? (pi[i] += t[i]) : (pi[i] -= t[i]);
}
while (j > 0 && t[j] == 0)
j--;
for (k = !k, s = 3, n *= n; j > 0; k = !k, s += 2)
{
for (r = 0, i = j; i >= 0; i--)
{
r = (m = 10 * r + t[i]) % n;
t[i] = m / n;
}
while (j > 0 && t[j] == 0)
j--;
for (r = 0, i = j; i >= 0; i--)
{
r = (m = 10 * r + t[i]) % s;
m /= s;
k ? (pi[i] += m) : (pi[i] -= m);
}
}
}
for (n = i = 0; i <= d; pi[i++] = r)
{
n = (m = pi[i] + n) / 10;
if ((r = m % 10) < 0)
r += 10, n--;
}
printf("pi= %ld.", pi[d]);
for (i = d - 1; i >= 5; i--)
printf("%ld%s", pi[i], ((m = d - i + 5) % 65) ? ((m % 5) ? "启毁亏" : " ") : "余腔\悄神n");
printf("%sDIGITS: %d\n", (m % 65) ? "\n" : "", d - 5);
return 2;getch();
}

回答(3):

不过用tc编译的时晌信候会提示错误 因为数组太长
最好找别明谨销激游的编译器
#include "stdio.h"
#include "conio.h"
long a=10000, b, c=28000, d, e, f[28010], g;

void main()
{
for( ;b-c; ) f[b++] =a/5;
for( ; d=0, g=c*2; c-=14,printf("%.4d",e+d/a),e=d%a)
for(b=c; d+=f[b]*a,f[b] =d%--g,d/=g--,--b; d*=b ) ;
getch( );
return;
}

回答(4):

B-S 那些盗用版权者

回答(5):

495196274 - 魔法学徒 一级