Question: Need help with my program.Anyone? //Header file #ifndef CALCULATOR_H #define CALCULATOR_H #include LinkedStack.h #include #include using namespace std; template class Calculator : public LinkedStack {
Need help with my program.Anyone?
//Header file #ifndef CALCULATOR_H #define CALCULATOR_H #include "LinkedStack.h" #include
using namespace std;
template
private: std::string Postfixedform(std::string x); int evaluatepostfix(std::string exp); bool wellformed(std::string exp); bool isBalanced(std::string exp); std::string exp;
};
#endif // CALCULATOR_H
//implementation file
#include "Calculator.h" #include
template
}
template
template
if (isNum==!((isdigit(exp[index])) || (isOperator(exp[index])) || '(' || ')'))
isNum = false;
index++;
} return isNum; }
template
int count; for(count = 0; count
{ if(!astack.isEmpty()) astack.pop(); else balancedsofar=false; }
} if(balancedsofar && astack.isEmpty()) return true; else return false; }
template
for(int i= 0; i < s; i++) {
if(isdigit(x[i]))
postfix += x[i];
else if (x[i] == '(')
opstack.push(x[i]);
else if (isOperator(x[i])) {
if (!opstack.isEmpty() && opstack.peek() != '(' && precedence(x[i]) <= precedence(opstack.peek())) { postfix += opstack.peek(); opstack.pop();
}
opstack.push(x[i]); } else if (x[i] == ')') {
while (!(opstack.peek() == '(')) { postfix += opstack.peek(); opstack.pop();
} opstack.pop(); } }
while (!opstack.isEmpty()) { postfix += opstack.peek(); opstack.pop(); } return postfix; }
template
int operand1; int operand2; char op; LinkedStack
int s = exp.size(); for(int i= 0; i < s; i++) { op = exp[i]; if(isdigit(op)) { operatorstack.push(op-'0');
}
else { operand2 = operatorstack.peek(); operatorstack.pop();
operand1 = operatorstack.peek(); operatorstack.pop();
result = eval(operand1, operand2, op); operatorstack.push(result); } } return result; }
template
return true; else return false;
}
template
int answer = evaluatepostfix(Postfixedform(x)); return answer; }
template
case '*': return oper1 * oper2; break;
case '/': return oper2 / oper1; break;
case '+': return oper1 + oper2; break;
case '-': return oper2 - oper1; break;
default : return 0; } }
template
return true; else return false; }
template
int w = -1; switch(op) { case '+': case '-':
w = 1; break;
case '/': case '*':
w = 2; break; } return w; }
//appliction file
#include
int main() { int ans; std::string strexp; Calculator
ans = mycal.Evaluate(strexp);
cout << ans; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
