Starting Out with C++: Early Objects (9th Edition)
Starting Out with C++: Early Objects (9th Edition)
9th Edition
ISBN: 9780134400242
Author: Tony Gaddis, Judy Walters, Godfrey Muganda
Publisher: PEARSON
Question
Book Icon
Chapter 18, Problem 15PC
Program Plan Intro

Stack Based evaluation of prefix expression

Program Plan:

  • Declare a structure to create a stack element.
    • Include all the required header files.
  • Declare a function to input prefix statements.
  • Declare a Function int evaluate that evaluates the prefix expression by considering if the next token in the input stream is an integer, read  the integer and push it onto the stack using the push() operation of the stack .
    • But if the input stream is an operator, pop the last two values from the stack using the pop operation and apply the operator, and push the result onto the stack and the lone value is the result.
  • Declare the function bool prefix_reducible that returns true if there are at least three elements on the stack and top two elements are values and the third from the top is an operator.
  • Declare the main function.
    • Prompt the user to enter a prefix expression.
      • Evaluate the prefix expression by calling the int evaluate function and print the result.

Blurred answer
Students have asked these similar questions
* This function evaluates the provided postfix expression. When an operand is encountered, it is added to the stack. When an operator is encountered, two operands are popped, the operation is evaluated, and the result is pushed to the stack.* @param expr string postfix expression representation * @return value of the supplied expression
The destination of a function's return value may be represented as a sequence of instructions. When making modifications to the stack, keep in mind that they must not prevent the method from returning to its caller.
Python Language - Bad Luck Numbers
Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning