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

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

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

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

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

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

增加文件存储功能等等。

#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语言大作业:编写菜单控制猜商品价格程序相关推荐

  1. c语言大作业菜单管理,C语言大作业:编写菜单控制猜商品价格程序

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 改写猜价格游戏的程序(见下),实现对这个游戏的一些管理功能,可以根据菜单对商品进行添加.删除.查找.浏览等操作,根据模块间数据传递的方式分析各个模块的函数 ...

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

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 改写猜价格游戏的程序(见下),实现对这个游戏的一些管理功能,可以根据菜单对商品进行添加.删除.查找.浏览等操作,根据模块间数据传递的方式分析各个模块的函数 ...

  3. 0039c语言作业答案2020,西南大学2019年网络与继续教育[0039]《C语言程序设计》大作业试题(资料).doc...

    西南大学2019年网络与继续教育[0039]<C语言程序设计>大作业试题(资料).doc 文档编号:764150 文档页数:4 上传时间: 2019-10-12 文档级别: 文档类型:do ...

  4. 微信小程序云开发体会——总结软件工程导论大作业

    微信小程序云开发体会--总结软件工程导论大作业 前言 第一次接触 具体难题 好用的技术 开发完之后的体会 无法不热爱更多 前言 可能大家完成这次作业都会选择比较擅长的领域--网页前后端.这的确是比较稳 ...

  5. 已知有一个名为MyServlet的程序,程序可向浏览器输出“Hello MyServlet”。请编写一个用于拦截MyServlet程序的MyFilter拦截器。

    1:题目详情: 已知有一个名为MyServlet的程序,程序可向浏览器输出"Hello MyServlet".请编写一个用于拦截MyServlet程序的MyFilter拦截器. 要 ...

  6. c语言编写程序判断图的连通,图论期末大作业编程题(如何判断一个4连通4正则图为无爪、无K4图)...

    博士期间估计这可能是唯一一个要编程的作业,搞了半天弄出这个东西,放这里为以后用到的时候查找方便. 说来也是可笑,读博士期间发现大家对上课也都没什么兴趣,老师也是那么回事,都说博士期间学的课程是要有助于 ...

  7. c语言程序设计创新大作业,C语言程序设计大作业报告.pdf

    C语言程序设计大作业报告 课程设计(大作业)报告 课程名称: C 语言程序设计 设计题目: 实验设备管理系统 院 系: 信息技术学院 班 级: 2015级物联网工程 1班 设 计 者: 何盛 高陶 王 ...

  8. c语言程序设计0039大作业答案,2019西南大学0039C语言程序设计机考大作业答案.doc...

    - PAGE 1 - 西南大学网络与继续教育学院课程考试试题卷 类别: 网教 2019年 6月 课程名称[编号]: C语言程序设计 [0039] A卷 大作业 满分:100 分 一.大作业题目 1.简 ...

  9. 学生成绩管理系统(C语言实现)大作业不会?快来看看吧!

    目录 一,简述 二,注意 三,代码 1.结构体及函数声明 menu函数 in函数(录入) search_函数(查询) del函数(删除) modify函数(修改) insert函数(插入) show函 ...

最新文章

  1. heartbeat原理介绍
  2. hibernate二级缓存理解
  3. C语言编译全过程剖析
  4. 【ABAP增强】基于BADI的增强
  5. js array 的理解
  6. Spring Cloud 微服务架构
  7. Ideal配置Struts项目提示Cannot resolve symbol 'xx.jsp',以及没有找到Namespace为/的指定Action的解决
  8. git 添加公钥的命令
  9. Java提高篇——equals()方法和“==”运算符
  10. sql 2005中全文索引的使用
  11. Mac Brew Uninstall MySql
  12. CSS3实现的立体button
  13. Markdown 模板
  14. 代理模式———动态代理
  15. 求101到150之间的质数
  16. 图像处理​​​​​​​--十大经典算法
  17. javascript中mouseover和mouseout事件详解
  18. 数值分析笔记(一):方程求根
  19. 华为云对象存储浏览器直接打开,而不是下载
  20. sql 求和并且将求和条件作为查询条件

热门文章

  1. iOS Appstore 版本更新
  2. 让您的Xcode键字如飞
  3. I.MX6 Linux Qt 启动流程跟踪
  4. 11个实用的CSS学习工具
  5. 排列与组合的Java递归实现 (参考)
  6. Linux面试题集锦,测测你的水平(答案)四
  7. VC Ws2_32.lib
  8. php堆是什么,PHP 堆与堆排序的详解
  9. mysql 数据库名称限制_mysql 数据库名称限制
  10. Supervised Descent Method and its Applications to Face Alignment