CS302 -- Lab 1


Lab Objective

The objective of this lab is several fold:

  1. to give you experience using Dr. Plank's fields, jval, and dllist libraries.
  2. to brush up on various C skills you should have learned in CS140, including string handling, malloc, and sscanf.
  3. to gain some experience in creating a rudimentary include file.
  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 name of the individual. The name of the individual can consist of an arbitrary number of words. A multi-word last name is denoted with braces (<>).


include file

Write an include file, transactions.h, that contains a typedef declaration for a struct that will contain the information for a single transaction. Use this include file for all of your programs. Since the struct you declare must include information for more than one program it will be bigger than if you targeted the struct for just one program. Do not worry about that.


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 dlist 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 dlist to keep track of the transactions and should insert transactions into this dlist based on the 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 doubly linked list to hold the stocks and each stock must hold a doubly linked list containing its transactions. You will lose points if you use only one doubly linked 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


What To Hand In

You should submit the following files:

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