- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int number, total=0, a, b;
- while(true)
- {
- cout<<"請輸入要購買的物品 (1)蘋果18元 (2)芒果56元 (3)西瓜250元 (4)結帳: ";
- cin>>number;
- switch(number)
- {
- case 1:
- total=total+18;
- cout<<"目前: "<<total<<"元"<<endl<<endl;
- break;
- case 2:
- total=total+56;
- cout<<"目前: "<<total<<"元"<<endl<<endl;
- break;
- case 3:
- total=total+250;
- cout<<"目前: "<<total<<"元"<<endl<<endl;
- break;
- case 4:
- cout<<"目前: "<<total<<"元"<<endl;
- cout<<"付款: ";
- cin>>a;
- b=a-total;
- total=0;
- cout<<"需找"<<b<<"元"<<endl<<endl;
- if(b >= 500)
- {
- cout<<"五百元"<<b/500<<"張"<<endl;
- b=b%500;
- }
- if(b >= 100)
- {
- cout<<"一百元"<<b/100<<"張"<<endl;
- b=b%100;
- }
- if(b >= 50)
- {
- cout<<"五十元"<<b/50<<"枚"<<endl;
- b=b%50;
- }
- if(b >= 10)
- {
- cout<<"十元"<<b/10<<"枚"<<endl;
- b=b%10;
- }
- if(b >= 1)
- {
- cout<<"一元"<<b<<"枚"<<endl;
- }
- cout<<endl;
- }
- }
- system("pause");
- return 0;
- }
複製代碼 |