返回列表 發帖
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int number, total=0, a, b;
  7.     while(true)
  8.     {
  9.         cout<<"請輸入要購買的物品 (1)蘋果18元 (2)芒果56元 (3)西瓜250元 (4)結帳: ";
  10.         cin>>number;
  11.         switch(number)
  12.         {
  13.             case 1:
  14.                 total=total+18;
  15.                 cout<<"目前: "<<total<<"元"<<endl<<endl;
  16.                 break;
  17.             case 2:
  18.                 total=total+56;
  19.                 cout<<"目前: "<<total<<"元"<<endl<<endl;
  20.                 break;
  21.             case 3:
  22.                 total=total+250;
  23.                 cout<<"目前: "<<total<<"元"<<endl<<endl;
  24.                 break;
  25.             case 4:
  26.                 cout<<"目前: "<<total<<"元"<<endl;
  27.                 cout<<"付款: ";
  28.                 cin>>a;
  29.                 b=a-total;
  30.                 total=0;
  31.                 cout<<"需找"<<b<<"元"<<endl<<endl;
  32.                 if(b >= 500)
  33.                 {
  34.                     cout<<"五百元"<<b/500<<"張"<<endl;
  35.                     b=b%500;
  36.                 }
  37.                 if(b >= 100)
  38.                 {
  39.                     cout<<"一百元"<<b/100<<"張"<<endl;
  40.                     b=b%100;
  41.                 }
  42.                 if(b >= 50)
  43.                 {
  44.                     cout<<"五十元"<<b/50<<"枚"<<endl;
  45.                     b=b%50;
  46.                 }
  47.                 if(b >= 10)
  48.                 {
  49.                     cout<<"十元"<<b/10<<"枚"<<endl;
  50.                     b=b%10;
  51.                 }
  52.                 if(b >= 1)
  53.                 {
  54.                     cout<<"一元"<<b<<"枚"<<endl;
  55.                 }
  56.                 cout<<endl;
  57.         }
  58.     }
  59.     system("pause");
  60.     return 0;
  61. }
複製代碼

TOP

返回列表