返回列表 發帖

[10/21 作業1] 購物系統(四)

本帖最後由 李泳霖 於 2023-10-28 14:51 編輯

新增 "1-正確無誤 2-重新選購" 的選單,
讓使用者在準備結帳時還能反悔,譬如錢帶不夠。
確定結帳後,銜接自動找零系統。

p.s.參考




  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     re:
  7.     system("cls");
  8.     int p,q,sum=0,o,pay;
  9.     string name[]={"遙控汽車","飛機模型","足球    ","拼圖    ","玩具槍  ","可愛玩偶","籃球    "};
  10.     int price[]={450,550,325,200,660,150,380};
  11.     int qty[]={0,0,0,0,0,0,0};
  12.     cout<<"☆★☆智能玩具店☆★☆"<<endl<<endl;
  13.     cout<<"[商品價目表]"<<endl;
  14.     for(int i=0; i<7; i++)
  15.          cout<<"("<<i+1<<")"<<name[i]<<"\t"<<price[i]<<"元"<<endl;
  16.     cout<<"(8)結帳"<<endl<<endl;
  17.     re2:
  18.     cout<<"請輸入商品代碼: ";
  19.     cin>>p;
  20.     if(p==8)
  21.     {
  22.         goto checkout;
  23.     }else if(p>=1 && p<=7)
  24.     {
  25.         cout<<"數量: ";
  26.         cin>>q;
  27.         sum+=price[p-1]*q;
  28.         qty[p-1]+=q;
  29.         goto re2;  
  30.     }else
  31.     {
  32.         goto re;     
  33.     }
  34.     checkout:
  35.     cout<<endl<<"[購物清單]"<<endl;
  36.     cout<<"-----------------------------"<<endl;
  37.     for(int i=0; i<7; i++)
  38.     {
  39.         if(qty[i]!=0)
  40.             cout<<name[i]<<"\t"<<price[i]<<"元 * "<<qty[i]<<"個"<<endl;        
  41.     }
  42.     cout<<"-----------------------------"<<endl;
  43.     cout<<"總共"<<sum<<"元!"<<endl<<endl;     
  44.    
  45.     cout<<"1-正確無誤 2-重新選購 ";
  46.     cin>>o;
  47.    
  48.     if(o==1)
  49.     {
  50.         re3:
  51.         cout<<"請付帳:";
  52.         cin>>pay;
  53.         if(pay<sum)
  54.         {
  55.             cout<<"您付的錢不夠,還差"<<-(pay-sum)<<"元"<<endl<<endl;
  56.             goto re3;
  57.         }   
  58.         else if(pay==sum)
  59.         {
  60.             cout<<"剛剛好!無需找錢"<<endl<<endl;   
  61.         }  
  62.         else
  63.         {
  64.             int m=pay-sum;
  65.             cout<<"找你"<<m<<"元!"<<endl;
  66.             /*
  67.             m=686
  68.             */
  69.             if(m>=500)
  70.             {
  71.                 cout<<"五百元鈔票"<<m/500<<"張"<<endl;   
  72.                 //686/500=1....186
  73.                 m=m%500;      
  74.             }   
  75.             if(m>=100)
  76.             {
  77.                 cout<<"一百元鈔票"<<m/100<<"張"<<endl;   
  78.                 //686/500=1....186
  79.                 m=m%100;      
  80.             }
  81.             if(m>=50)
  82.             {
  83.                 cout<<"五十元硬幣"<<m/50<<"枚"<<endl;   
  84.                 //686/500=1....186
  85.                 m=m%50;      
  86.             }
  87.             if(m>=10)
  88.             {
  89.                 cout<<"十元硬幣"<<m/10<<"枚"<<endl;   
  90.                 //686/500=1....186
  91.                 m=m%10;      
  92.             }   
  93.             if(m>=5)
  94.             {
  95.                 cout<<"五元硬幣"<<m/5<<"枚"<<endl;   
  96.                 //686/500=1....186
  97.                 m=m%5;      
  98.             }
  99.             if(m>=1)
  100.             {
  101.                 cout<<"一元硬幣"<<m<<"枚"<<endl;      
  102.             }                                          
  103.         }  
  104.     }
  105.     else if(o==2)
  106.        goto re;
  107.     else
  108.        cout<<"輸入錯誤!"<<endl;
  109.       
  110.     system("pause");
  111.     goto re;   
  112.     return 0;
  113. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊
istak.teach2@gmail.com

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見
istak.teach2@gmail.com

TOP

返回列表