Skip to content

Commit

Permalink
refactor: update namings of variables
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 9aae133 commit 0d0d602
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 42 deletions.
80 changes: 40 additions & 40 deletions calculator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace dev {

bool Calculator::IsOperator(char c){
switch (c){
bool Calculator::isOperator(char op){
switch (op){
case '+':
case '-':
case '*':
Expand All @@ -17,67 +17,67 @@ bool Calculator::IsOperator(char c){
double Calculator::evaluate(std::string expression)
{
double ret =0.0;
float v1,v2;
v1=v2=0;
for(int i=0;i < expression.length();i++)
float var1,var2;
var1=var2=0.0;
for(int index=0;index < expression.length();index++)
{
// ignore the spaces
if (isspace(expression[i])){
if (isspace(expression[index])){
continue;
}

// Push to stack, if digit
if(isdigit(expression[i]))
if(isdigit(expression[index]))
{
std::string str;
std::string digitsStr;

while (i < expression.size() && ((isdigit(expression[i]) || expression[i] == '.')))
while (index < expression.size() && ((isdigit(expression[index]) || expression[index] == '.')))
{
str.push_back(expression[i]);
i++;
digitsStr.push_back(expression[index]);
index++;
}

mStack.push(stof(str));
mOperandStack.push(stof(digitsStr));
// Decrease index as currently points to advanced element
i--;
index--;
}
// Check if operator and perform the operation of numbers from stack
else if (IsOperator(expression[i])){
if (expression[i] == '+'){
v1 = mStack.top();
mStack.pop();
v2 = mStack.top();
mStack.pop();
ret = v1+v2;
else if (isOperator(expression[index])){
if (expression[index] == '+'){
var1 = mOperandStack.top();
mOperandStack.pop();
var2 = mOperandStack.top();
mOperandStack.pop();
ret = var1+var2;
}
if (expression[i] == '-'){
v1 = mStack.top();
mStack.pop();
v2 = mStack.top();
mStack.pop();
ret = v2-v1;
if (expression[index] == '-'){
var1 = mOperandStack.top();
mOperandStack.pop();
var2 = mOperandStack.top();
mOperandStack.pop();
ret = var2-var1;
}
if (expression[i] == '*'){
v1 = mStack.top();
mStack.pop();
v2 = mStack.top();
mStack.pop();
ret = v1*v2;
if (expression[index] == '*'){
var1 = mOperandStack.top();
mOperandStack.pop();
var2 = mOperandStack.top();
mOperandStack.pop();
ret = var1*var2;
}
if (expression[i] == '/'){
v1 = mStack.top();
mStack.pop();
v2 = mStack.top();
mStack.pop();
ret = v2/v1;
if (expression[index] == '/'){
var1 = mOperandStack.top();
mOperandStack.pop();
var2 = mOperandStack.top();
mOperandStack.pop();
ret = var2/var1;
}

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

} // namespace dev
4 changes: 2 additions & 2 deletions calculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Calculator {
private:

//Stack to hold the numbers
std::stack<float> mStack;
bool IsOperator(char c);
std::stack<float> mOperandStack;
bool isOperator(char op);
};

} // namespace calculator

0 comments on commit 0d0d602

Please sign in to comment.