Purpose

advertisement

1

COMPUTER SCIENCE 2210 - PROJECT 3 - INFIX TO POSTFIX

CONVERSION

PURPOSE

The purpose of this assignment is to get some experience with stacks and the .NET Stack<T> class. This is done in the context of parsing arithmetic expressions. Create a Windows forms application with a splash screen, menu, AboutBox, and other appropriate features. See screenshots at the end for guidance. Do not use ordinary message boxes for anything other than messages. Get the infix expressions from a text file. Use an OpenFileDialog opened to the folder containing your test data files.

INFIX

Infix notation is the standard notation used for arithmetic expressions in C#. To add operands X and Y, the operation (+) is placed between the operands as in X + Y. Rules of precedence are applied to determine in what order an expression with a series of operations such as A + B – C * D / E is evaluated. Parentheses are used when the rules of precedence result in the operations being done in an order that is not intended.

POSTFIX

Postfix notation is an alternative notation that is unambiguous and never requires parentheses. In postfix notation, a binary operator (such as multiplication or subtraction) follows the two operands on which it operates.

For example, the infix expression X + Y is rewritten in postfix notation as X Y +. Some calculators are designed to use this form.

When evaluating a postfix expression, read from left to right. The first operator is applied to the two previous operands, and the result replaces the two operands and the operator. The process is repeated until there are no more operators.

For example, the postfix expression 5 6 7 8 + - * 9 - evaluates as follows

First apply + to 7 8 yielding 15. The new expression is 5 6 15 - * 9 -.

Next apply - to 6 15 yielding -9. The new expression is 5 -9 * 9 -.

Third, apply the * to 5 and -9 yielding -45. The new expression is -45 9 -.

Last apply the final - to -45 and 9 to get -54. This is the result.

Note that the char “-” appears as both a negative sign (unary operator) and as a subtraction (binary) operator. Only the binary operators are processed for our purposes here.

THE TASK

Input a series of lines from a file, one at a time. Each line will contain exactly one arithmetic expression in typical infix notation such as the following.

Alpha = 62.5 * (Gamma – Delta) / 125

The only permissible operators are +, -, *, /, (, ), and =. You may assume that all arithmetic and assignment operators are binary

(operations with two operands). For example, the minus sign always signifies subtraction rather than the indication of a negative value. The rules of precedence are the typical rules of C#. Spaces are irrelevant and your code should not be dependent on their presence or absence.

2

You may assume that the expression is syntactically valid except that you must verify that all parentheses are paired correctly. You may also assume that any token that is not an operator or a blank is a valid variable or constant.

For each line, parse the line and output the expression in the original infix notation and the result in postfix notation. For example, the infix expression above would result in output of the following postfix result.

Alpha 62.5 Gamma Delta - * 125 / =

SPECIFICATIONS

Allow the user to designate the input file to be processed. Format the output for easy readability. The program continues until the end of the input file is processed.

The general approach to solving the problem is as follows. Create and use an Operator class. Assign a numeric precedence to each operator. Some operators may have the same precedence as some others. Operators with higher precedence are “done” before those with lower precedence. Those with the same precedence are “done” from left to right. Parentheses change the rules, with expressions in parentheses being evaluated before those outside (in the left to right processing).

Also create and use a PostFix class with a Convert method that uses the Operator class to help in the conversion of an infix expression to postfix.

A quick overview of the algorithm follows. An output string is initialized to null. As the input string is parsed, variables and constants are concatenated into the output string (separated by spaces). When an open parenthesis “(“ is found, it is placed in an operator

stack. When a closed parenthesis “)” is found, the operator stack is popped until an open parenthesis is found in the stack.

Operators popped off the stack go into the output string except for parentheses, which are discarded.

When an operator other than an open or closed parenthesis is found, the stack is popped until either an operator of lower

precedence is found or until an open parenthesis is found. Then, the new operator is pushed onto the stack.

When the end of the input string is reached, all operators on the stack (except open parentheses) are popped and added to the output string. An error is recognized if an open parenthesis remains in the stack at the end or if a closed parenthesis is found without a corresponding open parenthesis already in the stack.

Make sure the output is clearly readable and understandable. Both the original expression and the resulting postfix notation expression should be displayed and labeled. Clear the screen/form between expressions.

Use the Stack<T> class and its members to manage all stack-related parts of this program. Design issues not specifically discussed here are left to you. Your Utility class from earlier projects may be of use in completing this project.

DELIVERABLES

Follow the rules of submission given on the website. Be sure to direct your submission to both your instructor and your TA. This assignment is to be done individually.

CLASS DIAGRAM

The following class diagram represents one possible solution.

3

SCREENSHOTS

Following are some screenshots of a running version of one solution.

4

Download