18岁:一场关于禁忌与自由的华丽冒险

核心内容摘要

从“暗黑传奇”到“全能女神”:上原亚衣新作重磅曝光,这场跨界革命你准备好了吗?
那些年,我们一起愁愁愁愁的青涩时光

解锁17c永久免费网名:数字时代的个性化符号,你也能拥有!

C实现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]

1

0;balances[user2]

5

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;cout

Check Balanceendl;cout

Depositendl;cout

Withdrawendl;cout

Logoutendl;coutEnter your choice: ;cinchoice;switch(choice){

:coutYour balance is: $atm-getBalance()endl;break;

:{doubleamount;coutEnter amount to deposit: ;cinamount;atm-deposit(amount);coutDeposit successful!endl;break;}

:{doubleamount;coutEnter amount to withdraw: ;cinamount;if(atm-withdraw(amount)){coutWithdrawal successful!endl;}else{coutInsufficient balance!endl;}break;}

:atm-logout();coutLogged out successfully!endl;atm-setState(newIdleState(atm));return;default:coutInvalid choice. Try again.endl;}}while(choice!

;}};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:

Check Balance

Deposit

Withdraw

Logout Enter your choice: 1输出Your balance is: $1000扩展功能支持更多状态可以扩展更多状态例如维护模式、错误状态等。

持久化存储将用户数据存储到数据库或文件中而不是硬编码在程序中。

安全性使用加密存储密码避免明文存储。

通过这种面向对象的设计可以轻松扩展 ATM 的功能同时保持代码的清晰和可维护性。

唐伯虎甜心vlog网站-唐伯虎甜心vlog网站应用

百度百家号客服电话人工服务

123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123