zoukankan      html  css  js  c++  java
  • 矩形法求定积分通用函数

    题目:实现求sin,cos,exp的通用函数

    思路:其实就是练习指向函数的指针

     1 #include<stdio.h>
     2 #include<math.h> 
     3 int main(){
     4     void fun(float h1,float a1,double(*p)(float x1));
     5     double fexp(float x1);
     6     double fsin(float x1);
     7     double fcos(float x1);
     8     float a,b,h;
     9     double y;
    10     printf("请输入所求区间的下限和上限:");
    11     scanf("%f%f",&a,&b);
    12      h=(b-a)/10000; 
    13     int e;
    14     printf("求sin函数定积分选择 1  "); 
    15     printf("求cos函数定积分选择 2  "); 
    16     printf("求exp函数定积分选择 3  "); 
    17     while(scanf("%d",&e)!=0){
    18     switch(e){
    19         case 1:
    20             fun(h,a,fsin);break;
    21         case 2:
    22             fun(h,a,fcos);break;
    23         case 3:
    24             fun(h,a,fexp);break;
    25         default:
    26             break;
    27     }
    28 } 
    29 }
    30 double fsin(float x1){
    31     return(sin(x1));
    32 }
    33 double fcos(float x1){
    34     return(cos(x1));
    35 }
    36 double fexp(float x1){
    37     return(exp(x1));
    38 }
    39 void fun(float h1,float a1,double(*p)(float x1)){
    40     double y=0;
    41     float x;
    42     x=a1;
    43     for(int i=0;i<10000;i++){
    44         x=x+h1;
    45         y=y+(*p)(x)*h1;
    46     }
    47     printf("定积分为  %lf",y);
    48 }

    说明:矩形法求定积分就是把整个区域分成很多个小矩形,然后计算每个矩形的面积,累加后得到的就是定积分的值。上限减去下限的差除以你分成矩形的个数就是每一个小矩形的底,高为函数值。

    运行结果:

    务实,说实话!
  • 相关阅读:
    javac 命令行使用总结
    电脑右键菜单的编辑(注册表操作)
    C++ —— 类模板的分离式编译
    命令行学习备份
    浏览器老是自动跳出广告垃圾网页
    SQL 事务
    Python中MySQL插入数据
    Python给数字前固定位数加零
    selenium+python3 鼠标事件
    mysql与mongodb命令对比
  • 原文地址:https://www.cnblogs.com/xtuxiongda/p/8316168.html
Copyright © 2011-2022 走看看