This is C++ programming.
Use separate compilation to implement a polynomial ADT that manipulates polynomials in a single variable x (e.g., p = 4 x^5 + 7 x^3 – x^2 + 9 ). For this problem, consider only polynomials whose exponents are non-negative integers. You are required to identify a proper data representation schema to store such polynomials and hide such data from external users of this ADT. Additionally, your ADT will at least include the following member functions:
One default constructor, which creates a polynomial with no terms.
Ex: Polynomial p; // This should create a polynomial with no terms.
One method allowing one to get an entire polynomial by interacting with the user (by writing to cout and reading from cin) to obtain the degree and coefficient of each term in a polynomial. If the user enters nonsense, your program should not crash! It is up to you how to handle this case. The method can look something like this when called:
myPolynomial.readFromUser(); // prompts the user for the degree
and
// coefficients and stores the
// results in myPolynomial
degree() // Returns the degree of a polynomial, which is the highest power of a term with a nonzero coefficient.
E.g: int deg = myPolynomial.degree();
coefficient(power) // Returns the coefficient of the x p o w e r term.
changeCoefficient(newCoefficient, power) // Replaces the coefficient of the x p o w e r term with newCoefficient. (Note: This method should handle the invalid case too. what happens if the power is out of range?? )
A method that multiplies a polynomial by an integer (make this a normal named method - do not overload the multiplication (*) operator).
A method that adds two polynomials ( overload the addition(+) operator as a standalone function).
Overload the division operator (/) as a member function to multiple divide a polynomial by a scalar variable.
A method that prints out a polynomial. Ex: cout<<p; // should print 4 x^5 + 7 x^3 – x^2 + 9 (Here you have to overload the "<<" put operator as a friend function. Because you cannot overload it as a member function)
Overload the subtraction operator(-) as a member function to subtract two polynomials.
Example: p1 = 3 x^3 - 2 x^2 - 3; p2 = 4 x^4 - x^2 + 3 x^1 - 5;
p3 = p1 - p2; // p3 should result in: - 4 x^4 + 3 x^3 - x^2 - 3 x + 2;
Overload the negation operator (-) as a member function to negate a polynomial. (NOTE: This is different from the subtraction operator! The subtraction operator takes an argument - the polynomial you are subtracting - whereas the negation operator takes no arguments.)
Example: p1 = 3 x^3 - 2 x^2 - 3; // -p1 should result in - 3 x^3 + 2 x^2 +3;
file containing adtpolyomial class.
adtpolynomial.h
#include<iostream>
#include<string.h>
#define max_degree 100000
using namespace std;
class Polynomial{
int coff[max_degree];
int deg;
inline bool isInteger(const std::string & s)
{
if(s.empty() || ((!isdigit(s[0])) && (s[0] !=
'-') && (s[0] != '+'))) return false ;
char * p ;
strtol(s.c_str(), &p, 10) ;
return (*p == 0) ;
}
public:
Polynomial (){
memset (coff, 0, sizeof(coff));
deg = 0;
}
void readFromUser(){
cout<<"enter deg : "<<endl;
char s[5000];
cin>>s;
while(!isInteger(s)||atoi(s)<0){
cout<<"invalid
deg, Try again."<<endl;
cout<<"enter deg :
"<<endl;
cin>>s;
}
deg = atoi(s);
for(int i=0;i<=deg;i++){
cout<<"enter
coefficient of x power "<<i<<endl;
cin>>s;
while(!isInteger(s)){
cout<<"Invalid Coefficient, try again"<<endl;
cout<<"enter coefficient of x power
"<<i<<endl;
cin>>s;
}
coff[i]=atoi(s);
}
}
int degree(){
return deg;
}
int coefficient(int power){
return coff[power];
}
void changeCoefficient(int newCoefficient,int
power){
if(power>deg)
cout<<"That term does not exist."<<endl;
else coff[power] =
newCoefficient;
}
void multiply(int a){
for(int i=0;i<=deg;i++){
coff[i] =
coff[i]*a;}
}
Polynomial operator+(const Polynomial& b)
{
Polynomial
a;
a.deg =
max(this->deg,b.deg);
for(int
i=0;i<=a.deg;i++){
a.coff[i] = this->coff[i] + b.coff[i];
}
return a;
}
Polynomial operator-(const
Polynomial& b) {
Polynomial
a;
a.deg =
max(this->deg,b.deg);
for(int
i=0;i<=a.deg;i++){
a.coff[i] = this->coff[i] - b.coff[i];
}
while(a.coff[a.deg]==0&& a.deg>0) a.deg--;
return a;
}
Polynomial operator-() {
Polynomial
a;
a.deg =
this->deg;
for(int
i=0;i<=a.deg;i++){
a.coff[i] = -this->coff[i] ;
}
return a;
}
Polynomial operator/(int b)
{
Polynomial
a;
a.deg =
this->deg;
for(int
i=0;i<=a.deg;i++){
a.coff[i] = this->coff[i]/b;
}
return a;
}
friend ostream & operator << (ostream
&out, Polynomial p);
};
ostream & operator << (ostream &out,Polynomial
p)
{ int j=0;
for(int i=p.degree();i>0;i--){
if(p.coff[i]!=0){
if(j!=0&&p.coff[i]>0){out<<"+";}
if(p.coff[i]==-1){
out<<"-x";}
else
if(p.coff[i]==1){out<<"x";}
else
{out<<p.coff[i]<<"x";}
if(i==1){}
else
{out<<"^"<<i;}
j=1;
}}
if(p.coff[0]!=0){
if(j!=0&&p.coff[0]>0){out<<"+";}
cout<<p.coff[0]<<endl;
}
out<<endl;
return out;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////adtpolynomial.h ends here /////////
file for testing adtpolynomial implementation
test.cpp
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include "adtpolynomial.h"
using namespace std;
int main(void) {
Polynomial p1;
Polynomial p2;
Polynomial p3;
p1.readFromUser();
cout<<p1;
p2.readFromUser();
cout<<p2;
p3=p1+p2;
cout<<p3;
p3=p1-p2;
cout<<p3;
p3=-p1;
cout<<p3;
p3.multiply(5);
cout<<p3;
p3 = p3/5;
cout<<p3;
cout<<p3.degree()<<endl;
p3.changeCoefficient(7,9);
p3.changeCoefficient(4,0);
cout<<p3;
return 0;
}
///////////////////////////////////// test.cpp ends
here/////////////////////////////////////////////////
screentshot for sample case p1=x^2+1 , p2 = x-7.
Get Answers For Free
Most questions answered within 1 hours.