CS12, Program 5
Assigned: March 5th,
2002
Due: March 15th,
2002, 11pm
Topics
·
Inheritance
·
Virtual
functions and polymorphism
·
More
pointers
·
Character
manipulation tools
·
Putting
together your very own program with multiple files from scratch
Background
A token is a single unit of information. For example, in a computer program,
variable names, operators and constants are all tokens. Each type of token is associated with a
set of actions. However, some
actions are common to all tokens.
For example, all tokens can be displayed. For this assignment, you will write a hierarchy of classes
for storing tokens read from the input.
The base class will store a generic token; two inherited classes will
store digits (0..9) and single character operators (+, -, *, =, etc.)
The
Program
First
you will need to write your classes.
·
The
base class, called Token, will have two virtual public methods: setValue() to
assign the token a value, and display() to display the token in the format
discussed below.
·
A
derived class, Digit, will store single digits in a private int data member.
Digit::setValue(char) will assign it a numerical value based on the input
parameter, which should be one of the chars in the range
‘0’..’9’.
Digit::display() will display the name of the digit (“zero”,
“one”, etc.). Five
points extra credit if you do this without using a switch and without using a
10-way if/if-else.
·
A
derived class, Oper, will store operators in a private char data member.
Oper::setValue(char) will assign it the value of the input parameter. Oper::display() will display the
operator.
Not
mentioned above, but still required, are methods such as constructors and
destructors. You may add other
data members and methods if you think they will enhance your program.
Your
program will have an array of pointers to Token. Make your array capable of holding at least 100
pointers. Based on the input, each
of these pointers will be assigned the address of a dynamically allocated Digit
or Oper object.
The
program reads in a stream of digits, operators and white space (space, tab, new
line). As each character is read,
the program will determine if it is a digit, operator or whitespace (the
functions isdigit() and isspace() will come in useful here), and create for it
an appropriate token (Digit or Oper), assigning its address to the next pointer
in the array. Assign this token a
value using setValue(), using the input character as parameter.
When
all the input has been processed, go through the array, displaying each token, separating
tokens with a single space.
Because of the way polymorphism works, you do not nee to know the type
of token you have. They are all
related by inheritance, and the appropriate display() method will be
called. The output loop will look
something like this
for (int i = 0; i < array_size; i++)
{
token_array[i]->display();
cout << “ “;
}
where
token_array is the name of the array and array_size is the number of tokens processed from the
input.
The
Input
A
sequence of single digits and operators, possibly separated by white space
(spaces, tabs, newlines).
For
example:
3+41*2/8
-2 + 4*86 - 9
A note on input:
The input should be entered from standard input
(keyboard or piped). Read the input character by character using a loop like
this:
char c;
while (cin >> c)
{
// process input
}
If you are entering the input from the keyboard,
hit ^D (control-d) when done. This
will cause cin >> c to return false and you will exit the loop.
The
Output
The
input sequence, digits translated to words and only a single space between
tokens. For the example above,
this would be
three
+ four one * two / eight – two + four * eight six - nine
What
to turn in
·
token.h
·
token.cc
·
digit.h
·
digit.cc
·
oper.h
·
oper.cc
·
main.cc
Please
note:
·
Do
not use any other names for your files.
These names are case sensitive – do not use capital letters
anywhere in the file names.
Failure to name your files as above will cost you 10 points.
·
Please
make sure that at least main.cc includes your name, login and lab section. Each is worth 16 points on this
assignment. If you are not paying
attention, let me remind you that three * one six = four eight. This means that you can barely get half
credit if you turn in a perfect program without your info on it.