本帖最後由 陳品肇 於 2018-11-3 17:18 編輯
改寫現有購物系統, 使其搭配促銷方案.
譬如: 消費滿萬送千元折價券, 滿五千享9折優惠... 等等.
自由發揮!
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- restart:
- system("cls"); //清空畫面
- cout<<"☆★☆智能玩具店☆★☆"<<endl<<endl;
- cout<<"[商品價目表]"<<endl;
-
- string a[]={"遙控汽車","飛機模型","足球 ","拼圖 ","玩具槍 ","可愛玩偶","籃球 "};
- int price[7]={450,550,325,200,660,
- 150,380 };
-
- int tmpQty[]={0,0,0,0,0,0,0}; //創一個數量暫存的陣列
- int p,q,sum=0,no; //p商品代碼 q商品數量 no 結帳確認
- for(int i=0;i<7;i++)
- {
- cout<<"("<<i+1<<")"<<a[i]<<"\t"<<price[i]<<"元"<<endl;
- }
- cout<<"(8)結帳"<<endl<<endl;
- cout<<"[優惠方案]滿五千享9折優惠,消費滿萬送千元折價。"<<endl;
- cout<<"-------------------------------------------"<<endl;
-
- re:
- cout<<"請輸入商品代碼: ";
- cin>> p;
-
- if(p>=1 && p<=7)
- {
- cout<<"數量: ";
- cin>> q;
- if(q>0) //數量>0
- {
- sum= sum+ price[p-1]*q; //計算總金額
- tmpQty[p-1] = q; // 選擇的商品數量存起來
- goto re;
- }else //數量<0
- {
- cout<<"您輸入的數量有誤!!"<<endl;
- goto re;
- }
- }else if(p==8)
- {
- goto checkout;
- }else
- {
- cout<<"您輸入的代碼有錯!!"<<endl;
- goto re;
- }
-
- checkout:
- cout<<endl;
- cout<<"[購物清單]"<<endl;
- cout<<"------------------------------"<<endl;
- for(int i =0;i<7;i++) //印出陣列裡的內容
- {
- if( tmpQty[i] !=0) //剛才數量不為0 ,才印出來 != 不等於 == 等於
- {
- cout<<a[i]<<"\t\t"<<price[i]<<"元 * "<<tmpQty[i]<<"個"<<endl;
- }
- }
-
- cout<<"-------------------------------"<<endl;
- cout<<"總金額: "<<sum<<"元"<<endl;
-
- if(sum >= 10000) // 判斷是否有金額折扣
- {
- sum = sum-1000; //滿萬送千
- cout<<"回饋後總金額: "<<sum<<"元"<<endl;
- }else if(sum<10000 && sum>=5000) //滿5000打9折
- {
- sum = sum*0.9;
- cout<<"回饋後總金額: "<<sum<<"元"<<endl;
- }else
- {
- sum =sum;
- }
-
-
- cout<<"1:正確無誤 2:重新選購 "<<endl;
- cin>>no;
- if(no==1)
- {
- goto pay; // 結帳區
- }else
- {
- goto restart; //重新開始
- }
-
- int payMoney;
- pay: //準備結帳
- cout<<"請付帳: ";
- cin>>payMoney;
- if(payMoney <sum) //支付錢小於買的
- {
- cout<<"您付的錢不夠,還差"<<sum-payMoney<<"元!"<<endl;
- goto pay;
- }else
- {
- int tmp;
- tmp=payMoney-sum; // 差額存起來,存在tmp變數
- cout<<"找您的錢為: "<<tmp<<"元"<<endl;
- if(tmp>=500)
- { cout<<"五百元鈔票"<<tmp/500<<"張"<<endl;
- tmp=tmp%500;
- }
-
- if(tmp>=100)
- { cout<<"一百元鈔票"<<tmp/100<<"張"<<endl;
- tmp=tmp%100;
- }
-
- if(tmp>=50)
- { cout<<"你五十元"<<tmp/50<<"枚"<<endl;
- tmp=tmp%50;
- }
- if(tmp>=10)
- { cout<<"你十元"<<tmp/10<<"枚"<<endl;
- tmp=tmp%10;
- }
- if(tmp>=1)
- { cout<<"你一元"<<tmp/1<<"枚"<<endl;
- tmp=tmp%1;
- }
-
- }
- system("pause");
- return 0;
- }
複製代碼 |