返回列表 發帖

購物系統 (三)

本帖最後由 tonyh 於 2015-3-21 11:41 編輯

新增 "購物清單" 功能, 使結帳時能不只計算出價錢, 還能列出細目.
參考執行畫面如下:

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int p,q,sum=0;
  7.     string name[7]={"遙控汽車","飛機模型","足球    ",
  8.                     "拼圖    ","玩具槍  ","可愛玩偶","籃球    "};
  9.     int price[7]={450,550,325,200,660,150,380};
  10.     int qty[7]={0,0,0,0,0,0,0};
  11.     cout<<"☆★☆智能玩具店☆★☆"<<endl<<endl;
  12.     cout<<"[商品價目表]"<<endl;
  13.     for(int i=0; i<7; i++)
  14.     {
  15.         cout<<"("<<i+1<<")"<<name[i]<<"\t"<<price[i]<<"元"<<endl;
  16.     }
  17.     cout<<"(8)結帳"<<endl<<endl;
  18.     re:
  19.     cout<<"請輸入商品代碼: ";
  20.     cin>>p;
  21.     if(p==8)
  22.         goto checkout;
  23.     else if(p>=1 && p<=7)
  24.     {
  25.         cout<<"數量: ";
  26.         cin>>q;
  27.         if(q<=0)
  28.         {
  29.             cout<<"輸入錯誤!"<<endl;
  30.             goto re;        
  31.         }
  32.         sum+=price[p-1]*q;
  33.         qty[p-1]+=q;         
  34.     }
  35.     else
  36.     {
  37.         cout<<"輸入錯誤!"<<endl;   
  38.     }
  39.     goto re;
  40.     checkout:
  41.     cout<<endl<<"[購物清單]"<<endl;         
  42.     cout<<"-----------------------------"<<endl;
  43.     for(int i=0; i<7; i++)
  44.     {
  45.         if(qty[i]!=0)
  46.             cout<<name[i]<<"\t"<<price[i]<<"元 * "<<qty[i]<<"個"<<endl;  
  47.     }
  48.     cout<<"-----------------------------"<<endl;         
  49.     cout<<"總共"<<sum<<"元!"<<endl<<endl;
  50.     system("pause");
  51.     return 0;   
  52. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

返回列表