本帖最後由 陳品肇 於 2019-3-9 14:51 編輯
新增 "1-正確無誤 2-重新選購" 的選單,
讓使用者在準備結帳時還能反悔, 譬如錢帶不夠.
確定結帳後, 銜接自動找零系統.
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- restart:
- system("cls"); //畫面清空
- int p,q,sum=0,no; //p放商品代碼 q放商品數量 sum 金額加總的值 no確定結帳
- cout<<"☆★☆智能玩具店☆★☆"<<endl;
- cout<<endl;
- cout<<"[商品價目表]"<<endl;
- string a[7]= {"遙控器車",
- "飛機模型",
- "足球 ",
- "拼圖 ",
- "玩具槍 ",
- "可愛玩偶",
- "籃球 "};
- int price[7]={450,550,325,200,660,150,380};
- int tmpQty[7]={0,0,0,0,0,0,0}; // 暫存購買商品的個數
-
-
- for(int i=0;i<7;i++) //列出商品清單
- {
- cout<<"("<<i+1<<")"<<a[i]<<"\t"<<price[i]<<"元"<<endl;
- }
- cout<<"(8)結帳"<<endl;
- cout<<endl;
-
- re:
- cout<<"請輸入商品代碼:";
- cin>>p;
- if(p>=1 && p<=7) //判斷選擇正確的商品 0~6
- {
- cout<<"數量:";
- cin>>q; //輸入數量
- if(q>0) //數量大於0
- {
- sum = sum + price[p-1]*q; //單價*購買數量
- tmpQty[p-1] = tmpQty[p-1] + q; //計算各別商品總數量
- goto re;
- }else
- {
- cout<<"您輸入的數量有誤!!"<<endl;
- goto re;
- }
-
- }else if(p==8) //結帳
- {
- goto checkout;
- }else
- {
- cout<<"您輸入的代碼有誤"<<endl;
- goto re;
- }
-
- checkout:
- cout<<"[購物清單]"<<endl;
- cout<<"-----------------------------"<<endl;
- for(int i=0;i<7;i++)
- {
- if(tmpQty[i] != 0 ) //過濾清單是0 個的商品 != 不等於 驚嘆號指的是反向結果
- {
- cout<<a[i]<<"\t"<<price[i]<<"元 * "<<tmpQty[i]<<" 個"<<endl;
- }
- }
-
- cout<<"-----------------------------"<<endl<<endl;
- cout<<"總共"<<sum<<"元"<<endl<<endl;
-
- chose:
- cout<<"1-正確無誤 2-重新選擇: ";
- cin>>no;
-
- if(no == 1)
- {
- goto pay; //支付金額
- }else if(no == 2)
- {
- goto restart; //重新選擇商品
- }else
- {
- cout<<"您輸入的編號有錯!!"<<endl; //選擇錯誤
- goto chose;
- }
-
- int payMoney;
- pay:
- cout<<"請付帳:";
- cin>>payMoney;
- if( payMoney < sum) //支付金額小於總額
- {
- cout<<"您付的錢不夠,還差"<< sum- payMoney<<"元"<<endl;
- goto pay;
- }else
- {
- int money = payMoney-sum; //找錢的結果
- cout<<"找您"<<money<<"元!"<<endl;
-
- if(money >=500 )
- {
- cout<<"五佰元鈔票"<<money/500<<"張"<<endl;
- money = money%500; //換完500後的剩餘金額
- }
- if(money >=100 )
- {
- cout<<"一佰元鈔票"<<money/100<<"張"<<endl;
- money = money%100; //換完100後的剩餘金額
- }
- if(money >=50 )
- {
- cout<<"五十元硬幣"<<money/50<<"枚"<<endl;
- money = money%50; //換完500後的剩餘金額
- }
- if(money >=10 )
- {
- cout<<"十元硬幣"<<money/10<<"枚"<<endl;
- money = money%10; //換完500後的剩餘金額
- }
- if(money > 0 )
- {
- cout<<"一元硬幣"<<money<<"枚"<<endl;
- }
- }
-
-
- system("pause");
- return 0;
- }
複製代碼 |