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
Add remain code and explanation of whole code.    Given a stack, a function is_consecutive takes a stack as a parameter and thatreturns whether or not the stack contains a sequence of consecutive integersstarting from the bottom of the stack (returning true if it does, returningfalse if it does not). For example:bottom [3, 4, 5, 6, 7] topThen the call of is_consecutive(s) should return true.bottom [3, 4, 6, 7] topThen the call of is_consecutive(s) should return false.bottom [3, 2, 1] topThe function should return false due to reverse order. Note: There are 2 solutions:first_is_consecutive: it uses a single stack as auxiliary storagesecond_is_consecutive: it uses a single queue as auxiliary storage"""import collections def first_is_consecutive(stack):    storage_stack = []    for i in range(len(stack)):        first_value = stack.pop()        if len(stack) == 0:  # Case odd number of values in stack            return True        second_value = stack.pop()        if first_value -…
(Postfix Evaluation) Write a program that evaluates a valid postfix expression such as 6 2 + 5 * 8 4 / -The program should read a postfix expression consisting of digits and operators into a string. Using modified versions of the stack functions implemented earlier in this chapter, the program should scan the expression and evaluate it. The algorithm is as follows: While you have not reached the end of the string, read the expression from left to right. If the current character is a digit, Push its integer value onto the stack (the integer value of a digit character is its value in the computer’s character set minus the value of '0' in the computer’s character set). Otherwise, if the current character is an operator, Pop the two top elements of the stack into variables x and y. Calculate y operator x. Push the result of the calculation onto the stack. When you reach the end of the string, pop the top value of the stack. This is the result of the postfix expression. [Note: In Step 2…
Min Stack (interesting stack implementation): Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class: MinStack() initializes the stack object. void push(int val) pushes the element val onto the stack. void pop() removes the element on the top of the stack. int top() gets the top element of the stack. int getMin() retrieves the minimum element in the stack. You must implement a solution with O(1) time complexity for each function. Code in Java. A brief explanation of how and why your code works and how it relates to the concepts learned (for example, it uses bitwise operation, stack, or takes into account a word size).
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
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