vending machine-自动贩售机:每次提示输入money,直到money足够,再退回多余的money.
//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); }
结果:
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 0 D 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 1 d 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 1.1 q 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 1.35 q 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 1.6 D 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 2.6 D Enjoy you drink,and money back 0.1
原文地址:http://9320314.blog.51cto.com/9310314/1546935