CS302 -- Lab 2


Lab Objective

The objective of this lab is several fold:

  1. to introduce you the C++ Standard Template Library (STL), specifically lists.
  2. to introduce you to C++'s stream classes.
  3. to brush up on new C++ skills you should have learned, including string handling and the new operator.
  4. to gain some experience in creating a rudimentary make file.


Data File

You will be using a file that contains stock transactions of the type found in transactions. Each line of the file contains a transaction type, a stock symbol, the number of shares bought or sold, the price per share, the date of the transaction in month, day, year format, and the first and last names of the individual. The first and last names will be separated by a pipe character (|) which has at least one space both before and after it. Both first and last names can consist of an arbitary number of words. For example:

Bobbi Sue | Van der Zanden
You may assume that the input is error free. In other words, you will not be required to perform error checking in this lab.


include file

Write an include file, transactions.h, that contains a class declaration for a single stock transaction. Use this include file for all of your programs. The transaction class will be used as a record so you can declare the fields public if you wish.


avg

Write a program called avg.c that prints the total cost of each transaction and then prints the average cost of all the transactions. Read the input from stdin. Each transaction should have its own line of output and each line of output should list the stock symbol, the quantity, the amount, and the total transaction cost. The total transaction cost should be rounded to two decimal places and should have the format xxxxxx.xx. In other words the total transaction cost should occupy a minimum of 9 character spaces. The format of a line should be:

xxxx    xxxx    xxx.xx    xxxxxx.xx

The x's denote the minimum number of characters that each field should contain. The stock symbols should be left-justified and the remaining fields should be right-justified. The final line should have a blank line between it and the preceding transactions and should have the format:

average transaction cost = $xxxxxx.xx
Your program may use a counter to count the number of transactions.


name

Write a program named name.c that prints each name in the file on a separate line using the format:

lastname, firstname 
The firstname may include a middle name or initial. No name should be printed more than once. You should maintain a C++ STL list to keep track of the names your program has printed thus far. Read the input from stdin.


sort_date

Write a program named sort_date that sorts the transactions in ascending order according to the date of the transaction. Earlier years should precede later years. The transactions should be printed out in the same format in which they were read. You should use a STL list to keep track of the transactions and should sort this list based on each transaction's date. The user should provide the filename as a command line argument. You should print out an error message if the user fails to provide a command line argument.


stock

Write a program that sorts the transactions in alphabetical order by stock name. For each stock you should first print a line that contains the stock's name and the average transaction cost for that stock. The line number is the line that the transaction occurred upon in the original input file. You should then print the transactions for that stock using the following format:

    line_number mm/dd/yyyy    quantity  price  name

The precise format for your output should be:

xxxx    average transaction = $xxxxxx.xx
    xx    xx/xx/xxxx    xxxx    xxx.xx    xxxxxxxxxxxxxxxxxxxx
    xx    xx/xx/xxxx    xxxx    xxx.xx    xxxxxxxxxxxxxxxxxxxx
         ....

xxxx    average transaction = $xxxxxx.xx
    xx    xx/xx/xxxx    xxxx    xxx.xx    xxxxxxxxxxxxxxxxxxxx
    xx    xx/xx/xxxx    xxxx    xxx.xx    xxxxxxxxxxxxxxxxxxxx
         ....
There should be a blank line between each set of stock transactions.

You must use a STL list to hold the stocks and each stock must hold a STL list containing its transactions. You will lose points if you use only one list.

The user should provide the filename as a command line argument. You should print out an error message if the user fails to provide a command line argument.


Additional Information


Makefile

You should prepare a makefile that permits the TAs to individually or collectively create executables for each of the programs in this lab. The following link, makefiles, prepared by Professor James Plank provides excellent guidance on how to create a makefile. The TAs will also provide a lecture on makefiles during lab hours on Monday.

What To Hand In

You should submit the following files:

  1. avg.cpp
  2. name.cpp
  3. sort_date.cpp
  4. stock.cpp
  5. transaction.h
  6. makefile
Remember, you submit labs using the special script discussed on the TA web site.