该楼层疑似违规已被系统折叠隐藏此楼查看此楼
改写猜价格游戏的程序(见下),实现对这个游戏的一些管理功能,可以根据菜单对商品进行添加、删除、查找、浏览等操作,根据模块间数据传递的方式分析各个模块的函数原型及调用关系,并完成程序编写。商品价格要求在限定范围内取随机值。
如有兴趣可以完成更多功能(不属于作业要求内容),如:
使用链表存储商品(需要对链表进行操作的一些工具函数);
商品类型中加入商品个数信息,增加统计功能;
对用户进行分类(如管理员看到的界面和普通用户看到的界面应该是不同的);
增加文件存储功能等等。
#include
#include
#include
#include
#defineMAXNUMOFGOODS5
#defineMAXNUMOFGUESS6
structGOODSTYPE
{
charname[20];
intprice;
intlb;
intub;};
voidBegin(structGOODSTYPEgoods[],intsize,structGOODSTYPE*pchoice);
voidPlay(structGOODSTYPE*pgoods);
intCheckNewGame();
voidListGoods(structGOODSTYPEgoods[],intsize);
structGOODSTYPESelect(structGOODSTYPEgoods[],intsize);
intGuessPrice(structGOODSTYPE*pgoods);
intJudge(structGOODSTYPE*pgoods,intprice);
intmain()
{structGOODSTYPEgoods[MAXNUMOFGOODS]={{"Book",61,20,120},
{"Radio",177,100,300},
{"ElectricCooker",395,200,500},
{"MicrowaveOven",988,500,1500},
{"Television",2199,1000,3000}};
structGOODSTYPEchoice;clrscr();
while(1)
{
Begin(goods,MAXNUMOFGOODS,&choice);
Play(&choice);
if(!CheckNewGame())
{
printf("Thankyou!\nBye!\n");
break;
}
}
return0;
}
voidBegin(structGOODSTYPEgoods[],intsize,structGOODSTYPE*pchoice)
{
/*列出全部商品*/
ListGoods(goods,size);
*pchoice=Select(goods,size);
}
voidPlay(structGOODSTYPE*pgoods)
{
inti,price,judge;
for(i=0;i
{
price=GuessPrice(pgoods);
judge=Judge(pgoods,price);
switch(judge)
{
case-1:
printf("Low.%dopportunitiesleft.\n",MAXNUMOFGUESS-i-1);
break;
case0:
printf("Congratulations!Youwinthe%s!\n",pgoods->name);
break;
case1:
printf("High.%dopportunitiesleft.\n",MAXNUMOFGUESS-i-1);
break;
}
if(judge==0)break;
}
if(price!=pgoods->price)
printf("\n+++Youlose!+++\n+++Thepriceis%d+++\n",pgoods->price);
}
intCheckNewGame()
{
staticcharreplay[2]="N";
printf("\nDoyouwanttoplayanothergame(Y|N)?");
gets(replay);
if(toupper(replay[0])=='Y')
return1;
else
return0;
}
voidListGoods(structGOODSTYPEgoods[],intsize)
{
inti;
printf("++++++++++++++++Welcome!++++++++++++++\n\n");
printf("Chooseonefromthelist.You'llgetitifyoucan\n");
printf("tellthepricewithin%dtimes.\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");
}
structGOODSTYPESelect(structGOODSTYPEgoods[],intsize)
{
intsel;
printf("Inputyourchoice(%d-%d),Otherstoquitthegame:",
1,size);
scanf("%d",&sel);
if(selsize)
exit(0);
return(goods[sel-1]);
}
intGuessPrice(structGOODSTYPE*pgoods)
{
intprice;
while(1)
{
printf("Inputyourpricebetween%dand%d:",
pgoods->lb,pgoods->ub);
scanf("%d",&price);
getchar();
if((price>=pgoods->lb)&&(price<=pgoods->ub))
break;
else
printf("Outofrange,pleaseinputapricebetween%dand%d.\n",
pgoods->lb,pgoods->ub);
}
returnprice;
}
intJudge(structGOODSTYPE*pgoods,intprice)
{
if(price==pgoods->price)
return0;
elseif(priceprice)
return-1;
else
return1;
}