Solved: 08 - Switch Statements
Added a contract precondition to apply_operation
This commit is contained in:
@@ -2,20 +2,40 @@
|
||||
|
||||
namespace control_flow {
|
||||
|
||||
auto parse_operation(char op) -> Operation {
|
||||
(void)op;
|
||||
return Operation::Unknown;
|
||||
// enum class Operation { Add, Subtract, Multiply, Divide, Unknown };
|
||||
auto parse_operation(const char op) -> Operation {
|
||||
switch (op) {
|
||||
case '+': return Operation::Add;
|
||||
case '-': return Operation::Subtract;
|
||||
case '*': return Operation::Multiply;
|
||||
case '/': return Operation::Divide;
|
||||
default: return Operation::Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
auto apply_operation(Operation op, int lhs, int rhs) -> int {
|
||||
(void)op; (void)lhs; (void)rhs;
|
||||
return 0;
|
||||
auto apply_operation(const Operation op, const int lhs, const int rhs) -> int
|
||||
pre(op != Operation::Unknown)
|
||||
{
|
||||
switch (op) {
|
||||
case Operation::Add: return lhs + rhs;
|
||||
case Operation::Subtract: return lhs - rhs;
|
||||
case Operation::Multiply: return lhs * rhs;
|
||||
case Operation::Divide: return lhs / rhs;
|
||||
default: return -1; // Unreachable due to contract
|
||||
}
|
||||
}
|
||||
|
||||
auto day_name(int day) -> std::string {
|
||||
// TODO: 1=Monday ... 7=Sunday, else "Invalid"
|
||||
(void)day;
|
||||
return "Invalid";
|
||||
auto day_name(const int day) -> std::string {
|
||||
switch (day) {
|
||||
case 1: return "Monday";
|
||||
case 2: return "Tuesday";
|
||||
case 3: return "Wednesday";
|
||||
case 4: return "Thursday";
|
||||
case 5: return "Friday";
|
||||
case 6: return "Saturday";
|
||||
case 7: return "Sunday";
|
||||
default: return "Invalid";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace control_flow
|
||||
|
||||
Reference in New Issue
Block a user