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;
}
YTU 2421: C语言习题 矩形法求定积分的更多相关文章
- C语言复习---矩形法求定积分函数
一:分析: 大一学习积分的时候,我们学习过,可以通过矩形法来求定积分. 思路就是将积分区间划分成n等份,然后将这n等份近似看成矩形(或梯形),然后对所有的矩形(或梯形)的面积进行求和. 二:简单的例子 ...
- YTU 2405: C语言习题 牛顿迭代法求根
2405: C语言习题 牛顿迭代法求根 时间限制: 1 Sec 内存限制: 128 MB 提交: 562 解决: 317 题目描述 用牛顿迭代法求根.方程为ax3+bx2+cx+d=0.系数a,b ...
- YTU 2417: C语言习题 字符串长度
2417: C语言习题 字符串长度 时间限制: 1 Sec 内存限制: 128 MB 提交: 758 解决: 548 题目描述 写一函数,求一个字符串的长度.在main函数中输入字符串,并输出其长 ...
- YTU 2414: C语言习题 字符串排序
2414: C语言习题 字符串排序 时间限制: 1 Sec 内存限制: 128 MB 提交: 656 解决: 305 题目描述 输入n个字符串,将它们按字母由小到大的顺序排列并输出.编写三个函数实 ...
- YTU 2974: C语言习题5.26--文件操作3
2974: C语言习题5.26--文件操作3 时间限制: 1 Sec 内存限制: 128 MB 提交: 213 解决: 92 题目描述 文本文件score.dic 中存储了n名学生的信息(班级编号 ...
- YTU 2973: C语言习题5.25--文件操作2
2973: C语言习题5.25--文件操作2 时间限制: 1 Sec 内存限制: 128 MB 提交: 242 解决: 105 题目描述 文本文件score.dic 中存储了n名学生的信息(班级编 ...
- YTU 2972: C语言习题5.24--文件操作1
2972: C语言习题5.24--文件操作1 时间限制: 1 Sec 内存限制: 128 MB 提交: 248 解决: 94 题目描述 文本文件score.dic 中存储了n名学生的信息(班级编号 ...
- YTU 2425: C语言习题 输出月份
2425: C语言习题 输出月份 时间限制: 1 Sec 内存限制: 128 MB 提交: 476 解决: 287 题目描述 编写一程序,输入月份号,输出该月的英文月名.例如,输入3,则输出Mar ...
- YTU 2420: C语言习题 不等长字符串排序
2420: C语言习题 不等长字符串排序 时间限制: 1 Sec 内存限制: 128 MB 提交: 460 解决: 239 题目描述 在主函数中输入n(n<=10)个不等长的字符串.用另一函 ...
随机推荐
- Linux(10):期中架构(2)--- NFS存储服务 & 实时同步
1. 共享存储服务概念: # NFS是Network File System的缩写,中文意思是网络文件系统, # 它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录. 2. ...
- 洛谷 [P3834] 可持久化线段树(主席树)
主席树可以存储线段树的历史状态,空间消耗很大,一般开45n即可 #include <iostream> #include <cstdio> #include <cstri ...
- KVM 网络虚拟化基础
网络虚拟化是虚拟化技术中最复杂的部分,学习难度最大. 但因为网络是虚拟化中非常重要的资源,所以再硬的骨头也必须要把它啃下来. 为了让大家对虚拟化网络的复杂程度有一个直观的认识,请看下图 这是 Open ...
- MVC 上传文件的方法
这两天又开始研究MVC了,期间断断续续已经搞了好久了,可是都没坚持下来.囧!这次一定坚持搞出来一个名堂. 废话少说,直接上代码. 前台引擎采用Razor @model System.Web.HttpP ...
- android本地存储SharedPreferences
SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来 ...
- ORACLE RMAN增量备份经典理解
http://blog.itpub.net/26118480/viewspace-1793548/
- Struts2 文件上传和下载
首先我们写一个单文件长传的fileupload.jsp: <body> <s:fielderror></s:fielderror> <!-- 报错信息 --& ...
- Codeforces D. Iahub and Xors
题目大意:给定一个N*N的区间,1:对(x0,y0,x1,y1)每个直 都xor v: 2: 求(x0,y0,x1,y1)区间的 sum xor: http://codeforces.com/blog ...
- Linux使用screen实现关闭ssh连接的情况下,让程序继续在后台运行
Ubuntu默认没有安装screen,需要手动安装. 安装命令: sudo apt-get install screen 简单的操作方法: 直接输入命令 screen 进入screen子界面,此时pu ...
- PYTHON 源码
http://www.wklken.me/index2.html http://blog.csdn.net/dbzhang800/article/details/6683440