2026/2/15 7:47:00
网站建设
项目流程
网站建设如何更加稳定,龙华龙岗网站建设公司,百度推广网站备案,网页美工设计教学设计C实现ATM状态机
以下是一个使用 C 实现的 ATM 状态机示例程序#xff0c;采用面向对象的方式实现。程序模拟了一个简单的 ATM 系统#xff0c;包含以下功能#xff1a;
用户登录查询余额存款取款退出完整代码
#include iostream
#include string
#include 实现ATM状态机以下是一个使用 C 实现的 ATM 状态机示例程序采用面向对象的方式实现。程序模拟了一个简单的 ATM 系统包含以下功能用户登录查询余额存款取款退出完整代码#includeiostream#includestring#includeunordered_mapusingnamespacestd;// 定义 ATM 状态的基类classATMState{public:virtualvoidhandle()0;// 纯虚函数表示状态处理virtual~ATMState(){}};// 定义 ATM 类classATM{private:unordered_mapstring,stringusers;// 用户名和密码unordered_mapstring,doublebalances;// 用户余额ATMState*currentState;// 当前状态string currentUser;// 当前登录用户public:ATM(){// 初始化用户数据users[user1]password1;users[user2]password2;balances[user1]1000.0;balances[user2]500.0;currentStatenullptr;}voidsetState(ATMState*state){currentStatestate;}voidhandle(){if(currentState){currentState-handle();}}boollogin(conststringusername,conststringpassword){if(users.find(username)!users.end()users[username]password){currentUserusername;returntrue;}returnfalse;}doublegetBalance(){returnbalances[currentUser];}voiddeposit(doubleamount){balances[currentUser]amount;}boolwithdraw(doubleamount){if(balances[currentUser]amount){balances[currentUser]-amount;returntrue;}returnfalse;}voidlogout(){currentUser;}boolisLoggedIn(){return!currentUser.empty();}};// 定义具体状态类classIdleState:publicATMState{private:ATM*atm;public:IdleState(ATM*atm):atm(atm){}voidhandle()override{string username,password;coutEnter username: ;cinusername;coutEnter password: ;cinpassword;if(atm-login(username,password)){coutLogin successful!endl;atm-setState(newMenuState(atm));}else{coutInvalid username or password. Try again.endl;}}};classMenuState:publicATMState{private:ATM*atm;public:MenuState(ATM*atm):atm(atm){}voidhandle()override{intchoice;do{cout\nATM Menu:endl;cout1. Check Balanceendl;cout2. Depositendl;cout3. Withdrawendl;cout4. Logoutendl;coutEnter your choice: ;cinchoice;switch(choice){case1:coutYour balance is: $atm-getBalance()endl;break;case2:{doubleamount;coutEnter amount to deposit: ;cinamount;atm-deposit(amount);coutDeposit successful!endl;break;}case3:{doubleamount;coutEnter amount to withdraw: ;cinamount;if(atm-withdraw(amount)){coutWithdrawal successful!endl;}else{coutInsufficient balance!endl;}break;}case4:atm-logout();coutLogged out successfully!endl;atm-setState(newIdleState(atm));return;default:coutInvalid choice. Try again.endl;}}while(choice!4);}};intmain(){ATM atm;atm.setState(newIdleState(atm));while(true){atm.handle();}return0;}代码说明状态机设计使用ATMState作为状态的基类定义了一个纯虚函数handle()。IdleState和MenuState是具体的状态类分别表示 ATM 的空闲状态和菜单状态。ATM 类ATM类包含用户数据用户名、密码、余额和当前状态。提供了登录、查询余额、存款、取款和注销等功能。状态切换在IdleState中用户输入用户名和密码进行登录。如果登录成功状态切换到MenuState。在MenuState中用户可以选择不同的操作完成后可以返回主菜单或注销。动态分配状态使用new动态分配状态对象并通过setState()方法切换状态。运行示例输入Enter username: user1 Enter password: password1 Login successful! ATM Menu: 1. Check Balance 2. Deposit 3. Withdraw 4. Logout Enter your choice: 1输出Your balance is: $1000扩展功能支持更多状态可以扩展更多状态例如维护模式、错误状态等。持久化存储将用户数据存储到数据库或文件中而不是硬编码在程序中。安全性使用加密存储密码避免明文存储。通过这种面向对象的设计可以轻松扩展 ATM 的功能同时保持代码的清晰和可维护性。