资源描述
沈 阳 航 空 工 业 学 院
课程设计
学 号 __________
班 级 __________
姓 名 __________
指导教师 __________
年 月 日
沈阳航空工业学院
课程设计任务书
机械与汽车学院 机械设计制造及其自动化专业 5406108 班 学号 200504061291
一、课程设计题目:用折半查找法猜篮球的价格
二、课程设计工作自2007年7月9日起至2007年7月14日止
三、课程设计内容:
运用所学的C语言知识,编制和调试程序,具有如下功能:
用户从键盘输入篮球的价格(假设价格在1~200元之间,并且为整数),让计算器从1元开始用折半法猜测篮球的价格。若计算器的猜测价格偏高,用户输入“高”;若计算器的猜测价格偏低,用户就输入“低”;若计算器猜对了,则显示“正确”。
四、课程设计要求:
程序质量:
l 贯彻结构化程序设计思想。
l 用户界面友好,功能明确,操作方便;可以加以其它功能或修饰。
l 用户界面中的菜单至少应包括“开始”、“退出”2项。
l 代码应适当缩进,并给出必要的注释,以增强程序的可读性。
课程设计说明书:
课程结束后,上交课程设计说明书(打印稿和电子稿),其内容如下:
l 封面
l 课程设计任务书
l 目录
l 需求分析(分析题目的要求)
l 程序流程图(总体流程图和主要功能模块流程图)
l 核心技术的实现说明及相应程序段
l 个人总结
l 参考资料
l 源程序及适当的注释
指导教师:____________________
学生签名:____________________
目录
一、需求分析 1
二、程序流程图 2
三、核心技术的实现说明及相应程序段 4
四、个人总结 8
五、参考文献 8
六、源程序 9
一、需求分析
经过对程序设计题目的分析可知,整个程序的设计实现大致分为两个模块,其中每一个模块对应一个函数,他们的功能分别是:主函数定义函数价格范围调用子函数(main),子函数折半查找法猜篮球价格(game)。
1、主函数 主要实现程序最初运行时定义程序的总体框架,是程序的总体思想,定义函数“game”,“ mian”,调用子函数“game”。同时需要输入“function”和“price”两个变量和程序的输出界面以及程序中对错误的分析和改正的方法;
2、子函数 实现的功能是用折半查找法猜篮球价格;以及输出功能,实现主函数的功能,执行主函数的命令。子函数定义了篮球的最低和最高价格,如果正确则用“mark”代替,如果错误则用“sign”代替。
除上面介绍的功能之外,程序还具有退出功能,可以在程序运行正确或者需要退出的时候可以输入0退出。
二、程序流程图
1、程序总体结构图
2、具体功能框图
(1)主函数main
当(1)
Function= =1
真 假
输入1<price<200 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=1
low=guessnum
guessnum=
(high/low)/2
Judge==‘L’||judge==‘l’ 否
是 (judge==‘0’)
mark=1
high=guessnum
guessnum=
(high+low)/2
Sign=1
猜出篮球价格
图(2)
三、核心技术的实现说明及相应程序段
本程序主要由一个子函数和一个主函数组成,其中主函数以菜单的形式调用其他函数来实现要求的所有功能。子函数是实现折半查找法来猜篮球价格的。具体如下。
1、子函数
首先要定义变量,最低价格为1,最高价格为200.正确则输出mark,错误则输出sign.然后计算机开始猜篮球的价格。输入0退出。如果如果正确,计算机输出篮球的价格,如果不正确,则计算机提示,如果想要价格好一点的话则输入“H”如果要低一点的话则输出“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;
printf("\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\' or\'l\'");
printf("\nYou want:(H:Let's higher/L:Let's lower):");
scanf("%c",&judge);
scanf("%c",&judge);
if(judge=='H'||judge=='h')
{
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 interupted!");
}
}
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 price?(Y to allow).当计算机猜出篮球价格的时候,程序结束。当没有正确的按照计算机的指示操作的时候,计算机自动提示,你输入了错误的数字。直到你输入正确的猜测数字。然后输入0退出。
具体的程序如下:
#include <stdio.h>
void Game(int price);
void main()
{
int function,price;
char enter;
printf(" Welcome! Let's 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("Please input the price first(1~200):");
scanf("%d",&price);
while(price<1||price>200)
{
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);
scanf("%c",&enter);
if(enter=='Y'||enter=='y')
Game(price);break;
case 1:
printf("Please input the basketball price(1~200):");
scanf("%d",&price);
while(price<1||price>200)
{
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 wrong fuction number!");
printf("\n//***********Please guess the basketball price ************//\n");
printf("2-PC begin guessing\n");
printf("1-Input the price\n");
printf("\nWelcome to <Guess Basketball price>\nPlease input the function number:");
scanf("%d",&function);break;
case 0:exit(0);
}
}
}
} }
四、个人总结
折半查找法是程序的主要方法,通过对方法的认识制定方案。程序分为主函数和子函数两部分,主函数是程序的整体框架,子程序执行折半查找法的核心部分。
通过此次课程设计我对C语言的知识有了更加深刻的了解。在不断的对程序调试过程中,对以前学过的知识有了更深刻的了解加深了对知识的理解。经过不懈的努力顺利的完成答辩。
五、参考文献
1 谭浩强.C程序设计.北京:清华大学出版社,2005
2 刘成等.C语言程序设计实验指导与习题集.北京:中国铁道出版社,2006
六、源程序
#include <stdio.h>
void Game(int price);
void main()
{
int function,price;
char enter;
printf(" Welcome! Let's 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("Please input the price first(1~200):");
scanf("%d",&price);
while(price<1||price>200)
{
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 the basketball price(1~200):");
scanf("%d",&price);
while(price<1||price>200)
{
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 wrong fuction number!");
printf("\n//***********Please guess the basketball price ************//\n");
printf("2-PC begin guessing\n");
printf("1-Input the price\n");
printf("\nWelcome to <Guess Basketball price>\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("\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\' or\'l\'");
printf("\nYou want:(H:Let's higher/L:Let's lower):");
scanf("%c",&judge);
scanf("%c",&judge);
if(judge=='H'||judge=='h')
{
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 interupted!");
}
}
展开阅读全文