ch4_4.cpp
资源名称:c.rar [点击查看]
上传用户:puke2000
上传日期:2022-07-25
资源大小:912k
文件大小:1k
源码类别:
C#编程
开发平台:
Visual C++
- //*********************
- //** ch4_4.cpp **
- //*********************
- #include <iostream.h>
- #include <iomanip.h>
- #include <math.h>
- void main()
- {
- double s=0,x=1; //初始值
- long k=1;
- int sign=1;
- while(fabs(x)>1e-8){ //项值在比较前要先求绝对值
- s += x;
- k += 2;
- sign *= -1;
- x = sign/double(k); //强制转换使x得到浮点数值
- }
- s *= 4; //π值
- cout <<"the pi is " //输出
- <<setiosflags(ios::fixed)
- <<setprecision(8)
- <<s <<endl;
- }