lab04 : Evaluating postfix and prefix expressions with stacks

num ready? description assigned due
lab04 true Evaluating postfix and prefix expressions with stacks Thu 02/25 09:00AM Wed 03/03 11:59PM

Goals for this lab

By the time you have completed this lab, you should be able to

Step by Step

Step 0: Getting Started

This lab may be done solo, or in pairs. Before you begin working on the lab, please decide if you will work solo or with a partner. As stated in previous labs, there are a few requirements you must follow if you decide to work with a partner. I will re-iterate them here:

Step 1: Copying some programs from my directory

Visit the following web link—you may want to use “right-click” (or “control-click” on Mac) to bring up a window where you can open this in a new window or tab:

Lab04 Files

You should see a listing of several C++ files. Please copy those files into your local lab04 Github repo directory.

If so, you are ready to move on.

Step 2: Understand the code in given files

In this lab, you will be given three header files (i.e. operators.hpp, postfix.hpp and prefix.hpp). You need to first understand the code to finish the postfix.cpp and prefix.cpp. As you can see from the given files, you should make use of the stacks to implement several methods to evaluate the postfix and prefix expressions and convert between postfix notation and prefix notation. The descriptions of the functionalities of the methods are listed below:

Step 3: Learn the concepts of postfix and prefix expression and implementation with stacks

Infix: We are most accustomed to arithmetic expressions in infix form - number operator number - where the operator is between the two numbers, as in 7 + 5. With this form, it is necessary to consider the precedence of operators and the effects of parentheses, which makes 7 + 5 * 3 different than (7 + 5) * 3, for example.

Postfix: An expression is called the postfix expression if the operator appears in the expression after the operands. Simply of the form (operand1 operand2 operator). The first infix expression above translates to 7 5 + in postfix, the second one translates to 7 5 3 * + and the third one is 7 5 + 3 * in postfix form. Example : AB+CD-* (Infix : (A+B) * (C-D) )

Prefix: An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 operand2).The first infix expression above translates to + 7 5 in postfix, the second one translates to + 7 *5 3 and the third one is * + 7 5 3 in postfix form. Example : *+AB-CD (Infix : (A+B) * (C-D) )

After you understand the basic knowledge of postfix and prefix expressions, you should learn how to implement different algorithms with stacks. We will offer you two algorithms for reference.

Postfix Expression Evaluation Algorithm

Exceptions would be thrown if (a) a token cannot be identified, (b) there are less than two operands on the stack when an operator is encountered, or (c) the stack does not have exactly one operand left on it at the end of the loop.

Algorithm for Converting Prefix to Postfix:

Step 4: Finish the postfix.cpp and prefix.cpp

Now you can try to finish your postfix.cpp and prefix.cpp. Please read the helpful notes as follows and implement all of the methods with stacks.

Some helpful notes:

Step 5: Test Your Code and Upload to Gradescope

The Autograder will use some test cases to check if your implementations are correct. In order to verify them, you’ll need to write your own test code, which can be a ‘main’ function to print out the results, before the submission. Then, write a Makefile to link all dependencies to make and run your test. The lab assignment “Lab04” should appear in your Gradescope dashboard in CS 24. If you haven’t submitted anything for this assignment yet, Gradescope will prompt you to upload your files. For this lab, you will only need to upload your modified files (i.e.postfix.cpp and prefix.cpp). If you already submitted something on Gradescope, it will take you to their “Autograder Results” page. There is a “Resubmit” button on the bottom right that will allow you to update the files for your submission. For this lab, if everything is correct, you’ll see a successful submission passing all of the Autograder tests.

Remember to add your partner to Groups Members for this submission on Gradescope if applicable. At this point, if you worked in a pair, it is a good idea for both partners to log into Gradescope and check if you can see the uploaded files for Lab04.