C++——写一个矩阵法求定积分的通用函数,定积分如图所示(说明,sin,cos,exp已在系统的数学函数库中,程序开头要用#include<cmath>)用指针或引用方法处理。

没注释的源代码

#include <iostream>
#include<cmath>
using namespace std;
float integral(float(*p)(float),float a,float b,int x);
float fsin(float x);
float fcos(float x);
float fexp(float x);
int main()
{
    float a1,a2,a3,b1,b2,b3,c,(*p)(float);
    int x;
    cout<<"please x:";
    cin>>x;
    cout<<"please input upper(a1) and lower(b1) limlits of sin :";
    cin>>a1>>b1;
    cout<<"please input upper(a2) and lower(b2) limlits of cos :";
    cin>>a2>>b2;
    cout<<"please input upper(a3) and lower(b3) limlits of exp :";
    cin>>a3>>b3;
    p=fsin;
    c=integral(p,a1,a2,x);
    cout<<"the integral of sin(x) is:"<<c<<endl;
    p=fcos;
    c=integral(p,a2,b2,x);
    cout<<"the integral of cos(x) is:"<<c<<endl;
    p=fexp;
    c=integral(p,a3,b3,x);
    cout<<"the integral of exp(x) is:"<<c<<endl;
    return 0;
}
float integral(float(*p)(float),float a,float b,int n)
{
    float x,h,s;
    h=(b-a)/n;
    x=a;
    s=0;
    for(int i=0;i<=n;i++)
    {
        x+=h;
        s+=(*p)(x)*h;
    }
    return s;
}
float fsin(float x)
{
    return sin(x);
}
float fcos(float x)
{
    return cos(x);
}
float fexp(float x)
{
    return exp(x);
}
 

猜你喜欢

转载自blog.csdn.net/2303_80770781/article/details/143437694