Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a c++ program that accepts a string that is a space-delimited integer arithmetic expression in postfix notation. Use a stack to compute the value.

Write a c++ program that accepts a string that is a space-delimited integer arithmetic expression in postfix notation. Use a stack to compute the value. Example: The input string 5 4 + 3 10 * + is equivalent to the infix expression (5 + 4) + (3 * 10) The answer is 39. The algorithm is: Take the leftmost token from the string If it is numeric (which may include a unary operator), push that token onto the stack If it is a binary operation, pop the vector twice, apply the operation, and push the result When the input string is empty, the stack will contain one entry: the final answer Heres how the given string gets processed: String Stack 5 4 + 3 10 * + 4 + 3 10 * + 5 + 3 10 * + a 4 5 3 10 * + 9 10 * + 3 9 * + 10 3 9 + 30 9 39 The final answer, 39, is the only element in the stack after all tokens in the string have been consumed. This problem has an extra twist. The string contains characters, so 10 isnt a ten, but is the char 1 followed by the char 0. You will need to convert string representations of integers (signed and unsigned) into their integer values. Youll also need to handle unary - and +. We also have to worry about the three non-commuting operators / % . We will evaluate the postfix string 4 5 - as 4-5 and, likewise, will evaluate 4 5 / as 4/5 and 4 5 % as 4%5. The program should continue to accept and process input strings until the user enters a zero as input. Theres no need to check for validity; all input will be well-formed postfix expressions.

I do not need the program to ask for a users input, instead i need it modified to read a text file line at a time

Modify your postfix calculator to read entries from a file, one line at a time. The expressions in this file are guaranteed to be valid, so no error checking is necessary. Process each expression and keep a running total of the sum. Enter the sum to complete this quiz. Use int data type

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions

Question

Explain how analytics can be used for workplace redesign.

Answered: 1 week ago

Question

8. Provide recommendations for how to manage knowledge.

Answered: 1 week ago

Question

5. Develop a self-management module for a training program.

Answered: 1 week ago