#include"stdio.h"
#include"string.h"
#define
m 20
void main()
{
int i,b,e,d=0;
int c[m],number[m];
char flg;
double sum[m],total=0;
struct shangpin
{
int num;
char name[20];
double fee;
}shang[4]={{0},{1,"农夫山泉",1.03},{2,"今麦郎碗面",3.50},{3,"双汇火腿肠",12.60}};
printf("编号
");
printf("商品名称
");
printf("单价\n");
for(i=1;i<4;i++)
printf("%-6d%-12s%0.2f\n",shang[i].num,shang[i].name,shang[i].fee);
printf("\n");
do
{
printf("输入商品编号:");
scanf(" %d",&b);
c[d]=b;
printf("输入购买数量:");
scanf(" %d",&e);
number[d]=e;
d++;
printf("输入e停止购物,输入其他任意键继续购物:");
scanf(" %c",&flg);
printf("\n");
}while(flg!='e');
printf("\n");
printf("-------------欢迎光临光辉超市-------------\n");
printf("商品名称
单价
数量
小计\n");
for(i=0;i<d;i++)
{
if(c[i]==1)
{
sum[i]=shang[1].fee*number[i];
total+=sum[i];
printf("%-12s%-6.2f%-6d¥%0.2f\n",shang[1].name,shang[1].fee,number[i],sum[i]);
}
if(c[i]==2)
{
sum[i]=shang[2].fee*number[i];
total+=sum[i];
printf("%-12s%-6.2f%-6d¥%0.2f\n",shang[2].name,shang[2].fee,number[i],sum[i]);
}
if(c[i]==3)
{
sum[i]=shang[3].fee*number[i];
total+=sum[i];
printf("%-12s%-6.2f%-6d¥%0.2f\n",shang[3].name,shang[3].fee,number[i],sum[i]);
}
}
printf("\n");
printf("总计:¥%0.2f\n",total);
}
格式你自己调整吧,这个不太对。