CS 14 - Lab


CS 14 Homepage

Stacks Using Arrays

In this lab you will learn about the implementation of stacks using arrays. You will also learn how to allow your program to accept command line parameters (a very important thing to know).

Pre-lab Preperation

Please make sure that you are familiar with file I/O before coming to lab this week. You will not finish the lab if you don't understand file I/O. Past students have spend the entire lab just on the file I/O and you are expected to know this before coming to lab. I suggest you prepare ahead of time by doing the file I/O portion of this lab before coming to lab.

Assigned pre-lab work - You must evaluate the postfix expression BEFORE coming to lab. You must write up the solution and show all of your work. You must show the state of the stack at each iteration (just like in the homework). You will turn in a hard copy of this during the first 5 minutes of lab.

Stacks

In this lab you will evaluate postfix expressions using a stack implemented with an array (you may assume that the maximum size your stack needs to be is 25). You will read a postfix expression from a file, exaluate it, and print the result to the screen. So that you can get points for the file I/O without getting the evaluation of the expression correct, please print the postfix expression to the screen as you read it in. Please be aware that your completed program will be reading from the file, printing what it read to the screen, and evaluation the expression all at the same time. The file name will be specified as a command line argument to your program. You may assume that the postfix expression supplied in the file is a valid expression. You should be able to handle any number value (including negative numbers) and the following operators: * / + -. You may assume that if the value read in is not one of the operators listed then it is a number. Numbers will be of arbitrary size but will be able to be stored as an int (in the case of division, assume integer division). You may not use char*'s in this assignment.

HINT - you may want to know what the function atoi ( ... ) does. Do a man on atoi to find out (man atoi). Also, understand what string.c_str() does.

EXTRA CREDIT OPERTUNITY - Alternatively to atoi ( ... ), you can use a strstream. Strstreams are VERY cool and VERY useful. However, to get the extra credit you will have to look online and figure out how to use strstreams on your own. The TAs will not help you with them. Please finish the lab first before attempting to use strstreams.

Object-Oriented Implementation of the Stack

Please remember to use an object oriented programming style for this lab. Your stack must be implemented with a class and your class will contain member functions to access the stack. Remember to use good encapsulation skills. Also remember to provide error checking.

Postfix Expression

Your program must work with this test file. Please look at the postfix expression and evaluate it by hand to determine what the correct output should be.

Suggested Procedure

1. Turn in your postfix evaluation during the first 5 minutes of lab.
2. Get your command line argument to work (simply print the command line argument to the screen and exit).
3. Get the file I/O to work correctly. Read through the file and print the values in the file to the screen.
4. Create your stack and evalute the expression as you are reading in from the file. To check the functionality of your program, you should not start out by testing your program with my long test file. Create you own small test files to make sure the functionality is correct.

Point Breakdown - In Lab Demo

For this assignment, you will demo your program in lab. Your program will read in the file, print the expression to the screen, evaluate the expression, and print the result to the screen. You will show this result to the TA. The TA will also look at your code for correctness when checking out. Remember, to receive credit for this lab, you MUST turn the code in online as well as demo in lab, however, you will only receive points for what you demo to the TA during lab section. If you do not turn in your code online, you will lose points for your lab. All code must be turned in online for archival purposes. Remember to put both partners names on the code that you turn in.

Remeber to compile your code using the flags "-g -Wall -W -Werror -pedantic"