用c语言编写一个猜价格程序,C语言大作业:编写菜单控制猜商品价格程序

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

改写猜价格游戏的程序(见下),实现对这个游戏的一些管理功能,可以根据菜单对商品进行添加、删除、查找、浏览等操作,根据模块间数据传递的方式分析各个模块的函数原型及调用关系,并完成程序编写。商品价格要求在限定范围内取随机值。

如有兴趣可以完成更多功能(不属于作业要求内容),如:

使用链表存储商品(需要对链表进行操作的一些工具函数);

商品类型中加入商品个数信息,增加统计功能;

对用户进行分类(如管理员看到的界面和普通用户看到的界面应该是不同的);

增加文件存储功能等等。

#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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值