Board logo

標題: [隨堂練習] 智慧找零系統 [打印本頁]

作者: 周政輝    時間: 2017-8-12 09:03     標題: [隨堂練習] 智慧找零系統

本帖最後由 周政輝 於 2017-8-12 09:58 編輯

使用者可以自行輸入商品的金額以及想要支付的費用
並換算總共該找的零錢數量
  1. #include<cstdlib>
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.     int money =0; //支付金額
  7.     int buy = 0; // 購買商品金額
  8.     int total =0; // 剩餘金額
  9.     int thousand = 0; // 千
  10.     int hundred = 0; // 百
  11.     int fiftydollers = 0; //五十
  12.     int tendollers = 0; // 十
  13.     int fivedollers =0; // 五
  14.     int onedollers = 0; // 一
  15.      
  16.     cout << "請輸入購買金額:" << endl;
  17.     cin >> money; //500
  18.     cout << "請輸入付款金額:" << endl;
  19.     cin >> buy; // 1000
  20.    
  21.     total = buy - money;  // 500 = 1000-500
  22.    
  23.     if(total <0) // -50 = 450 - 500
  24.     {
  25.       cout << "金額不足,請重新輸入" << endl;         
  26.     }
  27.     else {
  28.        // total 是要找回的金額  
  29.        // 商品 345
  30.        // 支付 1000
  31.        // 找 655
  32.         
  33.        thousand = total/1000;   // 0
  34.        total = total % 1000; // 655
  35.        hundred = total/100; // 6
  36.        total = total % 100; // 55
  37.        fiftydollers = total/50; // 1
  38.        total = total %50; // 5
  39.        tendollers = total /10; // 0
  40.        total = total % 10; // 5
  41.        fivedollers = total /5; // 1
  42.        total = total % 5; // 0
  43.        onedollers = total;  
  44.       
  45.     }
  46.    
  47.     cout << thousand << "千" <<  hundred << "百" <<  fiftydollers << "五十" <<  tendollers << "十" <<  fivedollers << "五" <<  onedollers << "元" << endl;
  48.    
  49.    
  50.      
  51.    
  52.    
  53.    
  54.     system("pause");
  55.     return 0;   
  56. }
複製代碼

作者: 莊旻叡    時間: 2017-8-12 10:35

此帖僅作者可見
作者: 高睿辰    時間: 2017-8-12 10:35

此帖僅作者可見




歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/) Powered by Discuz! 7.2