求助:VC++关于梯形法和辛普生法求定积分的问题
1、main.cpp
#include <integral.h>
void main()
{
double a=0,b=0;
int n=0;
cout<<"f1(x)=1+x^2\nf2(x)=1+x+x^2+x^3"<<endl;
cout<<"请输入积分上限";
cin>>b;
cout<<"请输入积分下限";
cin>>a;
cout<<"请输入n值";
cin>>n; //输入过程
double tjifenf1,tjifenf2,result1,result2,sjifenf1,sjifenf2;
tjifenf1=integralt(a,b,n,f1);
tjifenf2=integralt(a,b,n,f2);
sjifenf1=integrals(a,b,n,f1);
sjifenf2=integrals(a,b,n,f2);
result1=realintegral(a,b,real1);
result2=realintegral(a,b,real2);
cout<<"梯形结果:\t"<<tjifenf1<<"\t"<<tjifenf2<<endl;
cout<<"误差\t\t"<<result1-tjifenf1<<"\t"<<result2-tjifenf2<<endl;
cout<<"辛普生结果:\t"<<sjifenf1<<"\t"<<sjifenf2<<endl;
cout<<"误差\t\t"<<result1-sjifenf1<<"\t"<<result2-sjifenf2<<endl; //输出过程
}
2、f(x).cpp
double f1(double x)
{
return 1.0+x*x;//函数f1(x)=1+x*x
}
double f2(double x)
{
return 1.0+x+x*x+x*x*x;//函数f2(x)=1+x+x^2+x^3
}
3、tixing.cpp
double integralt(double a,double b,int n,double(*f)(double))//梯形积分算法
{
double h;
h=(b-a)/n;
double result=0.0;
for(int i=1;i<=n;i++)
result+=((*f)(a+i*h)+(*f)(a+(i-1)*h))*h/2.0;
return result;
}
4、sinpson.cpp
double integrals(double a,double b,int n,double(*f)(double))//辛普生积分算法
{
double h;
h=(b-a)/(2*n);
int i;
double l1=0.0,l2=0.0;
for(i=2;i<=2*n-2;i+=2)
l1+=(*f)(a+i*h);
for(i=1;i<=2*n-1;i+=2)
l2+=(*f)(a+i*h);
return ((*f)(a)+(*f)(b)+4*l2+2*l1)*h/3.0;
}
5、real.cpp
double real1(double x)// f1(x)计算牛顿公式原函数
{
return x+x*x*x/3.0;
}
double real2(double x)// f2(x)计算牛顿公式原函数
{
return x+x*x/2+x*x*x/3+x*x*x*x/4;
}
double realintegral(double a,double b,double(*s)(double))//牛顿公式计算算法
{
return (*s)(b)-(*s)(a);
}
6、integral.h
#include <iostream.h>
#include <math.h>
double f1(double x);
double f2(double x);
double integralt(double ,double ,int ,double(*f)(double));
double integrals(double ,double ,int ,double(*f)(double));
double real1(double x);
double real2(double x);
double realintegral(double a,double b,double(*s)(double));//声明
以上程序在VC++6.0里调试后不能运行,有错误,希望高手帮忙修改一下,谢谢啦~~ --------------------编程问答-------------------- 哪一句报错了?
补充:.NET技术 , VC.NET