CSC 245 Evaluating Reverse Polish Notation (RPN) Expressions Lab 2 In this laboratory you will learn how to use a stack abstract data type to evaluate a mathematical expression written in reverse Polish notation (also called postfix notation). This is an individual exercise, so each class member must submit their own work. Preliminaries: For this lab you will need to download the adt_stack.ads and adt_stack.adb package source codes and the rpn.adb program source code from the Web Page and place them in the C:\usr\bin directory. As always be sure that the file extensions are correct. Build Program: Open rpn.adb , build it and then run it. This program asked you to enter values for some variables A, B, C etc. and to enter –999 when you are finished. Enter values for three variables A=1, B=2 and C=3 and then enter –999 for D. Next the program asks you to enter an expression (postfix). To test your implementation of rpn enter the expression AB+C* and press enter. Did you get 9.0 ? If not be sure that A,B and C were entered in upper case. If you continue to have problems get help before you continue. Reverse Polish Notation: RPN or postfix notation is a way of representing mathematical expressions in which the operands (values or variables) come before the operator (+,-,*,/). A stack data type is a convenient way to control the evaluation of a postfix expression. The infix and postfix representations of the expression in the example above are, (A+B)*C infix AB+C* postfix Notice that the postfix expression does not need parentheses to ensure that A+B is computed before the multiplication of C. To change an infix expression into a postfix expression you work from the innermost parentheses moving binary operators to the right of their operands until all operations are in postfix form. Try converting these example infix expressions to postfix form. (A+B)*(C+D)/(E-F) A+B+C+D ((A+B)*C+D)/E A+(B-(C+D*E)) (AB+)*(CD+)/(EF-) (AB+CD+*)/(EF-) AB+CD+*EF-/ We can use a stack to evaluate a postfix expression in the following manner. Scanning from left to right when we encounter an operand we push its value onto a stack. When we encounter a binary (two operand) operator we pop two values off the stack, perform the indicated operation and then push the result back onto the stack. When we are finished we can pop the stack to get the final result. Again using our example we have, AB+C* AB+C* AB+C* AB+C* AB+C* _1_ 2 _1_ _3_ 3 _3_ _9_ push A push B pop B pop A push A+B push C pop C pop A+B push (A+B)*C Review Source Code: Bring up the source code rpn.adb and look at the main executable. The first loop loads values entered by the user into an array called vars( ) Note that the index into this array is type character. So the first value entered is placed in vars(‘A’), the second is placed in vars(‘B’) and so on. This gives us a convenient way to relate the variable names A, B, C etc. to the values we have entered. The second loop in the main executable of rpn asks the user to enter a postfix expression that is loaded into a string called exp. These expressions are passed to the function evaluate(exp,nchr). In this function the expression is scanned character by character and evaluated with the help of a stack S. When a variable (A..Z) is scanned its corresponding value is pushed onto S, when an operator (+ or *) is scanned the top two values in S are popped off and used in the indicated calculation, then the result is pushed back onto S. Notice that the function only handles + and *. Modify this function to handle – (subtraction) and / (division). Once you have modified the function evaluate(exp,nchr) to perform all four math operations (+,*,-,/) test the correctness of your version of this function with several example expressions. Write down your functions and evaluate them by hand to verify code correctness. Infix expression Postfix expression Expected Value Please submit this handout with your modified source code. Computed Value