试验二模块化程序设计
学号: 姓名: 专业班级:( )( )班
实验二 模块化程序设计
实验目的
体验结构化程序设计方法要点,理解模块化设计的思想及划分子模块的原则。
掌握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 stdio.h
#include math.h /* 调用求平方根函数,需要包含数学库 */
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; i++)
if(m % i == 0) break;
if(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_07_1.cpp,保存为ex02_07.cpp
(1)编写函数void PrtLine(int bottom,int height):已知底长和高,输出一行表格数据。
(2)编写函数void PrtBody(void):打印表格除表头和标题以外的主体内容部分,调用PrtLine实现。
(3)在main函数中输出表头和标题,调用PrtBody打印表格主体。
ex02_07_1.cpp
#includestdio.h
void 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.cpp
2.运行结果截图
任务三 天气统计 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
原创力文档

文档评论(0)