Question

In: Computer Science

Develop a class Polynomial. The internal representation of a Polynomial is an array of terms. Each...

Develop a class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent. has the coefficient 2 and the exponent 4. The User should be able to add as many terms as he wants, so the array should be dynamic.

Develop a complete class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded operator capabilities:

1. Overload the addition operator (+) to add two Polynomials.

2. Overload the subtraction operator (-) to subtract two Polynomials.

3. Overload the assignment operator to assign one Polynomial to another.

4. Overload the multiplication operator (*) to multiply two Polynomials.

5. Overload the addition assignment operator (+=), subtraction assignment operator (-=), and multiplication assignment operator (*=).

6. Enable input and output of Polynomials via overloaded >> and << operators, respectively.

Provide 3 more classes, derived from the main Polynomial class:

1. Linear (highest-degree term is of 1st degree)

2. Quadratic (highest-degree term is of 2nd degree)

3. Cubic (highest-degree term is of 3rd degree)

If the user tries to enter a higher degree polynomial into one of these classes, then print out the error and only store the terms that are valid. In the main function, create instances of all the classes defined, i.e. Linear, Quadric, Cubic, and Polynomial, and test out all these operations on those objects, and print the results on the console.

Explain the code in your own words in a paragraph.

Solutions

Expert Solution

#include <iostream>

#include <vector>

#include <utility>

using namespace std;

//defining the polynomial class

class Polynomial{

public:

//using dynamic array and a pair to hold the coefficient and the exponent

vector<pair<int, int>> polymomial;

Polynomial(){

}

//to get the coeff corrosponding to a degree

int getCoff(int degree){

for(auto it = polymomial.begin(); it != polymomial.end(); it++){

if((*it).second == degree){

return (*it).first;

}

}

return 0;

}

//set a coeff corrosponding to a degree

void setCoff(int degree, int coff){

for(auto it = polymomial.begin(); it != polymomial.end(); it++){

if((*it).second == degree){

(*it).second = coff;

}

}

}

//adding a new term to the polynomial

void addTerm(int coff, int deg){

polymomial.push_back(make_pair(coff, deg));

}

//overloading addition operator

Polynomial operator +(Polynomial &p){

Polynomial r;

//adding the terms that are present in this, this and other

for(auto it = polymomial.begin(); it != polymomial.end(); it++){

r.addTerm((*it).first + p.getCoff((*it).second), (*it).second);

}

//adding the terms that are present in only other

for(auto it = p.polymomial.begin(); it != p.polymomial.end(); it++){

if(p.getCoff((*it).second) == 0){

r.polymomial.push_back(*it);

}

}

return r;

}

Polynomial operator -(Polynomial p){

Polynomial r;

for(auto it = polymomial.begin(); it != polymomial.end(); it++){

r.addTerm((*it).first - p.getCoff((*it).second), (*it).second);

}

for(auto it = p.polymomial.begin(); it != p.polymomial.end(); it++){

if(p.getCoff((*it).second) == 0){

r.polymomial.push_back(*it);

}

}

return r;

}

Polynomial operator *(Polynomial p){

Polynomial r;

for(auto it1 = polymomial.begin(); it1 != polymomial.end(); it1++){

for(auto it2 = polymomial.begin(); it2 != polymomial.end(); it2++){

r.addTerm((*it1).first * (*it2).first, (*it1).second + (*it1).second);

}

}

return r;

}

void operator =(Polynomial &p){

for(auto it = p.polymomial.begin(); it != p.polymomial.end(); it++){

if(p.getCoff((*it).second) == 0){

polymomial.push_back(*it);

}

}

}

Polynomial operator +=(Polynomial &p){

Polynomial r = *this + p;

return r;

}

Polynomial operator -=(Polynomial &p){

Polynomial r = *this - p;

return r;

}

Polynomial operator *=(Polynomial &p){

Polynomial r = *this * p;

return r;

}

friend ostream &operator<<( ostream &out, const Polynomial &p ) {

auto it = p.polymomial.begin();

for(; it != p.polymomial.end() - 1; it++){

out<<(*it).first<<"x"<<"^"<<(*it).second<<" + ";

}

out<<(*it).first<<"x"<<"^"<<(*it).second;

out<<endl;

return out;

}

friend istream &operator>>( istream &in, Polynomial &p ) {

int terms;

cout<<"No.of terms: ";

in>>terms;

for(int i = 0; i < terms; i++){

int c, d;

cout<<"Coefficient, Degree: ";

in>>c; in>>d;

p.addTerm(c, d);

}

return in;

}

~Polynomial(){

polymomial.~vector();

}

};

int main() {

Polynomial p, q;

cout<<"Second Polynomial"<<endl;

cin>>p;

cout<<p;

cout<<"Second Polynomial"<<endl;

cin>>q;

cout<<q;

cout<<"Polynomial Addition"<<endl;

cout<<p+q;

cout<<"Polynomial Subtraction"<<endl;

cout<<p-q;

}


Related Solutions

           Homework: Polynomial Using Array Description: Implement a polynomial class (1) Name your class...
           Homework: Polynomial Using Array Description: Implement a polynomial class (1) Name your class Polynomial (2) Use array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array. (3) define the following methods: a. public Polynomial()    POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0)    POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p)    POSTCONDITION: Creates...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. Much of this work can be modelled on the C++ dynamic array of ints List that we discussed in class. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the...
Create a class and name it MyArray. This class must have an internal array of integers...
Create a class and name it MyArray. This class must have an internal array of integers and the consumer should specify the maximum capacity when instantiating. MyArray class must provide following functions: 1- insert: This method receives and integer and inserts into the array. For simplicity you can assume the array is large enough and never overflows. 2- display: This method displays all integers stored in the array in the same order as they are inserted (first in first out)....
Make an Array implementation of a binary tree given the below class ArrayBinaryTree(BinaryTree): """Linked representation of...
Make an Array implementation of a binary tree given the below class ArrayBinaryTree(BinaryTree): """Linked representation of a binary tree structure.""" # -------------------------- nested _Node class -------------------------- class _Node: def __init__(self, element, parent=None, left=None, right=None): # -------------------------- nested Position class -------------------------- class Position(BinaryTree.Position): """An abstraction representing the location of a single element.""" def __init__(self, container, node): def element(self): def __eq__(self, other): # ------------------------------- utility methods ------------------------------- def _validate(self, p): """Return associated node, if position is valid.""" def _make_position(self, node): """Return Position...
Task 3: Class Polynomial (Version 2) In a separate namespace, the class Polynomial (Version 2) re-implements...
Task 3: Class Polynomial (Version 2) In a separate namespace, the class Polynomial (Version 2) re-implements a polynomial as a linear array of terms ordered by exponent. Each polynomial is also reduced to simplest terms, that is, only one term for a given exponent. public class Polynomial { // P is a linear array of Terms private Term[ ] P; … // Re-implement the six methods of Polynomial (including the constructor) … }
Let T be a binary tree with n positions that is realized with an array representation...
Let T be a binary tree with n positions that is realized with an array representation A, and let f() be the level numbering function of the positions of T, as given in Section 8.3.2. Give pseudocode descriptions of each of the methods root, parent, left, right, isExternal, and isRoot.
According to the IIA the ‘Internal auditor should develop and record a plan for each engagement,...
According to the IIA the ‘Internal auditor should develop and record a plan for each engagement, including the scope , objectives, timing and resource allocation." The same can be said about the EA (external auditor). Compare and contrast engagement planning for the IA and EA. How does the control environment relate to engagement planning? Also what role does SOX have on the control environment and audit planning?
Complete the "dumb" 8 queens program that Use the 1 dimensional array representation. C++ This is...
Complete the "dumb" 8 queens program that Use the 1 dimensional array representation. C++ This is the solution to the  question. i want this program to written in different WAY. nothing fancy. #include<cmath> #include<fstream> #include<iostream> using namespace std; bool ok(int b[][8]){ int rQueens=0, dQueens=0; for(int row=0; row<8; row++){ for(int column=0; column<8; column++){ //Rows test if(b[row][column]==1) rQueens++; if(rQueens>1) return false; //Diagonals test    for(int j=1; ((column-j)>=0)&&((row-j)>=0); j++){ if(b[row-j][column-j]==1&&b[row][column]==1) return false; } for(int k=1; ((column-k)>=0)&&((row+k)<8); k++){ if(b[row+k][column-k]==1&&b[row][column]==1) return false; } } rQueens=0; }...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT