📄 新建 文本文档 (2).txt
字号:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double fun1(double x) /*计算f(x)的值*/
{
if (x==0)
return 1;
else
return (sin(x)/x);
}
double fun2(double x,double h) /*累计求和s*/
{
double s=0.00;
while (x<1)
{
s+=fun1(x);
x+=h;
}
return s;
}
void main()
{
int t,a=0,b=1,k=0;
double t1,t2,s1,s2,c1,c2,r1,r2,h;
h=b-a;
t1=(fun1(a)+fun1(b))*h/2;
cout<<"计算的数据如下:"<<endl;
cout<<setw(9)<<"K"<<setw(13)<<"S"<<setw(14)<<"C"<<setw(12)<<"R"<<endl<<endl;
cout<<k<<setw(13)<<setprecision(8)<<t1<<endl;
for (k=1;k<=5;k++)
{
if (k==1)
{
t=pow(2,1);
t2=t1/2+fun2(a+h/t,2*h/t)*h/t;
s2=4*t2/3-t1/3;
cout<<k<<setw(13)<<setprecision(8)<<t2<<setw(13)<<setprecision(8)<<s2<<endl;
}
if (k==2)
{
t=pow(2,2);
t1=t2;s1=s2;
t2=t1/2+fun2(a+h/t,2*h/t)*h/t;
s2=4*t2/3-t1/3;
c2=16*s2/15-s1/15;
cout<<k<<setw(13)<<setprecision(8)<<t2<<setw(13)<<setprecision(8)<<s2<<setw(13)<<setprecision(8)<<c2<<endl;
}
if (k==3)
{
t=pow(2,3);
t1=t2;s1=s2;c1=c2;
t2=t1/2+fun2(a+h/t,2*h/t)*h/t;
s2=4*t2/3-t1/3;
c2=16*s2/15-s1/15;
r2=64*c2/63-c1/63;
cout<<k<<setw(13)<<setprecision(8)<<t2<<setw(13)<<setprecision(8)<<s2
<<setw(13)<<setprecision(8)<<c2<<setw(13)<<setprecision(8)<<r2<<endl;
}
if (k==4)
{
t=pow(2,4);
t1=t2;s1=s2;c1=c2;r1=r2;
t2=t1/2+fun2(a+h/t,2*h/t)*h/t;
s2=4*t2/3-t1/3;
c2=16*s2/15-s1/15;
r2=64*c2/63-c1/63;
cout<<k<<setw(13)<<setprecision(8)<<t2<<setw(13)<<setprecision(8)<<s2
<<setw(13)<<setprecision(8)<<c2<<setw(13)<<setprecision(8)<<r2<<endl;
if ((r2-r1)<1/2*1e-7)
cout<<"积分值为:"<<r1<<endl;
}
if ((k==5)&&((r2-r1)>1e-7))
{
t=pow(2,5);
t1=t2;s1=s2;c1=c2;r1=r2;
t2=t1/2+fun2(a+h/t,2*h/t)*h/t;
s2=4*t2/3-t1/3;
c2=16*s2/15-s1/15;
r2=64*c2/63-c1/63;
cout<<k<<setw(13)<<setprecision(8)<<t2<<setw(13)<<setprecision(8)<<s2
<<setw(13)<<setprecision(8)<<c2<<setw(13)<<setprecision(8)<<r2<<endl;
if ((r2-r1)<1/2*1e-7)
cout<<"积分值为:"<<r2<<endl;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -