my project is
Develop class Polynomial.This internal representation of a polynomial is an array of terms.Each term contains a coefficient and an exponent, hence you can have an array for coefficients and exponents as a member of your class(maximum number of terms will 100 however your class should also contain a member variable that indicates how many terms are there in a given instance of the polynomial class).
the term 2x^4 has a coefficient of 2 and exponent of 4. Develop a full 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
i have tried but i failed
so please help me in building the code
please help me in building this code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include<iostream>
using namespace std;
class Polynomial
{
private:
int *Coefficient;
int *Exponents;
int Nterms;//number of terms
public:
Polynomial();
int getC(){return Coefficient};
int getE(){return Exponents};
void getUserInput();
friend Polynomial operator+(const Polynomial&);
};
Polynomial operator+(Polynomial&A,Polynomial&B){ //Adds two polynomial objects when exponents become equal in the loop
Polynomial C;
for(int i=0;i<100;i++){
if(A.getE()[i]==B.getE()[i]){
C.getE()[i]=A.getE()[i];
C.getC()[i]=B.getE()[i]+A.getE()[i];
}
}
return C;
};
Polynomial operator-(Polynomial&A,Polynomial&B){//Sub two polynomial objects when exponents become equal in the loop
Polynomial C;
for(int i=0;i<100;i++){
if(A.getE()[i]==B.getE()[i]){
C.getE()[i]=A.getE()[i];
C.getC()[i]=B.getE()[i]-A.getE()[i];
}
}
return C;
};
//constructors
Polynomial::Polynomial()
{
Nterms=0;
Coefficient = new int[100];
Exponents = new int[100];
for (int i = 0; i < 100; i++)
{
Coefficient[i] = 0;
Exponents[i] = 0;
}
}
void Polynomial::getUserInput(){//Allows user to enter coef and expo
cout<<"Enter the Exponent of Polynomial: ";
cin>>Exponents[Nterms];
cout<<endl;
cout<<"Enter the Coefficient of Polynomial: ";
cin>>Coefficient[Nterms];
Nterms++;
}
|