首页 > 代码库 > vending machine-自动贩售机
vending machine-自动贩售机
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
vending machine-自动贩售机
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。