声明:著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
附上c++版http://t.csdnimg.cn/BbDSL
https://blog.csdn.net/As_sBomb/article/details/105485940
实验三:自动售货机
题目:
图所示为简易自动售货机,物品架1、2上共有10样商品,按顺序进行编号分别为1~10,标有价格与名称,如下图:
1.售货机物品架上共有10样商品依次编号1-10,标有名称与价格。
2.售货机接受1/2/5/10面值的硬/纸币,用户可以一次性投入多个硬币并选择多种商品,售货机将一次性将商品输出并找零。
3.投入钱币和选择商品皆以-1作为结束,若输入商品编号超出范围或余额不足将自动结束购物,输出购买商品,钱币总额和找零。
输入不需要空格,10请输入’0’,-1请输入’-’
输入输出示例:
输入:
1 1 2 2 5 5 10 10 -1 /钱币序列/
1 2 3 5 1 6 9 10 -1 /商品编号/
输出:
Total:36yuan, change:19 yuan
Table-water1:2
Table-water2:1
Table-water3:1
Milk:1
Beer:1
Oolong-Tea:1
Green-Tea:1
#include "stdio.h" #include "stdlib.h"
int main()
{ printf(" 程序设计与实践 实验(三) 自动售货机\n");printf("\n");printf(" 自动售货机简介\n"); printf(" ============================= \n");printf("\n 1.售货机物品架上共有10样商品依次编号1~10,标有名称与价格。\n");printf(" 2.售货机可以一次性投入多个硬币并选择多种商品,售货机将一次性将商品输出并找零。\n");printf(" 3.用户输入钱钱币值序列,钱币值序列——对应商品序列号,分别以-1作为结束\n");printf("\n");printf("1.---Table.water-----1元\n");printf("2.---Table.water-----1元\n");printf("3.---Table.water-----1元\n");printf("4.----Coca.Cola------2元\n");printf("5.------Milk---------2元\n");printf("6.------Beer---------3元\n");printf("7.---Orange.Juice----3元\n");printf("8.-----Sprite--------3元\n");printf("9.----Ooiong.Tea-----4元\n");printf("10.----Green.Tea-----4元\n");printf("\n"); char *productname[10]={"Table-water", "Table-water", "Table-water", "Coca-Cola",
"Milk","Beer", "Orange-Juice", "Sprite", "Oolong-Tea", "Green-Tea"};/*定义指针数组,其中每个元数都是
一个字符指针,分别指向各商品名称*/ int price[2][10]={{1,1,1,2,2,3,3,3,4,4},{}}; /*各商品对应的价格*/ //static int countp[10]; //采用二维数组,这行没用了 int income,outcome,change,i,x;// *p=price,income=outcome=change=i=x=0;/*钱币序列求和*/ printf("Please enter a coin sequence:");do{scanf("%d",&x);if(x>0)income+=x;} while(x!=-1);//输入商品序列号printf("Please enter a goods sequence:");do{scanf("%d",&x);if(x>10) {printf("编号超出范围,结束购物。");printf("\nTotal:%dyuan,\nchange:%dyuan\n",income,income);exit(0);}if(x>0){price[1][x-1]++;}}while(x!=-1);//计算商品金额
for(i=0;i<10;i++) {
outcome+=price[0][i]*price[1][i];if(outcome>income){ printf("余额不足,结束购物。");printf("\nTotal:%dyuan,\nchange:%dyuan\n",income,income);exit(0); }
}
/*输出总金额,应找回的金额*/
change= income-outcome;
printf("\nTotal:%dyuan,\nchange:%dyuan\n",income,change);
//输出找零序列
printf("Your change sequence:");
while(change!=0){if (change>10){printf("10 ");change-=10;continue;}else if (change>=5 && change<10){printf("5 ");change-=5;continue;}else if (change>=2 && change<5){printf("2 ");change-=2;continue;}else {printf("1");change-=1;}
}
printf("\n");
/*输出物品名称与数量*/ for(i=0;i<10;i++) { if(price[1][i]>0) printf("%s:%d \n",productname[i],price[1][i]);}
}