Write a program that accepts a string that is a space-delimited integer arithmetic expression in postfix...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a 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 Here's how the given string gets processed: String Stack 5 4 + 3 10 * <empty> 4 + 3 10 * + 3 10 5 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 isn't 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. You'll 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. There's no need to check for validity; all input will be well-formed postfix expressions. Submit your .cpp source code and a screen shot of your program in action. +++ Write a 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 Here's how the given string gets processed: String Stack 5 4 + 3 10 * <empty> 4 + 3 10 * + 3 10 5 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 isn't 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. You'll 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. There's no need to check for validity; all input will be well-formed postfix expressions. Submit your .cpp source code and a screen shot of your program in action. +++
Expert Answer:
Answer rating: 100% (QA)
Code include using namespace std int main string str cout Enter String getline ... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
Write a program that accepts a string as input, then prints out the vowels in that string and then prints the consonants in that string. >>> splitem("elephant") Vowels: eea Consonants: lphnt
-
Write a program that accepts a string as input, then prints out the vowels in that string and then prints the consonants in that string. >>>splitem("elephant")Vowels: eeaConsonants: Iphnt
-
Program Written in C++ Sample Input / Output Write a program that computes statistical information on an array (you can only use 'sqrt' from 'math.h'). Enter N : 10 Enter data : 1 2 3 4.5 5.5 5.5 6.5...
-
Start your VM and open a terminal window (note: you may always open more than one terminal window if desired). For this laboratory exam, we will be using the network topology shown in Figure 1....
-
In teams of three or four, interview several local businesses. Ask them how they maintain a safe and healthful workplace.
-
(a) Show directly that Eqs. 9.197 satisfy Maxwell's equations (9.177) and the boundary conditions 9.175. (b) Find the charge density, (z, t), and the current, I(z, t), on the inner conductor.
-
(a) Is a spring that has a large spring constant \(k\) stiffer or softer than a spring that has a small spring constant? (b) Which has a larger spring constant: steel or foam rubber?
-
EZ-Credit, Inc., has $80 million in consumer loans with an average interest rate of 13.5 percent. The bank also has $64 million in home equity loans with an average interest rate of 9 percent....
-
a) Explain the following accounting concepts: i. Business entity Revenue realization ii. Accruals iii. iv. V. Substance over form Conservatism vi. Going concern (6 Marks) b) Classify the following...
-
There is an array A made of N integers. Your task is to choose as many integers from A as possible so that, when they are put in ascending order, all of the differences between all pairs of...
-
You are evaluating the purchase of either of two machines used in your brewery. Machine A will last two years, costs $8,000 initially, and then $1,250 per year in maintenance costs. Machine B costs...
-
At the top of a cliff 100 m high, Raoul throws a rock upward with velocity 15.0 m/s. How much later should he drop a second rock from rest so both rocks arrive simultaneously at the bottom of the...
-
Consider the grid of letters that represent points below, which is formed by repeating the bold 3x3 grid. (The points are labeled by the letters A through 1.)
-
Assets Current Assets: Cash and cash equivalents Accounts Receivable, net Inventory Total Current Assets Property & Equipment at cost Less Accumulated depreciation Net Property & Equipment Total...
-
Find lim h-0 f(2+ h)-f(2) h if f(x) = x+2. f(2+ h)-f(2) lim (Simplify your answer.) h h-0
-
Pari bought $2,500 of new furniture on credit. Because her credit score isn't very good, the store is charging her a fairly high interest rate of 16% to finance this purchase. If she agreed to pay...
-
The Faculty of Computer and Mathematical Sciences are going to organize an ICT Festival. The festival consists of exhibitions of ICT products from the government and private sectors. Meanwhile, for...
-
The cost curve for the city water supply is C(Q) = 16 + 1/4 Q2, where Q is the amount of water supplied and C(Q) is the cost of providing Q acre-feet of water. (An acre-foot is the amount of water...
-
a. The Rochester Bank maintains customer records in a random access file. Write an application that creates 10,000 blank records and then allows the user to enter customer account information,...
-
Create a guessing game called Does It Have Legs?, in which you think of an animal and the computer tries to guess what it is. The game continually becomes smarter as you play because you teach it how...
-
The path class getfilename() method returns ____________. A. The string representation of a path B. An absolute path C. The first item in a paths list of name elements D. The last item in a paths...
-
Are allocations \(a\) and \(g\) in Figure 10.4 part of the contract curve? Data From Figure 10.4:- 60 80 50 160 30 12. 20 20 Jane's wood 40 40 Denise's candy Od 20 Contract curve Denise's wood 20 218...
-
People who qualify for affordable housing in Sydney, Australia, have their rent capped at \(30 \%\) of their gross annual income. People who live in private rental properties in the city are not...
-
Governments use wage subsidies to maintain employment in times of economic recession, to encourage firms to hire and train specific groups of workers such as low-income workers, women, and students...
Study smarter with the SolutionInn App