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