赞
踩
- #include<iostream>
- #include<iomanip>
- #include<cmath>
- using namespace std;
- class function
- {
- public:
- virtual double operator()(double x)const=0;
- virtual ~function();
- };
- class myfunction:public function
- {
- public:
- double operator()(double x)const
- {
- return log(1.0+x)/(1.0+x*x);
- }
- };
- class integration
- {
- public:
- virtual double operator()(double a,double b,double eps)const=0;
- virtual ~integration();
- };
- class trapz:public integration
- {
- public:
- virtual double operator()(double x,double y,double eps)const;
- trapz(const function &f):f(f){}
- private:
- const function &f;
- };
- double trapz::operator() (double a,double b,double eps)const
- {
- bool done=false;
- int n=1;
- int h=b-a;
- double tn=(f(a)+f(b))*h/2;//当n=1时,利用公式tn=h(f(x)+f(x+1))/2
- double t2n;
- do
- {
- double sum=0;
- for(int k=0;k<n;k++)
- {
- double x=a+(k+0.5)*h;//h=(b-a)/n,xk=a+kh,t2n=h(f(xk)+f(xk+1))/4+hf(xk+1/2)/2
- sum=sum+f(x);
- }//进行二分,利用递推公式计算新的积分值
- t2n=(tn+h*sum)/2.0;
- if(fabs(t2n-tn)<eps)
- done=true;
- else//进行判断,如果两次计算积分值的差在所给定的误差范围之内,二分后的积分值就是所求得的积分,若不是,则返回第二步,继续执行
- {
- tn=t2n;
- n=n*2;
- h=h/2;
- }
- }while(!done);
- return t2n;
- }
- int main()
- {
- myfunction m;
- trapz t(m);
- cout<<t.operator(0,2,1e-7)<<endl;
- return 0;
- }
哪位大神可以帮我看一下为什么在cout<<t.operator(0,2,1e-7)<<endl这条语句始终报错,我觉得没毛病呀,谢谢啦~
64 18 C:\Users\24864\Desktop\变步长梯形积分.cpp [Error] expected type-specifier before '(' token
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。