任务2——引入函数改善程序结构
1、数据中,用户密码、帐户余额用全局变量存储。 2、在输入密码环节,实现有限次密码输入的限制,如果三次都没有输入正确,程序将直接退出,不允许用户继续操作。 并将查验密码功能做成一个函数。
/* *Copyright (c) 2014,烟台大学计算机学院 *All gight reserved. *文件名称:temp.cpp *作者:邵帅 *完成时间:2014年11月12日 *版本号:v1.0*/#include运行结果:using namespace std;int password;double money;bool pass();void work();void exit();void showbalance();double drawmoney(double money);double deposit(double money);double transferAccounts(double money);int updatePassword(int password);int main(){ password=123456; money=10000; cout << "麻鱼银行欢迎您的光临!" << endl; cout << "请输入密码:"; if (pass()) { cout< > pass; if (pass == password) { passtrue = true; return passtrue; } else { i++; if (i < 4) cout << "密码输入错误" << i-1 << "次,再输错" << 4 - i << "次将要吞卡。" << endl; else if (i = 4) cout << "银行卡已被吞。" << endl; continue; } } while (passtrue = false || i < 4);}void work(){ int choice; while (choice != 0) { cout << "* 您可以办理下面的业务:" << endl; cout << "* 1.查询 2.取款 3.存款 4.转账 5.改密 0.退出" << endl; cout << "* 请输入(0~5):"; cin >> choice; switch (choice) { case 0: exit(); break; case 1: showbalance(); break; // 查询 case 2: money=drawmoney(money); break; // 取款 case 3: money=deposit(money); break; // 存款 case 4: money=transferAccounts(money); break; // 转账 case 5: password=updatePassword(password); break; // 改密 default: { cout << "输入错误,请重新输入。" << endl; continue; } } }}void exit(){ cout << "欢迎下次光临。" << endl;}void showbalance(){ cout << "您当前的账户余额为:" << money << "元。" << endl<< endl; work();}double drawmoney(double money){ double outmoney; while (1) { cout << "请输入取款金额:"; cin >> outmoney; if (outmoney > money) { cout << "对不去,余额不足。" << endl; continue; } else { money -= outmoney; cout << "取款后,您的余额是:" << money << "元" < < > inmoney; money += inmoney; cout << "存款后,您的余额是:" << money << "元。" << endl< > changemoney; cout << "请输入对方账户:"; cin >> otheraccount; if (otheraccount > money) { cout << "对不去,余额不足,请重新输入。" << endl; continue; cout << endl; } else { money -= changemoney; cout<<"汇款成功!"<<"您成功汇款"< <<"元。"< > pass_1; cout << "请确认新密码:"; cin >> pass_2; if (pass_1 == pass_2) { password = pass_1; cout << "密码修改成功!" << endl<
@ Mayuko