实验二 模块化程序设计 实验目的
(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
int count, i, m, n;
count = 0; /* count记录素数的个数,用于控制输出格式 */ for(m = 2; m <= 100; m++){ n = sqrt(m);
for(i = 2; i <= n; i++)
if(m % i == 0) break; if(i > n){ /* 如果m是素数 */ printf(\ /* 输出m */
count++; /* 累加已经输出的素数个数 */ if(count % 10 == 0) /* 如果count是10的倍数,换行 */ printf(\ } }
printf(\
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
#include
{int bottom,height;
int CSarea; // 横截面积 double inertia; //惯性力矩 double modulus; //截面系数
printf(\ 木材工程特性表 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(\ ? .2f %8.2f \\n\ bottom,height,CSarea,inertia,modulus); } }
1.源程序清单:ex02_07.cpp
2.运行结果截图
学号: 姓名: 专业班级:(
\\n\)( )班
任务三 改错 求PI值error02_02.cpp
打开程序文件error02_02.cpp改正程序中的错误。根据下式求PI值,直到某一项小于10-6 PI/2=1+1!/3 +2!/(3*5)+…… +n!/(3*5*…*(2n+1)) 正确运行结果 PI=3.14159
error02_02.cpp #include
printf(\ 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或更高),舒适天数(最高温度为华氏60~85),以及寒冷天数(最高温度小于华氏60),最后显示平均温度。从records.txt文件中读取最高气温数据进行分类统计。 测试数据如下:
55 62 68 74 59 45 41 58 60 67 65 78 82 88 91 92 90 93 87 80 78 79 72 68 61 59 源程序清单:
实验过程评价 实验评价
学号: 姓名:
专业班级:()( )班
相关推荐: