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.
Use the pseudocode algorithm that evaluates postfix expressions given at the end of section 6.3.1 and the pseudocode algorithm that converts an infix expression to postfix form given near the end of section 6.3.2. Use the STL stack class. Include the output. Also don't forget the separate function "infixToPostfix()"
Here is the pseudocode for 6.3.1
for ( each character ch in the string) {
if (ch is an operand)
Push the value of the operand ch onto the stack
else // ch is an operator named
{
// Evaluate and push the result
operand2 = top of stack
Pop the stack
operand1 = top of stack
Pop the stack
result = operand1 op operand2
Push result onto the stack
}
}
Here is the pseudocode for 6.3.2
for ( each character ch in the infix expression) {
switch (ch) {
case operand: // Append operand to end of postfix expressionstep 1
postfixExp = postfixExp ch
break
case '(': // Save '(' on stackstep 2
aStack.push(ch)
break
case operator: // Process stack operators of greater precedencestep 3
while (!aStack.isEmpty() and aStack.peek() is not a '(' and precedence(ch) <= precedence(aStack.peek())) {
Append aStack.peek() to the end of postfixExp
aStack.pop()
}
aStack.push(ch) // Save the operator
break
case ')': // Pop stack until matching '(' step 4
while (aStack.peek() is not a '(')
{ Append aStack.peek() to the end of postfixExp
aStack.pop()
}
aStack.pop() // Remove the open parenthesis
break }
}
// Append to postfixExp the operators remaining in the stackstep 5
while (!aStack.isEmpty())
{ Append aStack.peek() to the end of postfixExp
aStack.pop()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started