Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My solution for calculate function #1401

Closed
wants to merge 6 commits into from
Closed
20 changes: 17 additions & 3 deletions homework/calculate/calculate.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
#pragma once
#include <stdexcept>
#include <string>

std::string calculate(const std::string& command, int first, int second) {
// TODO: Implement your solution here and return proper value
return "";
std::string calculate(const std::string &command, int first, int second) {
if (command == "add") {
return std::to_string(first + second);
} else if (command == "subtract") {
return std::to_string(first - second);
} else if (command == "multiply") {
return std::to_string(first * second);
} else if (command == "divide") {
if (second == 0) {
return "Division by 0";
} else {
return std::to_string(first / second);
}
} else {
return "Invalid data";
}
}
Loading