本帖最後由 鄭繼威 於 2023-2-24 00:02 編輯
設計一個更聰明的智慧找零系統, 包括客人付的錢不足, 剛好, 或任何可能發生的狀況.
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- re:
- system("cls");
- int price,pay,money;
- cout<<"*** 智慧找零系統 ***"<<endl<<endl;
- cout<<"請輸入商品價格: ";
- cin>>price;
- if(price<=0)
- goto re;
- cout<<"客人付了多少錢: ";
- cin>>pay;
- if(pay<0)
- goto re;
- money=pay-price;
- if(money==0)
- {
- cout<<endl<<"剛剛好!銘謝惠顧!"<<endl;
- }else if(money<0)
- {
- cout<<endl<<"錢不夠喔!還差"<<-money<<"元!"<<endl;
- }else if(money>=1000)
- {
- cout<<endl<<"錢太多?全部送我吧!"<<endl;
- }else
- {
- cout<<endl<<"需找客人"<<money<<"元"<<endl<<endl;
- if(money>=500)
- {
- cout<<"五百元鈔票"<<money/500<<"張"<<endl;
- money%=500; //money=money%500;
- }
- if(money>=100)
- {
- cout<<"一百元鈔票"<<money/100<<"張"<<endl;
- money%=100;
- }
- if(money>=50)
- {
- cout<<"五十元硬幣"<<money/50<<"枚"<<endl;
- money%=50;
- }
- if(money>=10)
- {
- cout<<"十元硬幣"<<money/10<<"枚"<<endl;
- money%=10;
- }
- if(money>=5)
- {
- cout<<"五元硬幣"<<money/5<<"枚"<<endl;
- money%=5;
- }
- if(money>0)
- cout<<"一元硬幣"<<money<<"枚"<<endl;
- }
- cout<<endl;
- system("pause");
- goto re;
- return 0;
- }
複製代碼 |