Use the C++ Standard Template Library's stack class to write a program for processing a file of postfix еxpressions. Recall that in a postfix expression, a binary operator comes after its two operands. Thus the infix expression (w +x) * (-2) becomes wx+ys-* in postfix. A postfix expression can be efficiently evaluated using a stack as follows. 1. Read the expression from the left to the right. 2. When an operand is encountered, push it on the stack. 3. When an operator is encountered, pop the top two elements from the stack, perform the operation, and then push the result. 4. There should be exactly one element on the stack at the end and this element is the value of the еxpreseion. For example, if the expression 20 5-3 * is entered, the evaluation should proceed as follows. Symbol read Actions taken 20 Push 20 Push 5 Роp 5, Рop 20, Рush 20-5 Push 3 3 Pop 3, Pop 15, Push 15 * 3 The result of 45 should then be output. Your program should ask the user for the name of an input file. The input file will contain postfix expressions one per line. The program should open the file and evaluate the postfix expressions in the file. Your program should give the user the option of writing the results to an output file or to the screen. Your program should also check the entered postfix expression for validity. A postfix expression is invalid if there is more than one element on the stack at the end of the evaluation or if ever there are not| enough operands on the stack when an operation is performed. E.g. If the input file contained: The output would be: 20 5 - 3 * 45 100 50 60 + + 210 345 + 3 4 ++ Too few operators. Too many operators. Illegal operation 34 ?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Use the C++ Standard Template Library's stack class to write a program for processing a file of postfix
expressions.
Recall that in a postfix expression, a binary operator comes after its two operands. Thus the infix
expression (w +x) * (v-2) becomes wx+ys-* in postfix.
A postfix expression can be efficiently evaluated using a stack as follows.
1. Read the expression from the left to the right.
2. When an operand is encountered, push it on the stack.
3. When an operator is encountered, pop the top two elements from the stack, perform the
operation, and then push the result.
4. There should be exactly one element on the stack at the end and this element is the value of the
еxpression.
For example, if the expression 20 5 - 3 * is entered, the evaluation should proceed as follows.
Symbol read
Actions taken
20
Push 20
5
Push 5
Pop 5, Pop 20, Push 20 - 5
Push 3
3
Pop 3, Pop 15, Push 15 * 3
The result of 45 should then be output.
Your program should ask the user for the name of an input file. The input file will contain postfix
expressions one per line. The program should open the file and evaluate the postfix expressions in the
file. Your program should give the user the option of writing the results to an output file or to the screen.
Your program should also check the entered postfix expression for validity. A postfix expression is
invalid if there is more than one element on the stack at the end of the evaluation or if ever there are not|
enough operands on the stack when an operation is performed.
E.g. If the input file contained:
The output would be:
20 5 - 3 *
45
100 50 60 + +
210
3 45 +
3 4 + +
3 4 ?
Too few operators.
Too many operators.
Illegal operation
Limit your postfix expression to the arithmetic operations of +, -, *, and / on integer values.
Transcribed Image Text:Use the C++ Standard Template Library's stack class to write a program for processing a file of postfix expressions. Recall that in a postfix expression, a binary operator comes after its two operands. Thus the infix expression (w +x) * (v-2) becomes wx+ys-* in postfix. A postfix expression can be efficiently evaluated using a stack as follows. 1. Read the expression from the left to the right. 2. When an operand is encountered, push it on the stack. 3. When an operator is encountered, pop the top two elements from the stack, perform the operation, and then push the result. 4. There should be exactly one element on the stack at the end and this element is the value of the еxpression. For example, if the expression 20 5 - 3 * is entered, the evaluation should proceed as follows. Symbol read Actions taken 20 Push 20 5 Push 5 Pop 5, Pop 20, Push 20 - 5 Push 3 3 Pop 3, Pop 15, Push 15 * 3 The result of 45 should then be output. Your program should ask the user for the name of an input file. The input file will contain postfix expressions one per line. The program should open the file and evaluate the postfix expressions in the file. Your program should give the user the option of writing the results to an output file or to the screen. Your program should also check the entered postfix expression for validity. A postfix expression is invalid if there is more than one element on the stack at the end of the evaluation or if ever there are not| enough operands on the stack when an operation is performed. E.g. If the input file contained: The output would be: 20 5 - 3 * 45 100 50 60 + + 210 3 45 + 3 4 + + 3 4 ? Too few operators. Too many operators. Illegal operation Limit your postfix expression to the arithmetic operations of +, -, *, and / on integer values.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education