本文实例为大家分享了C语言实现自动售货机的具体代码,供大家参考,具体内容如下
如图所示的简易自动售货机,物品架1、2上共有10样商品,按顺序进行编号分别为1-10,标有价格与名称,一个编号对应一个可操作按钮,供选择商品使用。如果物架上的商品被用户买走,储物柜中会自动取出商品送到物架上,保证物品架上一定会有商品。用户可以一次投入较多钱币,并可以选择多样商品,售货机可以一次性将商品输出并找零钱。
用户购买商品的操作方法:
(1)从“钱币入口”放入钱币,依次放入多个硬币或纸币。钱币可支持1元(纸币、硬币)、2元(纸币)、5元(纸币)、10元(纸币),放入钱币时,控制器会先对钱币进行检验识别出币值,并统计币值总额,显示在控制器显示屏中,提示用户确认钱币放入完毕;
(2)用户确认钱币放入完毕,便可选择商品,只要用手指按对应商品外面的编号按钮即可。每选中一样商品,售货机控制器会判断钱币是否足够购买,如果钱币足够,自动根据编号将物品进行计数和计算所需钱币值,并提示余额。如果钱币不足,控制器则提示“Insufficient money”。用户可以取消购买,将会把所有放入钱币退回给用户。
输入格式:
先输入钱币值序列,以-1作为结束,再依次输入多个购买商品编号,以-1结束。
输出格式:
输出钱币总额与找回零钱,以及所购买商品名称及数量。
输入样例:
1 1 2 2 5 5 10 10 -1
1 2 3 5 1 6 9 10 -1
输出样例:
Total:36yuan,change:19yuan
Table-water:2;Table-water:1;Table-water:1;Milk:1;Beer:1;Oolong-Tea:1;Green-Tea:1;
解法一:
#include<stdio.h>
int main(void)
{
char a[10][20] = {"Table-water","Table-water","Table-water","Coca-Cola","Milk","Beer","Orange-Juice","Sprite","Oolong-Tea","Green-Tea"};
int b[11] = {0,0,0,0,0,0,0,0,0,0,0};
int c[50];
int i=1, k, sum = 0, money, SUM = 0, change, flag=0;
scanf("%d",&money);//输入币值并计算总币值
while((money!=-1)&&(money <= 10))
{
sum = sum + money;
scanf("%d",&money);
}
scanf("%d",&c[i]);
while(c[i]!=-1)//将选的货物编号存储到数组c中并计算找零
{
switch(c[i])
{
case 1: case 2: case 3: SUM = SUM + 1;break;
case 4: case 5: SUM = SUM + 2;break;
case 6: case 7: case 8: SUM = SUM + 3;break;
case 9: case 10: SUM = SUM + 4;break;
default:break;
}
if(SUM>sum)
{
printf("Insufficient money");
flag = 1;
break;
}
i++;
scanf("%d",&c[i]);
}
change = sum-SUM;
//用数组b统计各种商品数量
i = 1;
while(c[i]!=-1)
{
switch(c[i])
{
case 1: b[1]++;break;
case 2: b[2]++;break;
case 3: b[3]++;break;
case 4: b[4]++;break;
case 5: b[5]++;break;
case 6: b[6]++;break;
case 7: b[7]++;break;
case 8: b[8]++;break;
case 9: b[9]++;break;
case 10: b[10]++;break;
default:break;
}
i++;
}
//输出结果
if(flag==0)
{
printf("Total:%dyuan,change:%dyuan\n",sum, change);
for(i=1; i<=10; i++)
{
if(b[i]==0)
continue;
else
{
printf("%s:%d;",a[i-1],b[i]);
}
}
}
return 0;
}
解法二:
#include"stdio.h"
struct goods
{
int num;
char name[20];
int price;
int amount;
};
int main()
{
struct goods good[10]=
{
{1,"Table-water",1,0},
{2,"Table-water",1,0},
{3,"Table-water",1,0},
{4,"Coca-Cola",2,0},
{5,"Milk",2,0},
{6,"Beer",3,0},
{7,"Orange-Juice",3,0},
{8,"Sprite",3,0},
{9,"Oolong-Tea",4,0},
{10,"Green-Tea",4,0}
};
int sum=0,num,change,total=0,money=0,i=0;
while(1)
{
scanf("%d",&money);
if(money==-1)
{
break;
}
sum=sum+money;
}
while(1)
{
scanf("%d",&num);
if(num==-1)
{
break;
}
switch(num)
{
case 1:
{
total=total+good[0].price;
good[0].amount=good[0].amount+1;
break;
}
case 2:
{
total=total+good[1].price;
good[1].amount=good[1].amount+1;
break;
}
case 3:
{
total=total+good[2].price;
good[2].amount=good[2].amount+1;
break;
}
case 4:
{
total=total+good[3].price;
good[3].amount=good[3].amount+1;
break;
}
case 5:
{
total=total+good[4].price;
good[4].amount=good[4].amount+1;
break;
}
case 6:
{
total=total+good[5].price;
good[5].amount=good[5].amount+1;
break;
}
case 7:
{
total=total+good[6].price;
good[6].amount=good[6].amount+1;
break;
}
case 8:
{
total=total+good[7].price;
good[7].amount=good[7].amount+1;
break;
}
case 9:
{
total=total+good[8].price;
good[8].amount=good[8].amount+1;
break;
}
case 10:
{
total=total+good[9].price;
good[9].amount=good[9].amount+1;
break;
}
}
}
if(total>sum)
{
printf("Insufficient money");
}
else
{
change=sum-total;
printf("Total:%dyuan,change:%dyuan\n",sum,change);
for(i=0;i<10;i++)
{
if(good[i].amount!=0)
{
printf("%s:%d;",good[i].name,good[i].amount);
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。