Implement the algorithm for evaluating postfix expressions

Assignment Help C/C++ Programming
Reference no: EM131137864

Programming Problem C++:

Write a program to implement the algorithm for evaluating postfix expressions that involve only singledigit integers and the integer operations: +, - , *, and /. To trace the action of postfix evaluation, display each token as it is encountered, and display the action of each stack operation.

Your program should output an error message if the postfix expression is not well formed. I.e. not correct. For example given the expression 9 2 1 + / 4 * the output of your program should resemble the following: Token = 9 Push 9 Token = 2 Push 2 Token = 1 Push 1 Token = + Pop 1 Pop 2 Push 3 Token = / Pop 3 Pop 9 Push 3 Token = 4 Push 4 Token = * Pop 4 Pop 3 Push 12 Token = Pop 12 The algorithm for evaluating postfix expressions is given below. ALGORITHM TO EVALUATE POSTFIX EXPRESSIONS Each time an operand is encountered, it is pushed onto the stack.

When an operator is encountered, the top two values are popped from the stack, the operator applied to them, and the result pushed back onto the stack.

The following algorithm summarizes this procedure:

1. Initialize an empty stack

2. Repeat the following until the end of the expression is encountered: a) Get the next token (character, variable, arithmetic operator) in the postfix expression.

b) If token is an operand, push it onto the stack. If it is an operator, do the following:

i) Retrieve and pop the top two values from the operand stack. If the operand stack does not contain two items, an error due to a malformed postfix expression has occurred and evaluation is terminated.

ii) Apply the operator token to these two values. iii) Push the resulting value back onto the operand stack.

3. When the end of the expression is encountered, its value is on top of the stack (and in fact must be the only value in the stack).

Reference no: EM131137864

Questions Cloud

A random sample from a normal population : Let X1 and X2 constitute a random sample from a normal population with σ2 = 1. If the null hypothesis μ = μ0 is to be rejected in favor of the alternative hypothesis μ = μ1 > μ0 when x > μ0 + 1, what is the size of the critical region?
Determine the degree of saturation at the maximum dry unit : Determine the maximum dry unit weight and the optimum water content.
Explain the elasticity of demand for products : Analyze the elasticity of demand for products within the selected industry relevant to Katrina's Candies. Determine the factors involved in making decisions about pricing these products that you believe to be the most influential.
A single observation of a random variable : A single observation of a random variable having a uniform density with α = 0 is used to test the null hypothesis β = β0 against the alternative hypothesis β = β0 + 2.
Implement the algorithm for evaluating postfix expressions : Write a program to implement the algorithm for evaluating postfix expressions that involve only singledigit integers and the integer operations.
Pearson lemma yields the critical region : A random sample of size n from a normal population with σ2 = 1 is to be used to test the null hypothesis μ = μ0against the alternative hypothesis μ = μ1, where μ1 > μ0. Use the Ney man-Pearson lemma to find the most powerful critical region of size α..
What are the total estimated manufacturing costs : Amity Plastic Manufacturing is a small plastic products manufacturer. The company uses machine-hours as the single, plant-wide predetermined cost driver rate to allocate manufacturing support costs to the various jobs contracted during the year, W..
Can ticket scalping be justified : Ticket scalping is common for popular games. Can ticket "scalping" be justified? Post opinion using aggregate demand and supply analysis.
Use the ney man-pearson lemma to indicate : Use the Ney man-Pearson lemma to indicate how to construct the most powerful critical region of size α to test the null hypothesis θ = θ0, where θ is the parameter of a binomial distribution with a given value of n, against the alternative hypoth..

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Prepare a set of non-functional requirements

Prepare a set of non-functional requirements, including operational, performance security, and cultural and political for the fore mentioned scenario:

  Consider implementing subnetting to support three department

Consider implementing subnetting to support three departments within an organization. The three departments P, Q and R need support for 30, 40 and 60 hosts.

  Set of integer numbers and another single number

Write an algorithm called locate (with running time log n, that accept a set of integer numbers and another single number and finds out whether the number is found in the set of integers. If it exist, it should print its index in the list otherwis..

  C++ program for food purchases at the baseball stadium

Write a menu-driven C++ program for food purchases at the baseball stadium's restaurant. The main program will loop allowing the user to select food items from the menu adding each item to the bill, terminating when the user selects the "End order" i..

  What system or application you wish to develop motivations

A project proposal (1-2 pages) outlines what system or application you wish to develop, the motivations or aims, user requirements or required functions, system structures and a rough plan. What you need to submit is a standalone Word or PDF file...

  Write another main function create three bank accounts

Write another main function - in this function create three bank accounts, then prompt the user to enter a bank account number, if the account number matches one on the three accounts then display the following menu otherwise allow the user to ent..

  Write a program to simulate lines in a grocery store

Write a program to simulate lines in a grocery store. We want to determine what line configuration is best

  Amounts of production time and paper

A publishing house publishes three weekly magazines-daily life, agriculture today, and surf's up. Publication of one issue of each of the magazines requires the following amounts of production time and paper:

  Design a system using a class called books

Design a system using a class called Books with suitable members

  Write a program that will read in 4 test scores

Write a program that will read in a list of numbers and double each number. Your program should work for any number of lines of data.

  Define the purpose of a pointer with a example

How does ASP.NET extend the capability of using C++ as a programming language to create dynamic websites? Is C++ the preferred programming language for developing ASP.NET pages?

  Write a program that convert fahrenheit to degrees celsius

Write a program that will convert Degrees Fahrenheit to Degrees Celsius.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd