YTU:C语言习题 矩形法求定积分
来源:互联网 发布:钱咖试客类似的软件 编辑:程序博客网 时间:2024/04/28 05:04
Description
写一个用矩形法求定积分的通用函数,分别求
(说明: sin,cos,exp已在系统的数学函数库中,程序开头要用#include<cmath>)。
Input
输入求sin(x) 定积分的下限和上限
输入求cos(x) 定积分的下限和上限
输入求exp(x) 定积分的下限和上限
Output
求出sin(x)的定积分
求出cos(x)的定积分
求出exp(x)的定积分
Sample Input
0 10 10 1
Sample Output
The integral of sin(x) is :0.48The integral of cos(x) is :0.83The integral of exp(x) is :1.76
HINT
主函数已给定如下,提交时不需要包含下述主函数
/* 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;
}
比较容易的一个题,关键是理解矩形法,本题的ac代码是用矩形右边值的函数值作高,用左边无法ac
下面是代码
#include <stdio.h>
#include <math.h>float integral(float (*p)(float),float a,float b,int n)
{
float i=0,sum=0.0,k=(b-a)/n;
for(i=0;i<n;i++)
{
sum+=(k*(p(a+(i+1)*k)));//就是运用数学矩形的方法一个一个加 求出的值是近似值
}
return (sum);
}
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;
}
0 0
- YTU:C语言习题 矩形法求定积分
- YTU 2421: C语言习题 矩形法求定积分
- Problem E: C语言习题 矩形法求定积分
- Problem E: C语言习题 矩形法求定积分
- Problem E: C语言习题 矩形法求定积分
- Problem E: C语言习题 矩形法求定积分
- YTU 2414: C语言习题 字符串排序
- YTU 2416: C语言习题 成绩输出
- YTU 2426: C语言习题 字符串排序
- YTU 2427: C语言习题 整数排序
- YTU 2417: C语言习题 字符串长度
- YTU 2424: C语言习题 字符串比较
- YTU 2425: C语言习题 输出月份
- YTU C语言习题 整数排序
- YTU:C语言习题 矩阵元素变换
- YTU:C语言习题 n个数逆序
- YTU: C语言习题 字符串比较
- YTU:C语言习题 输出月份
- Java工程师成神之路
- 通过一个例子快速上手矩阵求导
- java堆压测
- 图像有用区域
- JAVA gc垃圾回收机制
- YTU:C语言习题 矩形法求定积分
- MyReport实践:为ERP系统添加打印功能
- [LeetCode] Validate Binary Search Tree
- 移动硬盘打不开,提示“文件或目录损坏无法读取”
- leetCode题解
- Windows 10 配置vagrant环境
- 设计模式--创建型(2)
- new与malloc的10点区别
- OpenCV中GPU模块的使用