经典重庆

标题: [原创]C语言_学习题套_第九套 [打印本页]

作者: 文木四水    时间: 2007-2-1 13:22
标题: [原创]C语言_学习题套_第九套
<p>题目: 商品价格统计</p><p>一 语言与环境</p><p>&nbsp; A.实现语言<br/>C<br/>&nbsp; B.环境要求<br/>1 .Turbo C 2.0或者以上版本开发环境<br/>二 要求</p><p>&nbsp; 请用C语言编写一个程序,完成如下功能;定义一个结构数组,输入4种商品的名称,单价,数量,要求计算并逐个输出每种商品的总价,最后输出单价最高的商品价格。<br/>三 实现步骤</p><p>&nbsp; 1. 定义一个结构体,其中包括商品名称,单价,数量和总价四个成员。<br/>&nbsp; 2. 在程序中,使用循环结构控制连续输入(第一个循环)。<br/>2.1 在循环结构中,显示提示信息,要求用户输入商品的名称,单价和数量。<br/>2.2 读取输入的数据<br/>2.3 计算商品总价<br/>&nbsp; 3 .在程序中,使用循环结构控制连续输出(第二个循环)。<br/>3.1 循环输出每种商品的总价<br/>3.2 判断并输出单价最高的商品价格<br/>&nbsp; 4 .运行结果如下图:<br/>&nbsp;<br/>&nbsp;&nbsp; Please enter the name,price and amount of the NO.1 commodity: aaa 1 1 <br/>Please enter the name,price and amount of the NO.2 commodity: bbb 2 2<br/>&nbsp;&nbsp; Please enter the name,price and amount of the NO.3 commodity: ccc 3 3<br/>&nbsp;&nbsp; Please enter the name,price and amount of the NO.4 commodity: ddd 4 4<br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp; The total prices of aaa is :1<br/>The total prices of bbb is :4<br/>The total prices of ccc is :9<br/>The total prices of ddd is :16<br/>The highest price is :4</p><p>D:\turboc&gt;<br/>&nbsp; </p><p>四 注意事项 </p><p>&nbsp;&nbsp; A .请注意在程序中进行异常处理;</p><p>&nbsp;&nbsp; B .请注意代码的书写,命名符合规范;</p><p><br/>第9套<br/>&nbsp;&nbsp; <br/> <br/></p>
作者: 文木四水    时间: 2007-2-1 13:23
<p>解:</p><p>//第九套&nbsp; <br/>#include&lt;stdio.h&gt;<br/>typedef struct<br/>{<br/>&nbsp;char name[20];//名称<br/>&nbsp;int up;//单价<br/>&nbsp;int how;//数量<br/>&nbsp;int total;//总价<br/>}goods;<br/>void main()<br/>{<br/>&nbsp;goods god[4];<br/>&nbsp;int temp[4];<br/>&nbsp;int i,n,j=0;<br/>&nbsp;for(i=0;i&lt;4;i++)<br/>&nbsp;{<br/>&nbsp;&nbsp;printf("请输入第%d件商品的名称,单价,数量(以空格分开)",i+1);<br/>&nbsp;&nbsp;scanf("%s%d%d",&amp;god.name ,&amp;god.up,&amp;god.how );<br/>&nbsp;&nbsp;god.total=god.up*god.how;<br/>&nbsp;}<br/>&nbsp;printf("\n\n");<br/>&nbsp;for(n=0;n&lt;4;n++)<br/>&nbsp;{<br/>&nbsp;&nbsp;printf("第%d件商品的总价为:%d\n",n+1,god[n].total);<br/>&nbsp;&nbsp;temp[n]=god[n].up;<br/>&nbsp;}<br/>&nbsp;for(i=0;i&lt;4;i++)<br/>&nbsp;{<br/>&nbsp;&nbsp;for(n=0;n&lt;4-i;n++)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;if(temp[n]&lt;temp[n+1])<br/>&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;j=temp[n];<br/>&nbsp;&nbsp;&nbsp;&nbsp;temp[n]=temp[n+1];<br/>&nbsp;&nbsp;&nbsp;&nbsp;temp[n+1]=j;<br/>&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;}<br/>&nbsp;}<br/>&nbsp;printf("\n几件商品中单价最高的是:%d\n",temp[0]);<br/>&nbsp;getch();<br/>}</p>




欢迎光临 经典重庆 (http://bbs.jdcq.net/) Powered by Discuz! X3.1