C語言實現自動售貨機
本文實例為大傢分享瞭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); } } } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。