Skip to content

Commit

Permalink
refactor: update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Ganesh Rengasamy authored and Ganesh Rengasamy committed Jul 21, 2020
1 parent 0150f47 commit 9aae133
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
7 changes: 6 additions & 1 deletion calculator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ double Calculator::evaluate(std::string expression)
v1=v2=0;
for(int i=0;i < expression.length();i++)
{
// ignore the spaces
if (isspace(expression[i])){
continue;
}

// Push to stack, if digit
if(isdigit(expression[i]))
{
std::string str;
Expand All @@ -36,8 +38,10 @@ double Calculator::evaluate(std::string expression)
}

mStack.push(stof(str));
// Decrease index as currently points to advanced element
i--;
}
// Check if operator and perform the operation of numbers from stack
else if (IsOperator(expression[i])){
if (expression[i] == '+'){
v1 = mStack.top();
Expand Down Expand Up @@ -68,10 +72,11 @@ double Calculator::evaluate(std::string expression)
ret = v2/v1;
}

// push the final result
mStack.push(ret);
}
}
//Return answer
// return answer
return mStack.top();
}

Expand Down
5 changes: 5 additions & 0 deletions calculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ class Calculator {
Calculator() = default;
~Calculator() = default;

/**
* method to evaluate the expression
*/
double evaluate(std::string expression);

private:

//Stack to hold the numbers
std::stack<float> mStack;
bool IsOperator(char c);
};
Expand Down
3 changes: 1 addition & 2 deletions main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ int main() {

dev::Calculator calc;
std::string s;
std::cout<<"enter the string: ";
std::cout<<"Enter the string: ";
std::getline(std::cin,s);

std::cout<<"Result:"<<calc.evaluate(s)<<std::endl;

dev::Project p;
Expand Down

0 comments on commit 9aae133

Please sign in to comment.