Problem E: C语言习题 矩形法求定积分

Problem E: C语言习题 矩形法求定积分

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 410   Solved: 260
[ Submit][ Status][ Web Board]

Description

写一个用矩形法求定积分的通用函数,分别求

 

(说明: sin,cos,exp已在系统的数学函数库中,程序开头要用#include<cmath>)。

Input

输入求sin(x) 定积分的下限和上限 
输入求cos(x) 定积分的下限和上限
输入求exp(x) 定积分的下限和上限

Output

求出sin(x)的定积分 
求出cos(x)的定积分 
求出exp(x)的定积分

Sample Input

0 1
0 1
0 1

Sample Output

The integral of sin(x) is :0.48
The integral of cos(x) is :0.83
The integral of exp(x) is :1.76

HINT

#include<iostream>
#include<iomanip>
#include<cmath>
#define PI 3.1415
using namespace std;
float fsin( float a)
{
     return sin (a);
}
  float fcos( float a)
  {
      return cos (a);
  }
  float fexp( float a)
  {
      return exp (a);
  }
  float integral( float (*p)( float ), float a, float b, int n)
{
     int i=1;
     float c,sum=0.0;
     c=(b-a)/n;
     a+=c;
     for (i=1;i<=n;i++)
     {
         sum+=p(a)*c;
         a+=c;
     }
     return sum;
}
 
 
int main()
{
     float integral( float (*p)( float ), float a, float b, int n);
     float a1,b1,a2,b2,a3,b3,c,(*p)( float );
     float fsin( float );
     float fcos( float );
     float fexp( float );
     int n=20;
     cin>>a1>>b1;
     cin>>a2>>b2;
     cin>>a3>>b3;
     cout<<setiosflags(ios::fixed);
     cout<<setprecision(2);
     p=fsin;
     c=integral(p,a1,b1,n);
     cout<< "The integral of sin(x) is :" <<c<<endl;
     p=fcos;
     c=integral(p,a2,b2,n);
     cout<< "The integral of cos(x) is :" <<c<<endl;;
     p=fexp;
     c=integral(p,a3,b3,n);
     cout<< "The integral of exp(x) is :" <<c<<endl;
     return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值