本帖最後由 周政輝 於 2017-8-12 09:58 編輯
使用者可以自行輸入商品的金額以及想要支付的費用
並換算總共該找的零錢數量- #include<cstdlib>
- #include<iostream>
- using namespace std;
- int main()
- {
- int money =0; //支付金額
- int buy = 0; // 購買商品金額
- int total =0; // 剩餘金額
- int thousand = 0; // 千
- int hundred = 0; // 百
- int fiftydollers = 0; //五十
- int tendollers = 0; // 十
- int fivedollers =0; // 五
- int onedollers = 0; // 一
-
- cout << "請輸入購買金額:" << endl;
- cin >> money; //500
- cout << "請輸入付款金額:" << endl;
- cin >> buy; // 1000
-
- total = buy - money; // 500 = 1000-500
-
- if(total <0) // -50 = 450 - 500
- {
- cout << "金額不足,請重新輸入" << endl;
- }
- else {
- // total 是要找回的金額
- // 商品 345
- // 支付 1000
- // 找 655
-
- thousand = total/1000; // 0
- total = total % 1000; // 655
- hundred = total/100; // 6
- total = total % 100; // 55
- fiftydollers = total/50; // 1
- total = total %50; // 5
- tendollers = total /10; // 0
- total = total % 10; // 5
- fivedollers = total /5; // 1
- total = total % 5; // 0
- onedollers = total;
-
- }
-
- cout << thousand << "千" << hundred << "百" << fiftydollers << "五十" << tendollers << "十" << fivedollers << "五" << onedollers << "元" << endl;
-
-
-
-
-
-
- system("pause");
- return 0;
- }
複製代碼 |