⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex6_12.cpp

📁 c++程序设计教材第二版书上即课后习题源代码
💻 CPP
字号:
//【例6.12】设计梯形法求积分的函数模板,梯形法求积分的函数为独立的非成员函数。该方法更简洁。
#include <iostream>                                                    
#include <iomanip>
using namespace std;

class F1 {
public:
	double fun(double x){return (1+x+2*x*x);}
};
class F2 {
public:
	double fun(double x){return (1+x+2*x*x+3*x*x*x);}
};
class F3 {
public:
	double fun(double x){return (1+x+2*x*x+3*x*x*x+4*x*x*x*x);}
};

template<typename T>double integer (T cf,float a, float b,int n){
	double result,step;
	result=(cf.fun(a)+cf.fun(b))/2;
	step=(b-a)/n;
	for (int i=1;i<n;i++) result+=cf.fun(a+i*step);
	result*=step;
	return result;
}

int main(){
	F1 f1;F2 f2;F3 f3;
	double fixint1, fixint2, fixint3;
	int n=1000;
	fixint1=integer(f1,0.0,3.0,n);
	fixint2=integer(f2,0.0,3.0,n);
	fixint3=integer(f3,0.0,3.0,n);
	cout<<fixint1<<'\n'<<fixint2<<'\n'<<fixint3<<'\n';
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -