YTU 2421: C语言习题 矩形法求定积分

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

时间限制: 1 Sec  内存限制: 128 MB
提交: 354  解决: 234

题目描述

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

 

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

输入

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

输出

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

样例输入

0 1
0 1
0 1

样例输出

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

提示



主函数已给定如下,提交时不需要包含下述主函数



/* C代码 */

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;

    scanf("%f%f",&a1,&b1);

    scanf("%f%f",&a2,&b2);

    scanf("%f%f",&a3,&b3);

    p=fsin;

    c=integral(p,a1,b1,n);

    printf("The integral of sin(x) is :%.2f\n",c);

    p=fcos;

    c=integral(p,a2,b2,n);

    printf("The integral of cos(x) is :%.2f\n",c);

    p=fexp;

    c=integral(p,a3,b3,n);

    printf("The integral of exp(x) is :%.2f\n",c);

    return 0;

}





/* C++代码 */

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;

}

迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

#include <stdio.h>
#include <math.h>
float integral(float (*p)(float),float a,float b,int n)
{
    float s=0.0,i,w;
    w=(b-a)/n;
    for(i=a; i<=b; i+=w)
        s+=p(i+w)*w;
    return s;
}
float fsin(float a)
{
    return sin(a);
}
float fcos(float a)
{
    return cos(a);
}
float fexp(float a)
{
    return exp(a);
}
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;
    scanf("%f%f",&a1,&b1);
    scanf("%f%f",&a2,&b2);
    scanf("%f%f",&a3,&b3);
    p=fsin;
    c=integral(p,a1,b1,n);
    printf("The integral of sin(x) is :%.2f\n",c);
    p=fcos;
    c=integral(p,a2,b2,n);
    printf("The integral of cos(x) is :%.2f\n",c);
    p=fexp;
    c=integral(p,a3,b3,n);
    printf("The integral of exp(x) is :%.2f\n",c);
    return 0;
}

posted @ 2016-03-03 09:20  小坏蛋_千千  阅读(474)  评论(0编辑  收藏  举报