c语言大作业菜单,C语言大作业:编写菜单控制猜商品价格程序
該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓
改寫猜價格游戲的程序(見下),實現(xiàn)對這個游戲的一些管理功能,可以根據(jù)菜單對商品進行添加、刪除、查找、瀏覽等操作,根據(jù)模塊間數(shù)據(jù)傳遞的方式分析各個模塊的函數(shù)原型及調用關系,并完成程序編寫。商品價格要求在限定范圍內取隨機值。
如有興趣可以完成更多功能(不屬于作業(yè)要求內容),如:
使用鏈表存儲商品(需要對鏈表進行操作的一些工具函數(shù));
商品類型中加入商品個數(shù)信息,增加統(tǒng)計功能;
對用戶進行分類(如管理員看到的界面和普通用戶看到的界面應該是不同的);
增加文件存儲功能等等。
#include?
#include?
#include?
#include?
#define?MAXNUMOFGOODS?5
#define?MAXNUMOFGUESS?6
struct?GOODSTYPE
{
char?name[20];
int?price;
int?lb;
int?ub;?};
void?Begin(?struct?GOODSTYPE?goods[],?int?size,?struct?GOODSTYPE*?pchoice?);
void?Play(?struct?GOODSTYPE*?pgoods?);
int?CheckNewGame();
void?ListGoods(?struct?GOODSTYPE?goods[],?int?size?);
struct?GOODSTYPE?Select(?struct?GOODSTYPE?goods[],?int?size?);
int?GuessPrice(?struct?GOODSTYPE*?pgoods?);
int?Judge(?struct?GOODSTYPE*?pgoods,?int?price?);
int?main()
{??struct?GOODSTYPE?goods[?MAXNUMOFGOODS?]?=?{?{?"Book",?61,?20,?120?},
{?"Radio",?177,?100,?300?},
{?"Electric?Cooker",?395,?200,?500?},
{?"Microwave?Oven",?988,?500,?1500?},
{?"Television",?2199,?1000,?3000?}?};
struct?GOODSTYPE?choice;????clrscr();
while(?1?)
{
Begin(?goods,?MAXNUMOFGOODS,?&choice?);
Play(?&choice?);
if(?!CheckNewGame()?)
{
printf(?"Thank?you!\nBye!\n"?);
break;
}
}
return?0;
}
void?Begin(?struct?GOODSTYPE?goods[],?int?size,?struct?GOODSTYPE*?pchoice?)
{
/*?列出全部商品?*/
ListGoods(?goods,?size?);
*pchoice?=?Select(?goods,?size?);
}
void?Play(?struct?GOODSTYPE*?pgoods?)
{
int?i,?price,?judge;
for(?i?=?0;?i?
{
price?=?GuessPrice(?pgoods?);
judge?=?Judge(?pgoods,?price?);
switch(?judge?)
{
case?-1:
printf(?"Low.?%d?opportunities?left.\n",?MAXNUMOFGUESS?-?i?-?1?);
break;
case??0:
printf(?"Congratulations!?You?win?the?%s!\n",?pgoods->name?);
break;
case??1:
printf(?"High.?%d?opportunities?left.\n",?MAXNUMOFGUESS?-?i?-?1?);
break;
}
if(?judge?==?0?)?break;
}
if(?price?!=?pgoods->price?)
printf(?"\n+++?You?lose!?+++\n+++?The?price?is?%d?+++\n",?pgoods->price?);
}
int?CheckNewGame()
{
static?char?replay[2]?=?"N";
printf(?"\nDo?you?want?to?play?another?game?(?Y?|?N?)??"?);
gets(?replay?);
if(?toupper(?replay[0]?)?==?'Y'?)
return?1;
else
return?0;
}
void?ListGoods(?struct?GOODSTYPE?goods[],?int?size?)
{
int?i;
printf(?"++++++++++++++++????????Welcome!?????????++++++++++++++\n\n"?);
printf(?"Choose?one?from?the?list.?You'll?get?it?if?you?can\n"?);
printf(?"tell?the?price?within?%d?times.\n\n",?MAXNUMOFGUESS?);
printf(?"++++++++++++++++++++++++++++++++++++++++++++++++++++\n"?);
for(?i?=?0;?i?
printf(?"%d.?%-20s(price?:?%d-%d)\n",?i?+?1,?goods[i].name,
goods[i].lb,?goods[i].ub?);
printf(?"++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n"?);
}
struct?GOODSTYPE?Select(?struct?GOODSTYPE?goods[],?int?size?)
{
int?sel;
printf(?"Input?your?choice?(%d-%d),?Others?to?quit?the?game:?",
1,?size?);
scanf(?"%d",?&sel?);
if(?sel??size?)
exit(?0?);
return(?goods[?sel?-?1?]?);
}
int?GuessPrice(?struct?GOODSTYPE*?pgoods?)
{
int?price;
while(?1?)
{
printf(?"Input?your?price?between?%d?and?%d:?",
pgoods->lb,?pgoods->ub?);
scanf(?"%d",?&price?);
getchar();
if(?(?price?>=?pgoods->lb?)?&&?(?price?<=?pgoods->ub?)?)
break;
else
printf(?"Out?of?range,?please?input?a?price?between?%d?and?%d.\n",
pgoods->lb,?pgoods->ub?);
}
return?price;
}
int?Judge(?struct?GOODSTYPE*?pgoods,?int?price?)
{
if(?price?==?pgoods->price?)
return?0;
else?if(?price?price?)
return?-1;
else
return?1;
}
總結
以上是生活随笔為你收集整理的c语言大作业菜单,C语言大作业:编写菜单控制猜商品价格程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国好书推荐排行榜(2021年中国好书榜
- 下一篇: C语言中低位存放,C语言 大端小端存储解