本帖最後由 tonyh 於 2013-6-23 14:03 編輯
檢視該原始程式碼, 試著改寫使其更加完善.- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int a,b,c,d,e,f,g,h,sprice,total,pay,money;
- cout<<"***智能玩具店***"<<endl;
- cout<<"**商品價目表**"<<endl;
- cout<<"(1)遙控汽車 450元"<<endl;
- cout<<"(2)飛機模型 550元"<<endl;
- cout<<"(3)足球 325元"<<endl;
- cout<<"(4)拼圖 200元"<<endl;
- cout<<"(5)玩具槍 660元"<<endl;
- cout<<"(6)可愛玩偶 150元"<<endl;
- cout<<"(7)籃球 380元"<<endl;
- cout<<"請選擇你要的四樣商品-----"<<endl;
- cout<<"請輸入您要的第一樣商品的商品代碼: ";
- cin>>a;
- cout<<"數量: ";
- cin>>b;
- cout<<"請輸入您要的第二樣商品商品代碼: ";
- cin>>c;
- cout<<"數量: ";
- cin>>d;
- cout<<"請輸入您要的第三樣商品商品代碼: ";
- cin>>e;
- cout<<"數量: ";
- cin>>f;
- cout<<"請輸入您要的第四樣商品商品代碼: ";
- cin>>g;
- cout<<"數量: ";
- cin>>h;
- switch(a)
- {
- case 1:
- sprice=450;
- break;
- case 2:
- sprice=550;
- break;
- case 3:
- sprice=325;
- break;
- case 4:
- sprice=200;
- break;
- case 5:
- sprice=660;
- break;
- case 6:
- sprice=150;
- break;
- case 7:
- sprice=380;
- break;
- default:
- cout<<"輸入錯誤"<<endl;
- }
- total=(sprice*b)+(sprice*d)+(sprice*f)+(sprice*h);
- cout<<"總共是"<<total<<"元"<<endl;
- cout<<"請輸入你付的錢: "<<endl;
- cin>> pay;
- money=pay-total;
- cout<<"找你 "<<money<<"元"<<endl;
- if(money>=500)
- {
- cout<<"500元鈔票"<<money/500<<"張"<<endl;
- money=money%500;
- }
- if(money>=100)
- {
- cout<<"100元鈔票"<<money/100<<"張"<<endl;
- money=money%100;
- }
- if(money>=50)
- {
- cout<<"50元硬幣"<<money/50<<"枚"<<endl;
- money=money%50;
- }
- if(money>=10)
- {
- cout<<"10元硬幣"<<money/10<<"枚"<<endl;
- money=money%10;
- }
- if(money>=5)
- {
- cout<<"5元硬幣"<<money/5<<"枚"<<endl;
- money=money%5;
- }
- if(money>=1)
- {
- cout<<"1元硬幣"<<money<<"枚"<<endl;
- }
-
- system("pause");
- return 0;
- }
複製代碼 |