浅析用折半查找法猜篮球的价格_第1页
浅析用折半查找法猜篮球的价格_第2页
浅析用折半查找法猜篮球的价格_第3页
浅析用折半查找法猜篮球的价格_第4页
浅析用折半查找法猜篮球的价格_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

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

文档简介

1、沈 阳 航 空 工 业 学 院 课程设计学 号 _班 级 _ 姓 名 _指导教师 _年 月 日沈阳航空工业学院课程设计任务书机械与汽车学院 机械设计制造及其自动化专业 5406108 班 学号 200504061291一、课程设计题目:用折半查找法猜篮球的价格二、课程设计工作自2007年7月9日起至2007年7月14日止三、课程设计内容:运用所学的C语言知识,编制和调试程序,具有如下功能:用户从键盘输入篮球的价格(假设价格在1200元之间,并且为整数),让计算器从1元开始用折半法猜测篮球的价格。若计算器的猜测价格偏高,用户输入“高”;若计算器的猜测价格偏低,用户就输入“低”;若计算器猜对了,则

2、显示“正确”。四、课程设计要求:程序质量:l 贯彻结构化程序设计思想。l 用户界面友好,功能明确,操作方便;可以加以其它功能或修饰。l 用户界面中的菜单至少应包括“开始”、“退出”2项。l 代码应适当缩进,并给出必要的注释,以增强程序的可读性。课程设计说明书:课程结束后,上交课程设计说明书(打印稿和电子稿),其内容如下:l 封面l 课程设计任务书l 目录l 需求分析(分析题目的要求)l 程序流程图(总体流程图和主要功能模块流程图)l 核心技术的实现说明及相应程序段l 个人总结l 参考资料l 源程序及适当的注释指导教师:_学生签名:_目录一、需求分析经过对程序设计题目的分析可知,整个程序的设计实

3、现大致分为两个模块,其中每一个模块对应一个函数,他们的功能分别是:主函数定义函数价格范围调用子函数(main),子函数折半查找法猜篮球价格(game)。1、主函数 主要实现程序最初运行时定义程序的总体框架,是程序的总体思想,定义函数“game”,“ mian”,调用子函数“game”。同时需要输入“function”和“price”两个变量和程序的输出界面以及程序中对错误的分析和改正的方法;2、子函数 实现的功能是用折半查找法猜篮球价格;以及输出功能,实现主函数的功能,执行主函数的命令。子函数定义了篮球的最低和最高价格,如果正确则用“mark”代替,如果错误则用“sign”代替。除上面介绍的功

4、能之外,程序还具有退出功能,可以在程序运行正确或者需要退出的时候可以输入0退出。二、程序流程图1、程序总体结构图2、具体功能框图(1)主函数main当(1) Function= =1 真 假 输入1price200 function= =2 真 假 enter= =Y enter= =Y enter= =y enter= =y 真 假 真 假 退出 Game Game (price) (price) (2)子函数game 图(1)(2)子函数guessnum !=price是 judge= =“H”|judge=h 否Mark=1low=guessnumguessnum=(high/low)/

5、2 Judge=L|judge=l 否是 (judge=0) mark=1high=guessnumguessnum=(high+low)/2Sign=1猜出篮球价格 图(2)三、核心技术的实现说明及相应程序段本程序主要由一个子函数和一个主函数组成,其中主函数以菜单的形式调用其他函数来实现要求的所有功能。子函数是实现折半查找法来猜篮球价格的。具体如下。1、子函数 首先要定义变量,最低价格为1,最高价格为200.正确则输出mark,错误则输出sign.然后计算机开始猜篮球的价格。输入0退出。如果如果正确,计算机输出篮球的价格,如果不正确,则计算机提示,如果想要价格好一点的话则输入“H”如果要低一

6、点的话则输出“L”。然后计算机根据折半查找的方法猜篮球的价格。折半查找法的原理就是当你输入“H”的时候计算机就会将最低价格附给”guessnum”.guessnum=(high+low)/2否则当输入“L”的时候,计算机就会把high附给”guessnum”.guessnum=(high+low)/2.如果judge=0正确,程序结束。计算机输出“It is so easy.”然后输出篮球的价格。请输入0退出。否则计算继续。具体程序如下:void Game(int price) int guessnum=1,low=1,high=200,mark=1,sign=0; char judge; p

7、rintf(nThe game is beginning.You can input 0 to exit!); while(guessnum!=price) if(mark) printf(nPC:Is the price %d?,guessnum); else printf(nPC:What does that mean?Please input the letter h orl); printf(nYou want:(H:Lets higher/L:Lets lower):); scanf(%c,&judge); scanf(%c,&judge); if(judge=H|judge=h)

8、mark=1; low=guessnum; guessnum=(high+low)/2; else if(judge=L|judge=l) mark=1; high=guessnum; guessnum=(high+low)/2; else if(judge=0) sign=1; break; else mark=0; if(sign=0) printf(nPC:It is so easy!The price is %d!,price); printf(n The end! ); printf(n input-0-to exit! ); else printf(nThe game is int

9、erupted!); 2主函数主函数是程序的总体框架,主函数定义变量以及调用子函数。主函数定义function和price两个变量。主函数中确定了程序的输出界面。Welcome! Let us go! Input the price. Pc begin guessing. Exit to window.四部分。然后根据需要选择。当输入正确即,价格在1-200之间的时候。计算机继续运算,当输入错误的时候,则计算机提示Please input the price between 1 and 200.然后计算机继续提示,Do you want the computer to guess the pr

10、ice?(Y to allow).当计算机猜出篮球价格的时候,程序结束。当没有正确的按照计算机的指示操作的时候,计算机自动提示,你输入了错误的数字。直到你输入正确的猜测数字。然后输入0退出。具体的程序如下:#include void Game(int price);void main() int function,price; char enter; printf( Welcome! Lets go! n); printf( 1 -Input the price n); printf( 2 -PC begin guessing n); printf( 0 -exit to window n);

11、 printf(please have a choice!n);while(1) scanf(%d,&function); switch(function) case 2: if(function=2) printf(Please input the price first(1200):); scanf(%d,&price); while(price200) printf(Oh,no!Please input the price between 1 and 200:(OK?); scanf(%d,&price); printf(Do you want the computer to guess

12、 the price?(Y to allow):); scanf(%c,&enter); scanf(%c,&enter); if(enter=Y|enter=y) Game(price);break; case 1: printf(Please input the basketball price(1200):); scanf(%d,&price); while(price200) printf(Please input the price between 1 and 200:); scanf(%d,&price); printf(Do you want the computer to gu

13、ess the basketball price?(Y to allow):); scanf(%c,&enter); if(enter=Y|enter=y) Game(price);break; default : printf(You have input a wrong fuction number!); printf(n/*Please guess the basketball price */n); printf(2-PC begin guessingn); printf(1-Input the pricen); printf(nWelcome to nPlease input the

14、 function number:); scanf(%d,&function);break; case 0:exit(0); 四、个人总结折半查找法是程序的主要方法,通过对方法的认识制定方案。程序分为主函数和子函数两部分,主函数是程序的整体框架,子程序执行折半查找法的核心部分。通过此次课程设计我对C语言的知识有了更加深刻的了解。在不断的对程序调试过程中,对以前学过的知识有了更深刻的了解加深了对知识的理解。经过不懈的努力顺利的完成答辩。五、参考文献1 谭浩强C程序设计北京:清华大学出版社,20052 刘成等C语言程序设计实验指导与习题集北京:中国铁道出版社,2006六、源程序#include v

15、oid Game(int price);void main() int function,price; char enter; printf( Welcome! Lets go! n); printf( 1 -Input the price n); printf( 2 -PC begin guessing n); printf( 0 -exit to window n); printf(please have a choice!n);while(1) scanf(%d,&function); switch(function) case 2: if(function=2) printf(Plea

16、se input the price first(1200):); scanf(%d,&price); while(price200) printf(Oh,no!Please input the price between 1 and 200:(OK?); scanf(%d,&price); printf(Do you want the computer to guess the price?(Y to allow):); scanf(%c,&enter); if(enter=Y|enter=y) Game(price);break; case 1: printf(Please input t

17、he basketball price(1200):); scanf(%d,&price); while(price200) printf(Please input the price between 1 and 200:); scanf(%d,&price); printf(Do you want the computer to guess the basketball price?(Y to allow):); scanf(%c,&enter); if(enter=Y|enter=y) Game(price);break; default : printf(You have input a

18、 wrong fuction number!); printf(n/*Please guess the basketball price */n); printf(2-PC begin guessingn); printf(1-Input the pricen); printf(nWelcome to nPlease input the function number:); scanf(%d,&function);break; case 0:exit(0); void Game(int price) int guessnum=1,low=1,high=200,mark=1,sign=0; char judge; printf(nTh

温馨提示

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

评论

0/150

提交评论