求解bat和C语言还有VB计算圆周率的代码。

我说的是把圆周率π算出来。就是那个啥3.1415926........不是算圆的周长和面积。求大神帮忙~~

算法[ma教教主]说的很明白了,直接贴代码了。

 

C:

#include 
long a = 10000, b, c = 2800, d, e, f[2801], g;
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);
}

 

 VB:

Dim sum As Double, t As Double
Dim N As Integer
N = 6 'N是计算精度,越大越精确,但运算越慢  
sum = 0: t = 1
Do
    sum = sum + (-1) ^ (t + 1) / (2 * t - 1)
   t = t + 1
Loop Until Abs((-1) ^ (t + 1) / (2 * t - 1)) < 10 ^ (-N)
Magbox 4 * sum

 

BAT:

@echo off 
SETLOCAL ENABLEDELAYEDEXPANSION 
set pi=400000000
set t=0
set p=3
set times=0
set det=-1
set p1=-3
:loop
set /a 
t=400000000/p1
set /a pi=pi+t
set /a p=p+2
set /a det=det*-1
set /a 
p1=p*det
set /a times=times+1
if %times% geq 4000 goto ex :: 可以写成 if %times% geq 40000 goto ex
goto loop
:ex
set /a pi=pi%%100000000
echo 结果 3.%pi%
pause

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-10
(1/n)^2(n=1.2,3.....)
求和之后=(Pi^2)/6
所以用个循环语句,求和之后(n有1000就好)乘以6。再开根就好追问

大神。那C语言的怎么写~还有VB

追答

vb For i = 1 To 5 。。。。 next i 这样循环五次

c也是for 但是是用参数来设置for (int j = 0; j < 6; j++)循环五次

相似回答