实验二 模块化程序设计.doc_第1页
实验二 模块化程序设计.doc_第2页
实验二 模块化程序设计.doc_第3页
实验二 模块化程序设计.doc_第4页
实验二 模块化程序设计.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验二 模块化程序设计实验目的(1) 体验结构化程序设计方法要点,理解模块化设计的思想,划分子模块的原则(2) 掌握is函数的定义和调用实验内容任务一输出m-n之间所有的素数(ex02_06.cpp)改写( sample02_07.cpp )保存为ex02_06.cpp:输出m-n之间所有的素数,调用int isprime(int n)函数,每行输出10个素数。sample02_07.cpp/*求100以内的全部素数,每行输出10个。素数就是只能被1和自身整除的正整数,1不是素数,2是素数。*/* 使用嵌套循环求100以内的全部素数 */#include #include /* 调用求平方根函数,需要包含数学库 */int main(void) int count, i, m, n; count = 0; /* count记录素数的个数,用于控制输出格式 */ for(m = 2; m = 100; m+) n = sqrt(m); for(i = 2; i n) /* 如果m是素数 */ printf(%6d, m); /* 输出m */ count+; /* 累加已经输出的素数个数 */ if(count % 10 = 0) /* 如果count是10的倍数,换行 */ printf(n); printf(n); return 0;学号: 姓名: 专业班级:( )( )班1源程序清单:ex02_06.cpp 3。实现算法(自然语言描述)2运行结果任务二 打印表格ex02_07.cpp改写ex02_03.cpp,保存为ex02_07.cpp(1)增加函数void PrtLine(int bottom,int height),已知底长和高,输出一行表格数据。(2)增加函数void PrtBody(void) 打印表格除表头和标题以外的主体内容部分,调用PrtLine实现。(3)在main函数中输出表头和标题,调用PrtBody打印表格主体。ex02_03.cpp#includevoid main()int bottom,height; int CSarea; / 横截面积 double inertia; /惯性力矩 double modulus; /截面系数 printf( 木材工程特性表 n); printf(木材尺寸横截面积 惯性力矩截面系数 n); for(bottom=2;bottom=10;bottom+=2) for(height=2;height=12;height+=2) CSarea=bottom*height; inertia=(double)(bottom*height*height*height)/12; modulus=(double)(bottom*height*height)/6; printf(%2d * %2d%8d%10.2f%8.2f n, bottom,height,CSarea,inertia,modulus); 1源程序清单:ex02_07.cpp2运行结果截图任务三 改错 求PI值error02_02.cpp 打开程序文件error02_02.cpp改正程序中的错误。根据下式求PI值,直到某一项小于10-6PI/2=1+1!/3 +2!/(3*5)+ +n!/(3*5*(2n+1)正确运行结果PI=3.14159error02_02.cpp#include int fact(int n);int multi(int n);int main(void) int i; double sum, item, eps; eps = 1e-6; sum = 1; item = 1; for(i = 1;item = eps; i+)item = fact(i) / multi(2*i + 1);sum = sum + item; /* 调试时设置断点 */ printf(PI=%0.5lfn, sum * 2); return 0;int fact(int n) int i; double res; res = 1; for(i = 0; i = n; i+)res = res * i; return res; /* 调试时设置断点 */int multi(int n) int i; double res; res = 1; for(i = 3; i = n; i = i+2)res = res * i; return res; /* 调试时设置断点 */任务四 天气统计 ex02_13.cpp编写一个程序处理一组日最高气温。程序需要统计并打印出高温天数(最高温度为华氏85或更高),舒适天数(最高温度为华氏6085),以及寒冷天数(最高温度小于华氏60),最后显示平均温度。从records.txt文件中读取最高气温数据进行分类统计。测试数据如下:5562687459454158606765788288919290938780787972686159 源程序清单:实验体会实验过程评价:碰到什么问题?如何解决?#includevoid PrtBody(void);void PrtLine(int bottom,int height);void main() printf( 木材工程特性表 n); printf(木材尺寸横截面积 惯性力矩截面系数 n);PrtBody();void PrtBody(void) int bottom,height; for(bottom=2;bottom=10;bottom+=2) for(height=2;height=12;height+=2) PrtLine(bottom,height); void PrtLine(int bottom,int height)double inertia; /惯性力矩 double modulus; /截面系数 int CSarea;/ 横截面积CSarea=bottom*height;inertia=(double)(bottom*height*height*height)/12; modulus=(double)(bottom*height*height)/6; printf(%2d * %2d%8d%10.2f%8.2f n,bottom,height,CSarea,inertia,modulus); #include #include /* 调用求平方根函数,需要包含数学库 */int isprime(int n);int main(void) int count, i, m, n; count = 0; /* count记录素数的个数,用于控制输出格式 */ for(m = 2; m = 100; m+) if (isprime(m) /* 如果m是素数 */ printf(%6d, m); /* 输出m */ count+; /* 累加已经输出的素数个数 */ if(count % 10 = 0) /* 如果count是10的倍数,换行 */ print

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论