CS246—Assignment 3 (Winter 2019)
R. Hackman G. Tondello Due Date 1: Friday, February 15, 5pm
Due Date 2: Friday, March 1, 5pm
Question 4
In this problem, you will implement a Polynomial class to represent and perform operations on single variable polynomials. We will use the Rational class from Q1 to represent the coefficients of the terms in a Polynomial. A polynomial can be represented using an array with the value at index idx used to store the coefficient of the term with exponent idx in the polynomial. For example, (9/4)x3 + (−7/3)x + 3/2
is represented by the array of Rationals {3/2,-7/3,0/1,9/4}.
The degree of a polynomial is the exponent of the highest non-zero term (3 in the example just discussed). Therefore, an array of size n+1 is required to store a polynomial of degree n. In order to not restrict the Polynomial class to a maximum degree, the array used to encode the polynomial is heap allocated.
You may assume that the coefficients of polynomials (given as input or produced through operations) can always be represented using a Rational. Additionally, there is no need to worry about over and under flow.
In the file polynomial.h, we have provided the type definition of Polynomial and signatures for the overloaded input and output operators for the Polynomial class. Implement all methods and functions. Place your implementation in polynomial.cc. You are free to use your own implementation from Question 1 of the methods and functions in rational.h or use the implementation we have provided in the compiled binary ftle rational.o. Note that your implementation of polynomial.cc will be linked to our implementation of Rational during testing.
The zero parameter constructor for Polynomial should create the zero polynomial i.e. a polynomial with no non-zero coefficient.
The overloaded arithmetic operators work identically to single variable polynomial arithmetic. For the division operation, two methods are to be implemented; operator/ should return the quotient after long division and operator should return the remainder.
The input operator reads the input stream till the end of the line and uses the read input to modify an existing Polynomial object. The input format is a pair for each non-zero term in the polynomial in decreasing exponent values with no exponent repeated. The first value in each pair is a rational number and follows the input format for Rational numbers as specified in Q1. This is the coefficient. The second value is a non-negative integer and represents the exponent of this term. Arbitrary amount of whitespace (excluding newline) is allowed within each pair and between pairs. For example, the input 3/5 5 -2/5 2 1/2 1 3/7 0 represents the polynomial (3/5)x^5 + (-2/5)x^2 + (1/2)x + (3/7).
The output operator prints polynomials as the addition of terms in decreasing exponent order. Each term is output as (a/b)x^n and subject to the following additional requirements and exceptions:
• Terms with zero coefficients are not printed.
• Coefficients are printed using the output format for Rational numbers as described in Q1.
• A term whose exponent is 1 is printed as (a/b)x and a term whose exponent is 0 is printed as (a/b).
An example of the output produced by the output operator is shown above during the discussion of the input operator.
A test harness is available in the file a3q4.cc, which you will find in your a3/a3q4 directory. Make sure you read and understand this test harness, as you will need to know what it does in order to structure your test suite. Note that we may use a different test harness to evaluate the code you submit on Due Date 2 (if your functions work properly, it should not matter what test harness we use). You should not modify a3q4.cc
• Due On Due Date 1: Design a test suite for this program. The suite should be named suiteq4.txt and zip the suite into a zip file named a3q4a.zip.
• Due On Due Date 2: Full implementation of the Polynomial class in C++. Your zip archive should contain at minimum the unchanged file a3q4.cc, polynomial.h, polynomial.cc and your Makefile. Your Makefile must create an executable named a3q4. Note that the executable name is case-sensitive. Any additional classes (if created) must each reside in their own .h and .cc files. Name the zip file a3q4b.zip