已阅读5页,还剩16页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
9 .5 模块化程序设计实例程序设计基础(基于C语言讲解) 石光华 编著 北京: 清华大学出版社下面以设计一个简单的成绩管理软件为例,一步一步地按模块化程序设计方法进行设计。1 .定义问题设计一个成绩管理软件,其基本功能包括:输入成绩,成绩加分,计算平均成绩,找出最高分,找出最低分,输出成绩等。2 .确定组成程序的模块根据成绩管理软件的功能,确定软件的基本模块包括:输入模块,加分模块,平均分模块,最高分模块,最低分模块,输出模块等。142 程序设计基础3 .绘制程序结构图成绩管理软件的结构图如图9-5所示。图9-5 成绩管理软件结构图4 .流程图用流程图确定主程序的逻辑结构,如图9-6所示。在流程图中,istate 的作用是记录是否已经输入成绩。istate 的使用有如下两种方式。(1) 作为全局变量使用。此时istate可以在所有模块中改变其值,主程序更简洁,但可能产生边际效应。(2) 作为主程序的局部变量使用。此时istate只能在主程序中改变其值。在主程序中可以直观地看到其变化,能够防止边际效应。采用方式(2)的主程序如下。#include #define SIZE 10void main()int iscoreSIZE =0;int key= - 1;int iresult=0;float fresult=0;int istate=0;printf(1:Input scores;n);第9章 模块化程序设计 143图9-6 成绩管理软件主程序流程图printf(2:Output scores;n);printf(3:Count for the max score;n);printf(4:Count for the minimum score;n);printf(5:Count for the total score;n);printf(6:Count for theaverage score;n);printf(- 1:Exit .n);while(1)printf(Please input your choose:);scanf(%d,&key);if (key = = - 1)144 程序设计基础break;switch(key)case1:istate=input_all_numbers(iscore,SIZE);break;case2:if (istate = =0)printf(ERROR:You must input scoresfirst ! n);elseoutput_all_numbers(iscore,SIZE);break;case3:if (istate = =0)printf(ERROR:You must input scoresfirst ! n);elseiresult=count_for_max(iscore,SIZE);printf(the max score is %dn,iresult);break;case4:if (istate = =0)printf(ERROR:You must input scoresfirst ! n);elseiresult=count_for_min(iscore,SIZE);printf(the min score is %dn,iresult);break;case5:if (istate = =0)printf(ERROR:You must input scoresfirst ! n);else第9章 模块化程序设计 145iresult=count_for_total(iscore,SIZE);printf(the total score is %dn,iresult);break;case6:if (istate = =0)printf(ERROR:You must input scoresfirst ! n);elsefresult=count_for_average(iscore,SIZE);printf(the average score is % .2fn,fresult);break;default:printf(ERROR:Input error,please input again! n);5 .编写算法为程序结构图中每个模块编写算法。在前面的学习中,已经学过如何加分,计算平均分,以及查找最高、最低分,在这里就不再画出流程图了。6 .审查算法最后审查整个算法,直到没有任何逻辑错误。7 .编程调试审查算法后,即可进行编程调试。【例9-12】 成绩管理软件的完整程序。/ *name:a management system about scores*/ *creat:stone,2004/ 3/ 8*/146 程序设计基础/ *modify:stone,2004/ 3/ 20*/ *version:1 .0#include #define SIZE 5 / *定义成绩个数的符号常量*/int input_all_numbers(int iscore,int isize);void output_all_numbers(int iscore,int isize);int count_for_max(int iscore,int isize);int count_for_min(int iscore,int isize);int count_for_total(int iscore,int isize);floatcount_for_average(int iscore,int isize);void main()int iscoreSIZE =0;int key= - 1;int iresult=0;float fresult=0;/ *用于区分是否已经输入数据的标志,0表示未输入,1表示已经输入*/int istate=0;/ *主菜单,可以选择完成不同的成绩统计功能*/printf(*n);printf(This is a management system about scores .nttWELCOME ! n);printf(*n);printf(1:Input scores;n);printf(2:Output scores;n);printf(3:Count for the max score;n);printf(4:Count for the minimum score;n);printf(5:Count for the total score;n);printf(6:Count for theaverage score;n);printf(- 1:Exit .n);while(1)printf(Please input your choose:);scanf(%d,&key);/ *根据输入选择的不同,分别进行不同的处理*/第9章 模块化程序设计 147if (key = = - 1)break;switch(key)case 1:istate=input_all_numbers(iscore,SIZE);break;case 2:if (istate = =0)printf(ERROR:You must input scores first ! n);elseoutput_all_numbers(iscore,SIZE);break;case 3:if (istate = =0)printf(ERROR:You must input scoresfirst ! n);elseiresult=count_for_max(iscore,SIZE);printf(the max score is %dn,iresult);break;case 4:if (istate = =0)printf(ERROR:You must input scores first ! n);elseiresult=count_for_min(iscore,SIZE);printf(the min score is %dn,iresult);break;case 5:if (istate = =0)printf(ERROR:You must input scores first ! n);148 程序设计基础elseiresult=count_for_total(iscore,SIZE);printf(the total score is %dn,iresult);break;case 6:if (istate = =0)printf(ERROR:You must input scores first ! n);elsefresult=count_for_average(iscore,SIZE);printf(the average score is % .2fn,fresult);break;default:printf(ERROR:Input error,please input again! n);/ *功能: 输入学生成绩*/int input_all_numbers(int iscore,int isize)int iindex=0;printf(please input %d scores:n,isize);for(iindex=0; iindex scanf(%d,&iscoreiindex);return1;/ *功能: 输出学生成绩*/第9章 模块化程序设计 149void output_all_numbers(int iscore,int isize)int iindex=0;for(iindex=0; iindex printf(iscore%2d = %3dn,iindex,iscoreiindex);/ *功能: 计算最高分*/int count_for_max(int iscore,int isize)int imax=iscore0;int iindex=0;for(iindex=0; iindex if (imaxISCOREIINDEX) imax=iscoreiindex;return imax;/ *功能: 计算最低分*/int count_for_min(int iscore,int isize)int imin=iscore0;int iindex=0;for(iindex=0; iindex if (iminiscoreiindex)imin=iscoreiindex;return imin;150 程序设计基础/ *功能: 计算总分*/int count_for_total(int iscore,int isize)int iindex=0;int isum=0;for(iindex=0; iindex isum=isum+iscoreiindex;return isum;/ *功能: 计算平均分*/floatcount_for_average(int iscore,int isize)int iindex=0;float faverage=0;for(iindex=0; iindex faverage=faverage+iscoreiindex;faverage=faverage/ isize;returnfaverage;由于 C语言中,常用的输入语句 scanf()存在缺陷,在1 .0 版中,输入成绩时,如果误按了字母键,则程序会出现异常。为此,我们给出了一个改进版,通过自定义函数 int getdigi(int icount)进行输入,从而解决了这个问题,提高了程序的稳定性和健壮性。这个主程序能够灵活处理不同的分支情况,可以作为 C语言程序的一个基本框架。程序设计中不同的风格与经验,对程序的易用性和健壮性影响很大,要设计和编写出成功的程序,需要更深入的学习,需要丰富的经验。第9章 模块化程序设计 151【例9-13】 成绩管理软件程序的改进版。/ *name:a management system about scores*/ *creat:stone,2004/ 3/ 8*/ *modify:stone,2004/ 3/ 30*/ *version:1 .11 .改进了输入时误按字符导致程序失效的问题2 .采用void 型函数,简化主程序3 .采用功能键退出程序,符合常例*/#include #include #define ESC 27 / *使用功能键控制正常退出*/#define SIZE 5 / *定义成绩个数的符号常量*/#define MAXdigit 20 / *定义数据的最大字符个数*/int input_all_score(int iscore,int isize);void output_all_score(int iscore,int isize);voidcount_for_max(int iscore,int isize);voidcount_for_min(int iscore,int isize);voidcount_for_total(int iscore,int isize);voidcount_for_average(int iscore,int isize);int getdigi(int icount);void main()int iscoreSIZE =0;char ckey=a;/ *用于区分是否已经输入了数据的标志,0表示未输入,1表示已经输入*/int istate=0;/ *主菜单,可以选择完成不同的成绩统计功能*/doclrscr();printf(* n);printf(This is a management system about scores . n t tWELCOME !n);printf(*n);152 程序设计基础printf(1:Input scores;n);printf(2:Output scores;n);printf(3:Count for the max score;n);printf(4:Count for the minimum score;n);printf(5:Count for the total score;n);printf(6:Count for the average score;n);printf(Esc:Exit system .n);printf(Please input your choose(1-6):n);/ *根据输入的选择的不同,分别进行不同的处理*/ckey=getch();if(ckey= =1)istate=input_all_score(iscore,SIZE);else if(istate= =0)&(ckey !=Esc)printf(nERROR:You must input scores first !);printf(nPress a key tocontinue .);getch();else if (ckey= =2)output_all_score(iscore,SIZE);else if(ckey= =3)count_for_max(iscore,SIZE);else if(ckey= =4)count_for_min(iscore,SIZE);else if(ckey= =5)count_for_total(iscore,SIZE);else if(ckey= =6)count_for_average(iscore,SIZE);while(ckey !=Esc);/ *功能: 输入学生成绩*/int input_all_score(int iscore,int isize)第9章 模块化程序设计 153int iindex=0;printf(please input %d scores:n,isize);for(iindex=0; iindex printf(nplease input the %d scores:,iindex);iscoreiindex =getdigi(3);return1;/ *功能: 输出学生成绩*/void output_all_score(int iscore,int isize)int iindex=0;for(iindex=0; iindex printf(iscore%2d = %3dn,iindex,iscoreiindex);getch();/ *功能: 计算最高分*/voidcount_for_max(int iscore,int isize)int imax=iscore0;int iindex=0;for(iindex=0; iindex if (imaxISCOREIINDEX) imax=iscoreiindex;printf(nThe max score is %d,imax);154 程序设计基础getch();/ *功能: 计算最低分*/voidcount_for_min(int iscore,int isize)int imin=iscore0;int iindex=0;for(iindex=0; iindex if (iminiscoreiindex)imin=iscoreiindex;printf(nThe min score is %d,imin);getch();/ *功能: 计算总分*/voidcount_for_total(int iscore,int isize)int iindex=0;int isum=0;for(iindex=0; iindex isum=isum+iscoreiindex;printf(nThe total score is %d,isum);getch();/ *功能: 计算平均分*/voidcount_for_average(int iscore,int isize)第9章 模块化程序设计 155int iindex=0;float faverage=0;for(iindex=0; iindex faverage=faverage+iscoreiindex;faverage=faverage/ isize;printf(nThe average score is % .2f,faverage);getch();/ *function:输入指定个数的数字,构成一个整形数据,可以用来代替scanf*/ *name:getdigi() */ *creat:stone,2004/ 3/ 25*/ *modify:stone,2004/ 3/ 26*/ *parametric:int icount,数字个数*/ *return:int,整形数字*/int getdigi(int icount)int iloop=0;char digit_strMAXdigit =0;iloop=0;dodigit_striloop =getch();if (isdigit(digit_striloop)putch(digit_striloop);iloop+ + ;while(iloopICOUNT); returnatoi(digit_str);156 程序设计基础双语精髓The mainlineSince each module performs a single specific task,a mainline routine must providethe master control that tiesall themodules together andcoordinates their activity .Thisprogram mainline should show the main processing functions,and the order in whichtheyare to be performed . It should also show the flow of data and the major controlstructures .The mainline should alsobeeasy to read,be of manageable length and showsound logic structure .主程序由于每个模块完成一个特定的任务,因此需要一个主程序或主模块进行总体控制,把所有的模块组合在一起并协调它们之间的活动。主程序显示了程序的主要功能,以及这些功能完成的顺序,还显示了数据流向和主要的控制结构。另外,主程序应该容易阅读,从程序长度上易于管理,并有合理的逻辑结构。ModuleA module must be large enough to perform its task,and must include only theoperations thatcontribute tothe performanceof that task .It shouldhaveasingleentry,anda single exit with a top - to - bottom sequence of instructions . The name of themodule shoulddescribe thework tobedoneasa single
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2026学年我来配音教学设计
- 人工智能自然语言生成创新创业项目商业计划书
- 3.2.1醇 教学设计 高二下学期化学人教版(2019)选择性必修3
- 鼓励艺术修养提升创造性潜能办法
- 6.1 化学反应与能量变化 第二课时《化学反应与电能》 教学设计 2025-2026学年高一下学期化学人教版(2019)必修第二册
- 2027年辽恒职业学院高职单招职业适应性测试考试模拟试卷【有一套】附答案详解
- 2024年天府新区信息职业学院高职单招职业适应性测试考试模拟试卷及参考答案详解(预热题)
- 2025年张家口现代能源职业学院单招职业技能考试题库及答案详解
- 2026年陕西商贸职业学院高职单招职业技能考试题库附答案详解(夺分金卷)
- 2025年山西朔州桑干河职业学院单招综合素质考试模拟试卷及完整答案详解一套
- 地下连续墙专项施工方案
- 2026年度市场调研采购合同书
- 雨课堂学堂在线学堂云《走进军事理论(空军工程)》单元测试考核答案
- 2026年安徽马鞍山市中考语文试题(附答案)
- 心理干预的时机与方式
- (正式版)DB43∕T 1973-2020 《涉路工程安全技术规范》
- 矿山生态恢复项目管理方案
- 天主教管理工作制度汇编
- 水利水电工程单元工程施工质量检验表与验收表(SLT631.6-2025)
- CBT在精神分裂症治疗中的应用
- 吊篮使用应急预案(3篇)
评论
0/150
提交评论