STARTING OUT WITH C++ MPL
STARTING OUT WITH C++ MPL
9th Edition
ISBN: 9780136673989
Author: GADDIS
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
Python Language - Bad Luck Numbers
C++ code Screenshot and output is must
C Programming Language Task: Deviation Write a program that prompts the user to enter N numbers and calculates which of the numbers has the largest deviation from the average of all numbers. You program should first prompt the user to enter how many numbers that will specify. The program should then scan for each number, separated by a newline. You should calculate the average value and return the number from the list which is furthest away from this average (to 2dp). Try using dynamic memory functions to store the incoming array of numbers on the heap. Code to build from: + 1 #include 2 #include 3 4 int main(void) { 5 6} 7 Output Example: deviation.c How many numbers? 5 Enter them: 1.0 2.0 6.0 3.0 4.0 Average: 3.20 Largest deviation from average: 6.00
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