c++ - 如何简化 if - else 语句?
问题描述
我完成了一个 - 非常菜鸟的创建,因为我仍然是菜鸟 :( - 充当银行账户的程序。
我有main.cpp
, Account.cpp
, Account.h
.
在main.cpp
我有:
#include <iostream>
#include <string>
#include "Account.h"
int main()
{
Account Luigis;
Luigis.set_name("Luigi's Account");
Luigis.set_balance(0);
std::string letter;
do {
std::cout << "Your current balance is: " << Luigis.get_balance() << std::endl;
std::cout << "What would you like, to deposit, to withdraw or to exit?" << std::endl;
std::cout << "(D for 'Deposit, W for 'Withdraw' and Q for 'Exit') D/W/Q ";
std::cin >> letter;
if(letter == "d" || letter == "D") {
std::cout << "How much would you like to deposit?: ";
double amountDeposit{};
std::cin >> amountDeposit;
Luigis.deposit(amountDeposit);
}
else if(letter == "w" || letter == "W") {
std::cout << "How much would you like to withdraw?: ";
double amountWithdraw;
std::cin >> amountWithdraw;
Luigis.withdraw(amountWithdraw);
}
else if(letter == "q" || letter == "Q") {
std::cout << "Goodbye!" << std::endl;
}
else
std::cout << "Invalid selection, please try again with: D for Deposit, W for Withdraw, Q for Exist" << std::endl;
} while (letter != "q" && letter != "Q");
return 0;
}
在Account.cpp
我有:
#include "Account.h"
#include <string>
#include <iostream>
//IMPLEMENTATION or DEFINITION
bool Account::deposit(double amount) {
//money you're doing to deposit
balance = balance + amount;
}
bool Account::withdraw(double amount) {
//if you can withdraw
if(balance-amount >= 0) {
balance = balance - amount;
return true;
} else {
std::cout << "You cannot withdraw the specified amount - balance is too low and it would result in " << balance - amount << " funds!" << std::endl;
return false;
}
}
void Account::set_name(std::string name) {
this->name = name;
}
std::string Account::get_name(){
return name;
}
void Account::set_balance(double balance) {
this->balance = balance;
}
double Account::get_balance() {
return balance;
}
在Account.h
我有:
#ifndef _ACCOUNT_H_
#define _ACCOUNT_H_
#include <string>
//SPECIFICATION or DECLARATION
class Account
{
private:
//attributes
std::string name;
double balance;
public:
//methods
bool deposit(double amount);
bool withdraw(double amount);
void set_name(std::string name);
std::string get_name();
void set_balance(double balance);
double get_balance();
};
#endif // _ACCOUNT_H_
我的问题是:既然我“知道”如何使用函数,我该如何简化if
-else
语句?main.cpp
我的朋友说我应该能够使用函数并使事情更抽象,以避免写所有的if
andelse
语句。
基本上,我将 OOP 与过程编程混合在一起,这违背了目的。
解决方案
我认为您对条件构造的复杂性有夸大的印象。对我(以及至少 5 个其他人,从评论和“同意”得到的结果来看)它们似乎并不过分复杂。
因此,我提出了一些改变您的印象的方法,尽管它根本不会改变功能:
请应用一致且严格的缩进,实际样式无关紧要。
如果您允许我提出我最喜欢的风格,以下是结果示例:
if(letter == "d" || letter == "D")
{
std::cout << "How much would you like to deposit?: ";
double amountDeposit{};
std::cin >> amountDeposit;
Luigis.deposit(amountDeposit);
} else if(letter == "w" || letter == "W")
{
std::cout << "How much would you like to withdraw?: ";
double amountWithdraw;
std::cin >> amountWithdraw;
Luigis.withdraw(amountWithdraw);
} else if(letter == "q" || letter == "Q")
{
std::cout << "Goodbye!" << std::endl;
} else
{
std::cout << "Invalid selection, please try again with: D for Deposit, W for Withdraw, Q for Exist"
<< std::endl;
}
推荐阅读
- r - 文件中的错误(文件,“rt”):无法打开连接,只能打开一些目录文件?
- winapi - 如何使用 CreateProcess 函数正确调用“telnet”?
- ethereum - 迁移松露 rinkeby 时出错 [无法读取未定义的属性“绑定”]
- date - 无法在 elastisearch 上定义日期类型索引
- android - 在 react native 中使用 KeyboardAvoidingView 时,整个内容都被向上推
- cassandra - 为什么将列更新为空后(插入的)行会消失?(但不是在插入时)
- c - 我不知道为什么我的链表指针不动
- c++ - 如何处理类及其属性的抽象和特化?
- neo4j - 如何在 neo4j 中复制节点树?
- python - 将 Facenet 模型 .pb 文件转换为 TFLITE 格式时出错