Question
Visual C# 2012 How to Program From the book, page 812. Exercise 19.6, 19.7, with a twist. 19.6 (Evaluating Expression s with a Stack) Stacks
Visual C# 2012 How to Program
From the book, page 812. Exercise 19.6, 19.7, with a twist.
19.6 (Evaluating Expression s with a Stack) Stacks are used by compilers to evaluate expressions and generate machine-language code. In this and the next exercise, we investigate how compilers evaluate arithmetic expressions consisting of constants, operators and parentheses.
Humans generally write expressions like 3 + 4 and 7 / 9, in which the operator (+ or / here) is written between its operands - this is called infix notation. Computers "prefer" postfix notation, in which the operator is written to the right of its two operands. The preceding infix expressions would appear in postfix notation as 3 4 + and 7 9 /, respectively.
To evaluate a complex infix expression, a compiler would first convert the expression to postfix notation, then evaluate the postfix version of the expression. Each of these algorithms requires only a single left-to-right pass of the expression. Each algorithm uses a stack object in support of its operation, and in each algorithm the stack is used for a different purpose. Here, you'll implement the infix-to-postfix conversion algorithm. In the next exercise, you'll implement the postfix-expression evaluation algorithm.
Write class InfixToPostfixConverter to convert an ordinary infix arithmetic expression (assume a valid expression is entered), with single-digit integers, such as: (6 + 2) * 5 - 8 / 4 to a postfix expression. The postfix version of the preceding infix expression is 6 2 + 5 * 8 4 / -
The program should read the expression into StringBuilder infix, then use generic class Stack (page 826, Figure 20.5) to help create the postfix expression in StringBuilder postfix. The algorithm for creating a postfix expression is as follows:
Push a left parenthesis '(' on the stack
Append a right parenthesis ')' the the end of infix
While the stack is not empty, read infix from left to right and do the following:
If the current character in infix is a digit, append it to postfix
If the current character in infix is a left parenthesis, push it onto the stack
If the current character in infix is an operator:
Pop operators (if there are any) at the top of the stack while they have equal or higher precedence than the current operator, and append the popped operators to postfix
Push the current character in infix onto the stack
If the current character in infix is a right parenthesis:
Pop the operators from the top of the stack and append them to postfix until a left parenthesis is at the top of the stack.
Pop (and discard) the left parenthesis from the stack.
The following arithmetic operations are allowed in an expression:
+ addition
- subtraction
* multiplication
/ division
^ exponentation
% modulus
Some of the methods you may want to provide in your program follow:
Method ConvertToPostfix, which converts the infix expression to postfix notation
Method IsOperator, which determines whether c is an operator
Method Precedence, which determines whether the precedence of operator1 (from the infix expression) is less than, or equal to or greater than the precedence of operator2 (from the stack). The method returns true if operator1 has lower precedence than or equal precedence to operator2. Otherwise, false is returned.
19.7 (Evaluating a Postfix Expression with a Stack) Write class PostfixEvaluator, which evaluates a postfix expression (assume it is valid) such as: 6 2 + 5 * 8 4 / -
The program should read a postfix expression consisting of digits and operators into a StringBuilder. Using the stack class from Exercise 19.6 (above), the program should scan the expression and evaluate it. The algorithm (for single-digit numbers) is as follows:
Append a right parenthesis ')' to the end of the postfix expression. When the right-parenthesis is encountered, no further processing is necessary.
When the right-parenthesis character has not been encountered, read the expression from left to right. If the current character is a digit do the following:
Push its integer value on the stack (the integer value of a digit character is its value in the computer's character language set minus the value of '0' in Unicode).
Otherwise, if the current character is an operator:
Pop the two top elements of the stack into variables x and y.
Calculate y operator x
Push the result of the calculation onto the stack.
When the right parenthesis is encountered in the expression pop the top value of the stack. This is the result of the postfix expression.
[Note: In part B above (based on the sample expression at the beginning of this exercise), if the operator is '/', the top of the stack is 4 and the next element is 8, then pop 4 into x, pop 8 into y, evaluate y / x and push the result 2 back on the stack. This note also applies to operator '-'].
You may want to provide the following methods:
Method EvaluatePostfixExpression, which evaluates the postfix expression
Method Calculate, which evaluates the expression op1 operator op2.
Once you have both classes written, write a test program to supply the sample infix and postfix formulas, evaluate them, and display the final result.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started