CS140 -- Lab 6 -- hints
Wed Sep 19 14:42:56 EDT 2007
mydc
Whenever you are writing largish programs, you should try to write
and test them incrementally. Mydc lends itself well
to this. After each step below, make sure that you test the program
to make sure it does what you think it does.
-
Write the main body of the program that reads standard input
into an IS and then processes each word individually.
You will have one Stack, which is going to hold integers.
For each word, have a big 'ol if statement that
figures out what to do. First, test to see if the word is
a number. If so, push it on the stack.
Test this so far.
-
Now implement p and P. Make sure you deal with an
empty stack when implementing P (the program should not exit --
it should merely print that the stack is empty. Test.
-
Now implement q, d, c and s. All of these are
straightforward. Again, test.
- Finally, implement the arithmetic operations. With these and
with s, you'll have to deal with what happens if the stack
is empty or just has one element.
-
Test more.
histogram
Histogram uses a Queue and an IS. You should
first read standard input, putting each value of data into the Queue,
and at the same time keeping track of the maximum and minimum values.
When you are done with this, you should be able to calculate the maximum
and minimum bins and the total number of bins, tbins = (max-min+1). Allocate
an array that has tbins integers. The 0-th element of this array will contain
the number of data elements in the smallest bin. The tbin-1-th element will
contain the number of data elements in the largest bin. Traverse the queue
and for each element, add one to the element's bin.
Finally, traverse the array from top to bottom and print the output. Note, you
will need to do a sprintf() to create the string containing the range
of the bin, and then you will print that with printf().
Again, write this incrementally and test, test, test, test.