IUP Computer Science
COSC 310   Fall 2002
 

Project #2
(Due 27 Sep 2002)

        For this project, you are required to use at least three classes of objects:  a class called BadNews to be used to handle exceptions, a class called FlatElement to be used to represent flat things, and a class called Pile which is a container for FlatElements with restricted actions.  The overall goal is to produce a program that is able to accept input specifications of Piles of FlatElements and to Standardize and display the resulting Pile.  The following Interfaces for the classes are provided to you.  You are to write the member functions for the classes and then write a main and a Standardize function to interact with a user at the keyboard.  You may use in-line functions for the constructors, if you wish.

class BadNews
{
public:
     char     bad;                    // Invalid character from an input stream
     BadNews(char );           // Constructor to be used in exception handling
};

        The user at the keyboard is expected to specify the contents of a Pile of FlatElements by entering a string of characters in which each character represents a particular kind of FlatElement.   If your program were to deal with plastic container lids as FlatElements, a margarine lid might be represented by 'm', a nut container lid by 'n', a cottage cheese lid by 'c', vanilla ice cream by a 'v', etc.  So, the entry of the string  cnvmn  represents lids from cottage cheese, nuts, vanilla ice cream, margarine, and a second nuts lid (the cottage cheese lid is on top of the Pile).  An exception should be thrown if an input string contains an inappropriate character, something other that the characters representing particular the FlatElements being used.

class FlatElement
{
private:
     char         symbol;                        // Input Character that represents the FlatElement
     string       name;                          // Name of the FlatElement
     int             relativeSize;               // Represents the size of this element relative to others
public:
     FlatElement(void);                        // Default Constructor
     FlatElement(char , char *, int );    // Constructor with values to initialize the data
     friend class Pile;                           // Allows Pile member functions to access private data
};

FlatElements are things that are relatively flat, for example, coins, books, plastic lids, pancakes, boards, sheets of paper.  FlatElements can be placed one on top of another to form Piles of FlatElements.  Each has an input representation, symbol, an output representation, name, and a relativeSize.  The relativeSize is based on the diameter of the FlatElements; in the case of plastic lids, margarine lids might be size 1, icing container lids size 2, cottage cheese lids size 3, and so on.

class Pile
{
private:
     FlatElement      store[100];                // Array of FlatElements
     int                       size;                          // Number of FlatElements in the Pile
public:
     Pile (void);                                            // Default constructor
     void Input(void);                                    // Reads in a string of characters and builds a
                                                                    // Pile based on the symbols specified
     int Hooofe (const FlatElement &);      // Returns the position of the Highest Out Of Order
                                                                    // FlatElement of the specified FlatElement type
     int Looofe (const FlatElement &);       // Returns the position of the Lowest Out Of Order
                                                                    // FlatElement of the specified FlatElement type
     int LowestSmaller (const FlatElement &);    // Returns the array position of the Lowest
                                                                    // FlatElement Smaller than the specified one
     void Add (const Pile &);                      // Puts the specified Pile on top of the Pile object
     void Flip (int);                                       // Reverses the order of the FlatElements on the
                                                                   // Pile from the top down to the specified position
     void Output(void);                                // Displays the names from the Pile, top down
};

You are to write the member functions for the three classes.  In order to do this, you should define a constant to represent the number of FlatElements you will be working with and an array of FlatElements to represent the particular items that you will be using.  For example, if the FlatElements were plastic lids, you might declare

const int   PIECE_COUNT = 7;
const FlatElement PIECE[7] = { FlatElement('m', "margarine", 1),
       FlatElement('i', "icing", 2),       FlatElement('c', "cottage cheese", 3),
       FlatElement('n', "nuts", 4),       FlatElement('w', "Cool Whip", 5)
       FlatElement('k', "Kona coffee", 6),       FlatElement('v', "vanilla ice cream", 7) };

The member functions of Pile will need to use these constants to do their job.  By separating these constants from the classes, it is possible to use the same classes with Piles of different kinds of FlatElements.  NO FUNCTIONS OTHER THAN THOSE SPECIFIED ABOVE MAY BE PART OF THE PILE CLASS.  In fact, you do not even need one of these functions to write the client program described below.

        Your program is meant to deal with coins as FlatElements; so, a penny is represented as a 'p', a nickel as 'n', a dime as 'd', a quarter as 'q', a half-dollar as 'h', and a dollar as '$'.  So, entry of the string qnpn  represents the Pile:  quarter, nickel, penny, nickel in which the quarter is on top of the Pile and the second nickel at the bottom.   Your program should repeatedly prompt the user at the keyboard to enter the specification of a Pile of coins, read in the specification and form a Pile, add this Pile to the existing Pile, standardize the updated Pile, and display the contents of the accumulating Pile.

        To add one Pile to another, place the Pile being added on top of the existing Pile.  To standardize a Pile, rearrange its contents so that no FlatElement of a larger relativeSize is higher in the Pile than any FlatElements of a smaller relativeSize.  You must write a Standardize function that is part of the client program to do the standardization.  Do NOT write Standardize as a member function.

        Here is a sample of what the user at the keyboard should see.  Suppose the user enters  qnpn  to represent a Pile, the client program should reorder the Pile to become  pnnq  and then display
penny
nickel
nickel
quarter

If the user then enters  pqd$  the client program should form the Pile  pqd$pnnq  add the new Pile to the existing Pile, reorder the Pile to become dppnnqq$ and display it as
dime
penny
penny
nickel
nickel
quarter
quarter
dollar

The program should continue in this manner until the user enters a string containing an invalid character.  The program should display an error message on an invalid character and should terminate.

        Hand in a printout of your program and a printout of the interactive session for a user who enters at least three Piles of coins, totaling at least 10 coins.  Copy your source program to the hand-in folder for this course on the P drive.

Notes about Pile member functions:

Input                    Read in the string of symbol characters and use the defined constants to form the Pile of FlatElements

Hoofe                  Returns an array index referring to the position of a FlatElement of the specified type within the Pile.  The Highest Out-Of-Order FlatElement is the FlatElement of the specified type which is highest in the Pile and has a smaller FlatElement directly beneath it.

Loofe                    Returns an array index referring to the position of a FlatElement of the specified type within the Pile.  The Lowest Out-Of-Order FlatElement is the FlatElement of the specified type which is lowest in the Pile and has a smaller FlatElement directly beneath it.

LowestSmaller    Returns an array index referring to the position of a FlatElement that is smaller than a FlatElement of the specified type and is lower in the Pile than any other FlatElement smaller than the specified one.

Add                      Copies the contents of the argument Pile to the top of the implied object Pile, stacks the argument Pile on top of the implied Pile making the implied Pile larger.

Flip                      All FlatElements on the implied Pile from the top to the specified Pile position have their order reversed on the Pile.

Output                 Displays the names of all FlatElements on the Pile from the top through to the bottom.  Each name is displayed on a line by itself.