5、给定程序MODI1.C中函数 fun 的功能是:用下面的公式求π的近似值,直到最后一项的绝对值小于指定的数(参数num )为止:
π 1 1 1
┄┄≈1 - ┄┄ + ┄┄ - ┄┄ + ...
4 3 5 7
例如, 程序运行后, 输入0.0001, 则程序输出3.1414。
请改正程序中的错误,使它能输出正确的结果。
注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构!
#include #include float fun ( float num )
{ int s ;
float n, t, pi ;
t = 1 ; pi = 0 ; n = 1 ; s = 1 ;
/**************found**************/
while(t >= num)
{
pi = pi + t ;
n = n + 2 ;
s = -s ;
/**************found**************/
t = s % n ;
}
pi = pi * 4 ;
return pi ;
}
main( )
{ float n1, n2 ;
printf("Enter a float number: ") ;
scanf("%f", &n1) ;
n2 = fun(n1) ;
printf("%6.4f\n", n2) ;
}
| 广告合作:400-664-0084 全国热线:400-664-0084 Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号 珠峰网 版权所有 All Rights Reserved
|