Question: Use C++ write a Design and implement a class of infix calculators ,simply write a function named evaluateInfix() that evaluates infix expressions. It should have

Use C++ write a "Design and implement a class of infix calculators" ,simply write a function named "evaluateInfix()" that evaluates infix expressions. It should have one string parameter and should return an int result. It should call a separate function named "infixToPostfix()" to convert the infix expression into a postfix expression, and then it should do the work of evaluating the resulting postfix expression. Then write a main() function to thoroughly test the function.

The program is complete, but I am having an issue debugging it! Can someone help?

#include

#include

#include

#include

using namespace std;

int precedence(char ch) { switch (ch) { case '+': case '-': return 1; case '*': case '/': return 2; default: return 0; } }

string evaluateInfix(const string infix) { stack aStack; string postfix = ""; string::size_type chPos = 0; while (chPos < infix.length()) { char ch = infix[chPos]; switch (ch) { case '(':aStack.push(ch); break; case ')': while (aStack.top() != '(') { postfix = postfix + aStack.top(); aStack.pop(); } aStack.pop(); break; case '+': case '-': case '*': case '/': while (!aStack.empty() && aStack.top() != '(' && precedence(c) <= precedence(aStack.top())) { postfix = postfix + aStack.top(); aStack.pop(); } aStack.push(ch); break; case ' ': break; default: postfix = postfix + ch; break; } ++chPos; } while (!aStack.empty()) { postfix = postfix + aStack.top(); aStack.pop(); }

return postfix; } int main() { string infix, postfix; while (true) { getline(cin, infix); infix = infix.substr(0, infix.length() - 1); if (infix.length() == 0) break; postfix = evaluateInfix(infix); cout << postfix << endl << endl; } 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!