EECS 2031 Software Tools, Winter 2014
Lab 4 (lab open during first 30 minutes, then closed in labtest mode) Later: changed to open lab
Cellphones and other electronic devices must be off while you are in the lab.
Background Scenario
This lab uses the same bathroom scale data transmission scenario as
lab2 and lab3.
However, this time you have to adapt your solution for the previous labs so that
your program identifies if the user name for a weight record is new or has been previously
seen.
In the following and for ease of reading, the changes relative to lab3
have been marked below with '***'.
As before, each line of input contains the following information:
timestamp userID weight
There are one more space characters separating the three pieces of information, except that
the userID itself may now contain one or more spaces (as in "1000 John Jack Smith 123.45").
The fields are defined as follows:
- The timestamp is an integer with the number of seconds since 00:00, Jan 1, 1970 UTC,
which conforms to the standard specification of time in Unix/Linux systems.
- The userID is a string, which may contain spaces.
- The weight as a floating point number, specified to mean the weight in kilograms.
This protocol can be parsed deterministically,
as the weight is the first numeric field after a sequence of text fields, e.g. as in
timestamp userIDa userIDb userIDc userIDd weight and none of the parts of
the userID can start with a digit or '.'.
Objective
- Write an ANSI-C program called lab4.c that reads lines from standard input,
parses them, and classifies the input according to the context set out above and the requirements
specified below.
- In the initial open lab, you are welcome to create a draft solution. You need to submit that using the command
submit 2031 pre4 lab4.c
With this, your initial work will be available to you in the closed part of the labtest.
You can submit other text files, such as files with input data, as well.
- Once the labtest starts, your work will be available to you in a subdirectory
called unsubmit in your home directory. This is a read-only location.
So you have to copy these files out first, e.g. via the following command
cp unsubmit/pre4/* ~
- Test that your program correctly implements the required functionality
- Finally, submit your solution electronically before the end of
the lab test using the command
submit 2031 lab4 lab4.c
- You may submit your solution more than once. Additional documentation about the submit
command can be viewed by typing man submit.
Now create a new ANSI-C program that does the following.
Requirements
Your program must the input read line by line from standard input.
- If the timestamp field is missing, is not an integer, or is zero,
you must print Invalid time, followed by a newline character.
- The userID consists of a sequence of text strings each of which cannot start with a
digit or '.' character.
***
If the userID is missing, or if the userID is longer than 179 characters (including single
spaces in between the parts of the userID) you must print
Illegal userID, followed by a newline character.
- If the weight field is missing, or is less than 30.0 or more than 300.0,
you must print Illegal weight, followed by a newline character.
- If the timestamp of a record not larger than the previous timestamp,
i.e., the current is in the past relative to the previous one or identical to it,
you must print Nonmonotonic timestamps, followed by a newline character.
- Additional text on the line after the last field should be silently ignored.
- If there are multiple problems with the line of input, you must print only
the message for the first field that does not follow the specification. Processing should
then continue with the next line. All rules need to be applied in the order specified here.
- Data from lines that pass all above criteria are considered valid records.
-
*** If the information supplied in each line is otherwise fine,
and the userID is new, i.e., has not been seen before, you must print OK newuser
followed by a newline character.
Different amounts of whitespace
between the parts of a userID should be ignored in the test for equality of userID's.
- Otherwise, the userID has been seen before.
If the weight change between the current record and the last valid record
for the same userID exceeds 10 kg/day,
you must print Suspiciously large weight change, followed by a newline character.
Otherwise, you must print OK, followed by a newline character.
- No other output must be produced.
For the purpose of this lab, you do not need to worry about overflow. In other words, you
can safely assume that timestamps are guaranteed to fit in 32 bit integers,
userID's will not be longer than
1000 characters including spaces, and floating point numbers will fit
into a ANSI-C float variable.
You can also safely assume that there is always at least one (or more) space characters
between the fields and between each part of the userID.
Moreover, each line of input is guaranteed to be less than 1024 characters long.
Assuming that the program is started with lab4, and given the following input, which
is also provided for convenience as a file input.txt (file updated):
3600 godzilla's kid 30.0
36000 godzilla 299
36001 godzilla's kid 30.1
36002 godzilla's kid 30.2
46002 godzilla's kid 30.1
46999 godzilla 30.1
60000 john jack andrew wolfgang jiang rami tom bob robert frank richard smith 123.5
your program should create the following output, provided for convenience as
a file expectedoutput.txt:
OK newuser
OK newuser
OK
Suspiciously large weight change
OK
Suspiciously large weight change
OK newuser
Hints:
- Given that there is no specified limit on the number of weight records,
I suggest that you use a linked list to store the userID's.
- If you cannot deal with multi-part userID's then solve the lab first for userID's without
whitespace in them.
- It is usually easier to address each individual requirement to your code only after
you have verified that the previous requirement is met by your program.
- Remember to look up information related to this lab well in advance or (at the very latest)
in the first 30 minutes of the lab.
- Many of the test cases for previous labs are still valid for lab4.
You can use those test cases to test if your submission deals correctly with the
unchanged aspects of the specification.
Additional hints: