CS246 [Winter 2012]
Assignment 4.
V1.2 Clarified
meaning of “degree”
Polynomials. Write a C++ class that represents polynomials such as
3  5 x + 4.7 x^3
We will write x^3 to mean x to the third power.
We will write such a polynomial in this simple format:
3x^0  5x^1 + 0x^2 + 4.7x^3
In this notation x^0 is equal to 1 and x^1 is equal to x.
The constants 3, 5, 0 and 4.7 are
coefficients. These are real numbers.
The exponents are 0, 1 2 and 3. Exponents are nonnegative integers.
The degree of a polynomial is given by the largest exponent with a nonzero coefficient, so the degree of this polynomial is 3. The degree of a polynomial consisting of only a number, such as 3.7x^0 (or simply, 3.7) is zero (0). A polynomial of degree 1, called the null polynomial, has all coefficients equal to zero (it is not necessary to explicitly represent all those zeros).
The operations on polynomials include addition, subtraction and multiplication. For example, if a = 1 + 5x and b = 7x + 9x^2 then a+b is 1 + 12x + 9x^2.
We introduce the concept of a unit polynomial of degree d as the polynomial in which all coefficients (up to and including d) are 1. Coefficients beyond d are zero.
Polynomials as Booleans. We extend polynomials so they can represent Booleans as follows. We consider the null polynomial to be false and we consider that any nonnull polynomial is true. This is much like the C language which considers 0 to be false, and any nonzero to be true. Let f be the null polynomial and let t be any nonnull polynomial. Then the Boolean operators  (or), & (and), ~ (not) will apply to polynomials, as follows:
AND
f & f = f
f & t = f
t & f = f
t & t = t (1 is the default true value, it is the polynomial 1x^0)
OR
f  f = f
f  t = t
t  f = t
t  t = t
NOT
~f = t
~t = f
Implementation of polynomial class
Write a C++ class called poly that supports such operations (+, . *, &, , ~). Within each polynomial object is a C++ vector of doubles that represent coefficients. The size of the vector (less 1) gives the degree of the represented polynomial. We assume that all polynomials are initially null.
The interface to the polynomial class should be in the file poly.h and its implementation should be in the file poly.cpp. You are given poly.h (you are not to change it) and you are to write poly.cpp.
Implementation of the calculator
Write a program named polyrun that can be used to manipulate polynomials. It reads commands from the console and produces corresponding output on the console. The calculator polyrun manipulates 26 polynomials, denoted as a, b, c, up to z. It is have an array of 26 polynomials, one for each lower case letter a, b, c, …, z.
The polyrun function (the main function) calls the poly function which carries out polynomial operations such as + and  (or).
The ‘help’ command
The calculator outputs help
information when you give it the ‘?’ command.
This is implemented by calling a help
function. You will be given the help
function (which you are to use), i.e., you will be given help.h
and help.cpp. Your main program
polyrun.cpp is to include help.h.
It is recommended that you study the file help.h as it describes the commands that the polyrun calculator is to implement. You should also trying running the supplied solution for the calculator (polyrunsolution) so you can try out the various commands.
Example calculator sessions
Session 1.
Polyrun Calculator. Type '?' for help.
Command: s
Set coefficient.
Give A n c: x 3 33.33
Command: o
Output poly.
Give A: x
Polynomial x: 0x^0 + 0x^1 + 0x^2 + 33.33x^3
Command: s
Set coefficient.
Give A n c: x 2 2.222
Command: o
Output poly.
Give A: x
Polynomial x: 0x^0 + 0x^1 + 2.222x^2 + 33.33x^3
Command: =
Assign. Give
A B: y x
Command: o
Output poly.
Give A: y
Polynomial y: 0x^0 + 0x^1 + 2.222x^2 + 33.33x^3
Command: +
Add. Give A B
C: z x y
Command: o
Output poly.
Give A: z
Polynomial z: 0x^0 + 0x^1 + 4.444x^2 + 66.66x^3
Command: q
Quitting
Session 2.
Polyrun Calculator. Type '?' for
help.
Command:
u
Unitary
poly. Give A n: x 1
Command:
o
Output
poly. Give A: x
Polynomial
x: 1x^0 + 1x^1
Command:
*
Multiply. Give A B C: y x x
Command:
o
Output
poly. Give A: y
Polynomial
y: 1x^0 + 2x^1 + 1x^2
Command:
q
Quitting
Session 3.
Polyrun Calculator. Type '?' for
help.
Command:
o
Output
poly. Give A: f
Polynomial
f: Null
Command:
~
Not. Give A B: t x
Command:
o
Output
poly. Give A: t
Polynomial
t: 1x^0
Command:
&
And. Give A B C: x t f
Command:
o
Output
poly. Give A: x
Polynomial
x: Null
Command:

Or. Give A B C: x t f
Command:
o
Output
poly. Give A: x
Polynomial
x: 1x^0
Command:
q
Quitting
Errors. To keep things simple, the calculator can assume that the user always types commands that are wellformed and that do not contain mistakes. However, you should use assertions (use the assert function) when it is easy to check constraints, e.g., to test that a typed exponent is not negative or that a name is other than a, b, c, up to z. Any error messages produced should start with ERROR and should go to the standard error stream cerr. Generally, your program should keep on executing after outputting an error message.
A
compiled solution to this assignment is available in
polyrunsolution
You can run this under a student account to see if your solution gives the same answers. You should check for matching (character by character) of your output with its output.
Files. You can find the supplied files for this assignment in:
~holt/cpforstudents/cs/246/2012/asgn04
You are to use the supplied doth files (help.h
and poly.h) in your program without changing
them.
You are to write these files: poly.cpp
and polyrun.ccp