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 #include

using namespace std;

template class Calculator : public LinkedStack { public: Calculator(); Calculator(std::string x); bool setExpression(std::string x); int eval(int oper1, int oper2, char P); int precedence(char op); bool isOperator( char c); int Evaluate(std::string x);

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 #include using namespace std;

template Calculator::Calculator() {

}

template Calculator::Calculator(std::string x) { LinkedStack(); exp=x; }

template bool Calculator::wellformed(std::string exp) { int s = exp.size(); bool isNum = true; int index = 0; while((isNum) && (index < s)) {

if (isNum==!((isdigit(exp[index])) || (isOperator(exp[index])) || '(' || ')'))

isNum = false;

index++;

} return isNum; }

template bool Calculator::isBalanced(std::string exp) { int s = exp.size(); LinkedStack astack ; bool balancedsofar = true;

int count; for(count = 0; count

{ if(!astack.isEmpty()) astack.pop(); else balancedsofar=false; }

} if(balancedsofar && astack.isEmpty()) return true; else return false; }

template std::string Calculator::Postfixedform(std::string x) { LinkedStack opstack; std::string postfix = " "; int s = x.size();

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 Calculator::evaluatepostfix(std::string exp) {

int operand1; int operand2; char op; LinkedStack operatorstack; int result;

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 bool Calculator::setExpression(std::string x) { if(wellformed(x) && isBalanced(x))

return true; else return false;

}

template int Calculator::Evaluate(std::string x) {

int answer = evaluatepostfix(Postfixedform(x)); return answer; }

template int Calculator::eval(int oper1, int oper2, char operate) { switch (operate) {

case '*': return oper1 * oper2; break;

case '/': return oper2 / oper1; break;

case '+': return oper1 + oper2; break;

case '-': return oper2 - oper1; break;

default : return 0; } }

template bool Calculator::isOperator( char c) { if((c=='+')||(c=='-') || (c=='*') || (c=='/'))

return true; else return false; }

template int Calculator::precedence(char op) {

int w = -1; switch(op) { case '+': case '-':

w = 1; break;

case '/': case '*':

w = 2; break; } return w; }

//appliction file

#include #include "Calculator.h" #include #include "Linkedstack.cpp" #include "Node.cpp" #include "Calculator.cpp" using namespace std;

int main() { int ans; std::string strexp; Calculator mycal; cin>>strexp; if(!mycal.setExpression(strexp)) { cout << " Not valid " << endl; cin>>strexp; }

ans = mycal.Evaluate(strexp);

cout << ans; return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!