Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 31st International Conference Dexa 2020 Bratislava Slovakia September 14 17 2020 Proceedings Part 1 Lncs 12391

Authors: Sven Hartmann ,Josef Kung ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

303059002X, 978-3030590024

More Books

Students also viewed these Databases questions

Question

8. Managers are not trained to be innovation leaders.

Answered: 1 week ago