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

init calculator unit test #10

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
40 changes: 27 additions & 13 deletions src/Calculator/calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@ double Calculator::calculate(std::string input)

for (auto token : tokenize(input)) {

if (isNumber(token)) {
m_tmp_operands.push(atof(token.c_str()));
if (isOperand(token)) {
handleOperandToken(token);
continue;
}

if (isOperator(token.at(0))) {
auto res = calculateNext(popOperands(), token.at(0));

if (!res) {
return 0.0;
if (isOperator(token)) {
if (!handleOperatorToken(token)) {
return {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case if there is wrong input, user will see 0 as calculation result?

}

m_tmp_operands.push(res.value());
continue;
}

Expand Down Expand Up @@ -91,7 +87,24 @@ Calculator::result Calculator::calculateNext(operands operands, char oper)
}
}

Calculator::tokens Calculator::tokenize(std::string input) const
void Calculator::handleOperandToken(const std::string &token)
{
m_tmp_operands.push(atof(token.c_str()));
}

Calculator::result Calculator::handleOperatorToken(const std::string &token)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do assertion in places where you need

{
auto res = calculateNext(popOperands(), token.at(0));

if (!res) {
return {};
}

m_tmp_operands.push(res.value());
return res;
}

Calculator::tokens Calculator::tokenize(const std::string &input) const
{
std::istringstream inputStream(input);
std::vector<std::string> tokens(std::istream_iterator<std::string>{inputStream},
Expand All @@ -105,11 +118,12 @@ Calculator::tokens Calculator::tokenize(std::string input) const
}


bool Calculator::isOperator(char oper) const {
return std::find(m_availableOperators.begin(), m_availableOperators.end(), oper) != m_availableOperators.end();
bool Calculator::isOperator(const std::string &oper) const {
auto _oper = oper.at(0);
return std::find(m_availableOperators.begin(), m_availableOperators.end(), _oper) != m_availableOperators.end();
}

bool Calculator::isNumber(std::string token) const
bool Calculator::isOperand(std::string token) const
{
// consider negative numbers
if (token.at(0) == '-') {
Expand Down
8 changes: 5 additions & 3 deletions src/Calculator/include/calculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ class Calculator{
private:
operands popOperands();
result calculateNext(operands operands, char oper);
void handleOperandToken(const std::string& token);
result handleOperatorToken(const std::string& token);

tokens tokenize(std::string input) const;
bool isOperator(char oper) const;
bool isNumber(std::string token) const;
tokens tokenize(const std::string& input) const;
bool isOperator(const std::string& oper) const;
bool isOperand(std::string token) const;

std::stack<double> m_tmp_operands;
std::string m_error;
Expand Down