EECS 2031 Software Tools, Winter 2014
Lab 2 (open lab, not in labtest mode)
Cellphones and other electronic devices must be off while you are in the lab.
Background Scenario
One common usage scenario for smartphones is to monitor the status of the user as it
changes over time. Imagine that every time you weigh yourself, the data is stored on
the "smart" bathroom scale. Using Bluetooth technology, the scale can the then transfer
all recent measurements to your smartphone, whenever it is on and the smartphone is in range.
That enables the smartphone to show a graph of weight changes over time and
other visualizations to the user.
In this lab, we are going to implement a simple parser that deals with a set of data
coming from such a smart scale. Such data could be transferred in a simple text format.
Here we will deal with such input, consisting of individual measurement record,
one per line. Each line contains the following information:
timestamp userID weight
There is a single Space character separating the 3 pieces of information. 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 The userID is a string, which conforms to the rules for the naming of C variables. It will be at most 31 characters long.
- The weight as a floating point number, specified to mean the weight in kilograms.
Objective
- Write an ANSI-C program called lab2.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.
- Test that your program correctly implements the required functionality
- Submit your solution electronically before the end of the lab session using the command
submit 2031 lab2 lab2.c
- You may submit your solution more than once. Additional documentation about the submit
command can be viewed by typing man submit.
What to do
First, create a new directory for this lab
- after logging in, change your subdirectory to your directory for the class with
cd ~/2031. The '~' here refers to your home directory.
- create a subdirectory called "lab2" in 2031
- change the current directory to the newly created directory with cd lab2
- if you print the current directory with pwd, the system should show you now something like /cse/home/ZZZcseZZZ/2031/lab2 (with your CSE account name instead of ZZZcseZZZ).
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.
- If the first character of userid does not conform to the rules for C variable names or
if the fields is missing, 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.
- If the weight change between two records that fit all above criteria exceeds 10 kg/day,
you must print Suspiciously large weight change, followed by a newline character. Added: You are welcome to ignore the userID for this test.
- If the information supplied in each line is otherwise fine,
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
31 characters, and floating point numbers will fit into a ANSI-C float variable.
You can also safely assume that there is always a single space characters between the fields.
Moreover, each line of input is guaranteed to be less than 99 characters long.
Assuming that the program is started with lab2, and given the following input, which
is also provided for convenience as a file input.txt:
Testing 234 234.153
1235 Mega0123test -x-0.5
3600 godzilla 300
36000 godzilla 299
36001 godzilla 200
1000 innocent 69
your program should create the following output:
Invalid time
Illegal weight
OK
OK
Suspiciously large weight change
Nonmonotonic timestamps
Hints:
- Please do not use scanf() directly to parse the input. Instead read the
input line by line and then use the string-parsing version of scanf(), sscanf(), to
read the data.
- Added: the scanf() family of functions returns the number
of tokens/parts of the input that it was able to parse correctly. Using this fact
will greatly simplify your code.
- 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.
- Added: note that you need to test the rate of change (kg/day)
and not just the change (kg) itself.