CS140 -- Lab 6


Wed Sep 19 14:52:34 EDT 2007
The makefile for the test files is in http://www.cs.utk.edu/~plank/plank/classes/cs140/Labs/Lab6/makefile.

Executables and test input files are in the directory /home/plank/cs140/Labs/Lab6. As usual, if you have questions about how these programs should work, try these.

This Lab consists of two programs: mydc and histogram. In the first you should use a stack, and in the second you should use a queue.


mydc

Write mydc, which has the following man page:
MYDC(1)                    USER COMMANDS                    MYDC(1)

NAME
     mydc - desk calculator

SYNOPSIS
     mydc 

DESCRIPTION
     mydc is an arithmetic package.  It operates on integers.  The
     overall structure of mydc is a stacking (reverse Polish)
     calculator.  Input comes from standard input.

     The following input constructs are recognized:

  Commands
     number  Push a number onto the  stack.

     +  - /  *  % 
             The top two values on the stack  are:  added  (+),
             subtracted   (-),  multiplied  (*),  divided  (/),
             or remaindered (%).   The  two entries are popped off 
             the stack and the result is pushed in their place.  

     d       Duplicate the top value on the stack.

     p       Print the top value on the stack.  The  top  value
             remains unchanged.

     q       Exit the program.

     P       Pop the top value off the stack (don't print it).

     s       Swap the top two values on the stack.

     c       Clear the stack (i.e. pop off all values)

EXAMPLE

     INPUT: 5 6 + p                 OUTPUT: 11
     INPUT: 3 4 5 - * p 2 - p       OUTPUT: -3
                                            -5
     INPUT: c p                     OUTPUT: main stack: empty
     INPUT: +                       OUTPUT: not enough operands
     INPUT: + 1 2 + p               OUTPUT: not enough operands
                                            3

For hints, see the hints file.

histogram

Write histogram.c. This program takes one command line argument granularity, which is a positive integer, and data on standard input. Every non-blank line of standard input should have an integer as its last word. All of these integers compose the data. You are to print out an ASCII histogram of the data. The histogram groups all of the data into bins. Each bin represents a range of data from i*granularity to (i+1)*granularity-1 for some integer i.

For example, suppose the data is composed of the numbers 3, 3, 4, 5, 5, 6, 7:

Histogram reads the data, figures out the lowest and the highest bins, and then for each potential bin from the highest to the lowest, it prints out the identity of the bin, left-justified and padded to 15 characters, a space, and then an asterisk for each element that is in the bin.

So, the input file Simpex.txt contains the example data above:

3
3
4
5
5
6
Ignore text before the last word, which is the data: 7

Here are a few example calls of histogram:

UNIX> histogram 1 < Simpex.txt
[7]             *
[6]             *
[5]             **
[4]             *
[3]             **
UNIX> histogram 2 < Simpex.txt
[6-7]           **
[4-5]           ***
[2-3]           **
UNIX> histogram 3 < Simpex.txt
[6-8]           **
[3-5]           *****
UNIX> histogram 8 < Simpex.txt
[0-7]           *******
UNIX>
I have some fun data files below: See the hints for some hints on writing this program.