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

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!