Question: In this lab, we write C++ programs that define a class for manipulating polynomials, which are represented as linked lists of terms. You are provided
In this lab, we write C++ programs that define a class for manipulating polynomials, which are represented as linked lists of terms.
You are provided the main program lab9.cpp and the header files for polynomial.h and term.h.
Expected behavior of the main program lab9.cpp are given as comments after the program source codes. You are asked to write the codes for term.cpp and polynomial.cpp.
The commands to run the programs are:
g++ -c term.cpp
g++ -c polynomial.cpp
g++ lab9.cpp polynomial.o term.o
./a.out
You are asked to augment the main program with more codes that test the correctness of your implementations for
polynomials.
In addition, Extend your Lab 9 polynomial class to provide the following overloaded operator capabilities:
1.Overload the assignment operator (=) to assign onepolynomial to another.
2.Overload the addition operator (+) to add two polynomials.
3.Implement the subtract operation to subtract two polynomials, then overload it using (-) operator.
4.Overload the multiplication operator (*) to multiply two polynomials.
5.Overload the addition assignment operator (+=).
6.Overload the subtraction assignment operator (-=).
7.Overload the multiplication assignment operator (*=).
8.Overload the << operator for printing a polynomial (that supports cascaded printing of polynomials).
You are asked to augment the main program with new test cases demonstrating the correctness of your codes.
lab9.cpp :
#include "polynomial.h" #include#include using namespace std; main() { double coe1[4] = {1, 2, 3, 4}; double coe2[4] = {-1, -2, -3, -4}; int exp1[4] = {0, 4, 14, 18}; int exp2[4] = {0, 14, 4, 18}; polynomial p(4,coe1,exp1), q(4,coe2,exp1), s; cout << "P(x) = "; p.print(); cout << "Q(x) = "; q.print(); s.plus(q,q); cout << "Q(x) + Q(x) = "; s.print(); s.plus(p, q); cout << "P(x) + Q(x) = "; s.print(); s.multiply(p, p); cout << "P(x) * P(x) = "; s.print(); cout << endl; try { polynomial r(4,coe1,exp2); } catch (invalid_argument &e) { cout << "Exception: " << e.what() << endl; } } /* outputs: P(x) = 4x^18 + 3x^14 + 2x^ 4 + 1 Q(x) = -4x^18 + -3x^14 + -2x^ 4 + -1 Q(x) + Q(x) = -8x^18 + -6x^14 + -4x^ 4 + -2 P(x) + Q(x) = 0 P(x) * P(x) = 16x^36 + 24x^32 + 9x^28 + 16x^22 + 20x^18 + 6x^14 + 4x^ 8 + 4x^ 4 + 1 Exception: exponents are not in increasing order */
polynomial.h :
#ifndef POLYNOMIAL_H #define POLYNOMIAL_H #include "term.h" class polynomial { public: polynomial(); // default constructor polynomial(const polynomial& p); // copy constructor polynomial(int size, double coef[], int expon[]); // constructor // assume: expon[0] < expon[1] < expon[2] < ... // if assumption is not satisfied, throw an invalid_argument exception ~polynomial(); // destructor void plus(polynomial a, polynomial b); // add two polynomials void multiply(polynomial a, polynomial b); // multiply two polynomials int degree() const; // returns degree of polynomial bool isZero() const; // true if the polynomial is a zero polynomial // false otherwise void print() const; // print a polynomial private: term* h; }; #endif
term.h :
#ifndef TERM_H #define TERM_H struct term { int exp; // exponent double coe; // coefficient term* next; term(int e, double c, term* n=0) : exp(e), coe(c), next(n) {} }; void release(term *t); // free all terms pointed to by t void print_terms(term *t); // print terms term* dup_terms(term *t); // duplicate terms (copy constructor) term* add(term *a, term *b); // add the two polynomials a and b // return a linked list of terms term* build_poly(int size, double coef[], int expon[]); // build a linked list of terms // assume: expon[0] < expon[1] < expon[2] < ... // if assumption is not satisfied, throw an invalid_argument exception term* mult(term *t, double coef, int expon); // return a new polynomial that is the result of // multiplying the polynomial t // with the term with coefficient "coef" and exponent "expon" term* mult_terms(term *a, term *b); // multiple two polynomials a and b #endif Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
