zoukankan      html  css  js  c++  java
  • 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 ",c);

        p=fcos;

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

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

        p=fexp;

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

        printf("The integral of exp(x) is :%.2f ",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
    ",c);
        p=fcos;
        c=integral(p,a2,b2,n);
        printf("The integral of cos(x) is :%.2f
    ",c);
        p=fexp;
        c=integral(p,a3,b3,n);
        printf("The integral of exp(x) is :%.2f
    ",c);
        return 0;
    }
    

  • 相关阅读:
    【QT 学习笔记】 一、 VS2015+ QT环境安装
    Ubuntu16.04 + caffe + cuda 环境搭建
    【Caffe学习笔记】一 、环境安装 Caffe + cuda + windows10 + VS2015 安装笔记, win7也适用
    【CNN】 吴恩达课程中几种网络的比较
    【图像处理】二 高斯滤波及其加速方法
    【图像处理 】 一、OSTU分割法
    1028: C语言程序设计教程(第三版)课后习题8.2
    函数和带参的宏,从三个数中找出最大的数
    Last Defence (run time error)
    Penalty
  • 原文地址:https://www.cnblogs.com/im0qianqian/p/5989424.html
Copyright © 2011-2022 走看看