CS140 -- Lab 4


This lab will give you practice using linked lists and unions. Files that you will need:


Part 1: linenum

Write a program called linenum that takes a filename and an arbitrary number of words from the command line and then prints the line numbers from the file on which each of these strings appears. You must use the fields library to read each line of input. You should use a dynamically allocated array of lists to keep track of the list of line numbers for each word. Each line of output should consist of a word and then the line numbers on which it appears. Line numbers should not be duplicated. The words should be printed in the order in which they appear on the command line. For example, if the file brad contains the lines:
ice cream is nice
but chocolate ice
cream is dandy
then the command:
<UNIX> linenum brad cream nice ice Nice
should produce the output:
cream 1 3
nice 1
ice 1 2
Nice
The words on the command line may be duplicated (e.g., ice may appear twice) but it is okay to only print that word once in your output. My executable will duplicate the output for that word, which is also acceptable.


Testing Suggestions

I have provided one test file in the lab4 directory, linenum_test1 to test your program. However, you should test your program for boundary-type conditions and command line issues, such as:

  1. An empty file
  2. An extremely large file
  3. Files that have lines with duplicate words
  4. Command lines with duplicate words
  5. Files that have lines with duplicate words but the words are in different case (e.g., nice, Nice, and NICE).
  6. Command lines with duplicate words but the words are in different case
  7. Command lines with too few arguments
  8. Filenames that do not exist

Part 2: broker

Write a program called broker that reads stock and option transactions from a file, groups them by customer, and then prints out a report showing the total dollar amount of transactions for each customer and a list of that customer's transactions, printed in the order in which they were read. The customers will be printed in descending order based on the total value of their transactions.

input

The input will consist of records of two types:

  1. stock transaction: A stock transaction has the format:
    stock ticker_symbol num_shares price_per_share dividend_amt customer_name
    
    stock is a keyword that identifies the type of transaction. ticker_symbol is a 3-5 character string that is used by traders as a shorthand symbol for the stock's name, num_shares is an integer expressing the number of shares involved in the transaction, and price_per_share is a floating point number denoting the price paid for each share. The dividend_amt is the amount of dividends the company pays on each share of stock. Finally the customer's name is a character string consisting of one or more words. The fields library will represent these words as separate fields so you will have to concatenate them together and put a single space between each word. A sample stock line might be:
    stock ibm 200 83.50 2.50 Brad Vander Zanden
    
  2. option transaction: A single option gives you the right to buy 100 shares of a stock at a specific price, called the strike price, until a specific date, called the expiration date. An option record will have the following format:
    option ticker_symbol num_options price_per_share strike_price expiration_date customer_name
    
    option option is a keyword indicating the transaction type. ticker_symbol is a character string of 3-5 characters representing the option's trading symbol. num_options is an integer denoting the number of options purchased and price_per_share is a floating point number denoting the price paid for each share covered by an option. Since each option represents 100 shares the total transaction cost is determined by the following equation:
    total transaction cost = price_per_share * 100 * num_options
    
    The strike price is a floating point number and the expiration date has the format mm yyyy where mm is an integer between 1 and 12 and yyyy is a year. Finally the customer name is similar to the customer name for a stock. A sample input line for an option might appear as follows:
    option oibm 3 5.50 85 12 2004 Brad Vander Zanden
    
    This line says that Brad Vander Zanden has bought 3 calls on ibm at $5.50 a share. The call gives the customer the right to purchase the stock at $85.00 a share until December, 2004.

Writing the Program

Your program should create a dlist that contains customer records. Each customer record will contain the customer's name, the customer's total transaction costs and a dlist that consists of all of the customer's transactions. You will need to define two structs, one for a customer record and one for a transaction record. A transaction record needs to store two types of transactions, stock transactions and option transactions. Stock and option transactions share some common information but each transaction also has some unique fields. Hence you will need to employ a union within your transaction struct that can represent the alternative information required for each type of transaction. Your solution must employ a union.

Once you have read all the transactions and organized them by customer, you should create a new dlist that will contain the customers sorted in descending order by total transaction cost. You will need to iterate through your initial customer list and add each customer to your new dlist using insertion sort.

Printing the Report

The final thing you need to do is print the report. Each customer should start with a line that lists the customer's name and total transaction costs. The customer's name should be left justified in a field of 20 characters and the total transaction costs should be right-justified in a field that allows two decimal points. The customer line should be followed by indented lines that list the customer's transactions, one per line. Transaction lines should be indented by 4 spaces. Stock transaction lines should have fields in the following order and should be separated by one space:

Option transaction lines should have fields in the following order and should be separated by one space:

If you have questions about how the output should appear, consult the sample executable.

Helpful Functions and Files

  1. parse.c defines two functions named parse_stock_fields and parse_option_fields that will 1) type check the fields for a stock or option transaction, 2) print appropriate error messages for any bad data, 3) convert the fields denoting numeric values to the appropriate type and store them in the named parameters, and 4) return an error code to denote whether or not the line of input is valid.

  2. broker.h provides extern declarations for the two functions named above. You must complete this file with declarations for your customer and transaction structs.


What to Submit

Submit two source files named linenum.c and broker.c respectively and a header file named broker.h.