CS 14 - Lab 3
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. Also, be sure that you have read chapter 6 in the book
on stacks (bring your book to lab). Doing the homework problem related to
this lab is also helpful
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.
(If you have gotten this far, you can at least get 7 points for the lab
including your attendance points)
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"
- 2 points - Attendance - Lab attendance will be 20% of the grade for each
lab. You will receive 1 point when the lab begins and 1 point when the lab
ends. Attendance will be taken during the first and last 5 minutes of the
lab period
- 2 points - Coming to lab with the correct postfix evaluation of the
expression in the file (you must show your work and show the stack at each
step of the evaluation). You will turn this in as you enter the lab and
must be turned in during the first 5 minutes of lab. You will do this
individually
- 1 point - Takes filename as command line parameter and uses it
- 2 points - Reads expression from the file and prints it to the screen
- 2 points - Gets correct result and prints it to the screen
- 1 points - Good object oriented programming style
- Deductions:
- -.5 - Not providing error checking on the command line parameters
- -.5 - Not providing error checking on the file
- -.5 - Not providing error checking in the stack class (i.e. checking for
full on push, checking for empty on pop)
- -1 - Using char*
- +1 Extra Credit - Using strstream for conversion