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. Infix Infix notation is the standard notation used for arithmetic expressions in C#. For example, to add operands X and Y, the binary operator (+) 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 to override these rules when the rules of precedence result in the operations being done in an order that is not intended. For example, (A + B – C) * (D / E) would have a different calculated result than A + B – C * D / E which would be evaluated as A + (B – (C * D) / E). 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 it from left to right. The first operator is applied to the two immediately preceding 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 Expression Operation 5 6 7 8 + - * 9- First apply + to 7 8 yielding 15 which replaces those 3 items. The resulting expression is 5 6 15 - * 9 -. 5 6 15 - * 9 - Next apply - to 6 15 yielding -9. The new expression is 5 -9 * 9 -. 5 -9 * 9 - Third, apply the * to 5 and -9 yielding -45. The new expression is -45 9 -. -45 9 - Last, apply the final - to -45 and 9 to get -54. This is the result. Note that the character “-” 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 Using a Windows Form application, input a series of lines from a text 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 1 The only permissible operators are +, -, *, /, (, ), and =. You may assume that all arithmetic and assignment operators are binary (operators 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#. Whitespace characters are irrelevant and your code should not be dependent on their presence or absence. 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 whitespace character is a valid variable or constant. For each line, parse the line and display 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 (using an OpenFileDialog). 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 “evaluated” before those with lower precedence. Those with the same precedence are “processed” from left to right. Parentheses change the rules, with expressions in parentheses being evaluated before those outside (in the left to right processing). 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 postfix string is initialized to null. As the input string is parsed, variables and constants are concatenated into the postfix 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. o 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 textboxes between expressions. 2 Use the Stack<T> class and its members to manage all stack-related parts of this program. The Utility class may be of use in completing this project. The primary window must have its own icon, be centered on the screen, have a menu strip, 3 buttons, and a list box showing all infix expressions read from a text file. When an item is selected in the list box, display that item an infix text box and show its postfix equivalent in a postfix text box. See the screen shots below for one possible solution. The information in the main window’s caption, the splash screen text, and the about box text must come from AssemblyInfo.cs. The splash screen and the about box MUST NOT be ordinary message boxes – they must be derived from Windows.Forms.Form just as the main window is. They must be centered on the screen. The splash screen should be borderless and close itself after a reasonable period of time. Your colors, pictures, icons, etc. may be selected using your creativity to enhance the appearance of the program. Items should be sized, aligned, have appropriate fonts and colors, and so forth to make the window as professional looking as possible. 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 Figure 1 - Class diagram of one possible solution 4 Some Screen Shots Figure 2 – Splash Screen with information from AssemblyInfo.cs Figure 3 - About Box – information comes from AssemblyInfo.cs Figure 4-Empty Main Window with its own icon and highlighting the File menu item on the MenuStrip 5 Figure 5 - Open File Dialog initially displays text files in project's TextFiles subfolder Figure 6 - Infix Expressions have been read from the text file; selected item has been converted to Postfix Figure 7 - When the selection changes, the newly selected expression is displayed in Postfix without clicking the Generate Postfix button 6 Figure 8 - When a new expression is typed directly into the Infix text box and Generate Postfix button is clicked, Postfix conversion is displayed Figure 9 - When unmatched parentheses are entered, the result is an error message Figure 10 - Clear button clears the text boxes but not the list box 7