Vending machine-vending machine: enter money every time until the money is enough, and then return the excess money.
// Vending machine-vending machine # include <iostream> const double apply_money = 3.5; double total_money; double accept_money (); double compute_change (double total_money); int main () {using namespace STD; double money_back; total_money = accept_money (); money_back = compute_change (total_money); cout <"enjoy you drink, and money back" <money_back <Endl; return 0;} double accept_money () {using namespace STD; char ans; Total_money = 0; do {cout <"Enter the money :( D-dollar (1), Q-quarter (0.25), D-Dime (0.10 ), n-Nickel (0.05) \ n "; cout <" The apply money is 3.5, the inserted money is "<total_money <Endl; CIN> ans; switch (ANS) {Case 'D': total_money + = 1; break; Case 'q': total_money + = 0.25; break; Case 'D': total_money + = 0.10; break; Case 'N': total_money + = 0.05; break; default: cout <"error input! "<Endl ;}}while (total_money <apply_money); Return total_money;} double compute_change (double total_money) {return (total_money-apply_money );}
Result:
Enter the money:(D-dollar(1),q-quarter(0.25),d-dime(0.10),n-nickel(0.05))The apply money is 3.5,the inserted money is 0DEnter the money:(D-dollar(1),q-quarter(0.25),d-dime(0.10),n-nickel(0.05))The apply money is 3.5,the inserted money is 1dEnter the money:(D-dollar(1),q-quarter(0.25),d-dime(0.10),n-nickel(0.05))The apply money is 3.5,the inserted money is 1.1qEnter the money:(D-dollar(1),q-quarter(0.25),d-dime(0.10),n-nickel(0.05))The apply money is 3.5,the inserted money is 1.35qEnter the money:(D-dollar(1),q-quarter(0.25),d-dime(0.10),n-nickel(0.05))The apply money is 3.5,the inserted money is 1.6DEnter the money:(D-dollar(1),q-quarter(0.25),d-dime(0.10),n-nickel(0.05))The apply money is 3.5,the inserted money is 2.6DEnjoy you drink,and money back 0.1
Vending machine-Vending Machine