Polynomial C++ Class
//===============================
//Term.h
//===============================
#ifndef TERM_H
#define TERM_H
#include"Polynomial.h"
class Polynomial ; //forward declaration
class Term
{
friend Polynomial;
private:
int exp;
float coef;
};
#endif
//===============================//Polynomial.h
//===============================
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include"Term.h"
#include<iostream>
#include<math.h>
using namespace std;
class Polynomial:public Term
{
private:
Term* termArray;
int size;
public:
// make appropriate constructors.
Polynomial();
Polynomial(float *coef,int *exp, int size);
//functions other than constructors.
friend istream & operator>>(istream& is , Polynomial& X);
friend ostream & operator<<(ostream& os , /*const*/ Polynomial& X);
Polynomial operator+( /*const */Polynomial& B);
Polynomial operator-(Polynomial& B);
Polynomial operator*( const Polynomial& B);
float getcoef(int i);
int getexp(int i);
int getsize();
void setsize(int i);
void setcoef(int i,float val);
void setexp(int i,int val);
Polynomial neg();
int operator()(int n); //evaluates polynomial at ‘n’ and returns result.
};
#endif
//===============================
//Polynomial.cpp
//===============================
#include"Polynomial.h"
Polynomial::Polynomial()
{
size = 0 ;
termArray = NULL;
}
Polynomial::Polynomial(float *coef,int *exp, int size1)
{
this->size = size1;
this->termArray = new Term[size];
for(int i = 0 ; i < size ; i ++ )
{
this->termArray[i].coef = coef [ i ] ;
this->termArray[i].exp = exp [ i ] ;
}
}
float Polynomial::getcoef(int i)
{
return this->termArray[i].coef;
}
int Polynomial::getexp(int i)
{
return this->termArray[i].exp;
}
int Polynomial::getsize()
{
return size;
}
void Polynomial::setcoef(int i,float val)
{
this->termArray[i].coef = val;
}
void Polynomial::setexp(int i,int val)
{
termArray[i].exp = val ;
}
void Polynomial::setsize(int i)
{
delete []termArray;
size = i;
termArray = new Term[size];
for (int j = 0 ; j < size ; j++)
{
termArray[j].coef = 0;
termArray[j].exp = i;
i--;
}
}
istream& operator>>(istream& is , Polynomial& X)
{
int count = 0 ;
float coef[1000];
int exp[1000];
int i = 0;
char choice;
for( ; ; i++ )
{
cout << "Enter coefficient : ";
is>>coef[i];
cout << "Enter power : ";
is>>exp[i];
while(exp[i] < 0 ||( i>=1 && exp [ i ] >= exp [ i - 1 ] ))
{
cout << "Invalid exponent.";
cout << endl << "Enter again : ";
is >> exp[i];
}
if(exp[i] == 0 )
break;
cout << "Do You Want To Enter Another Value (y/n) : ";
cin >> choice;
if(choice == 'n' || choice == 'N')
break;
else if (choice == 'y' || choice == 'Y')
continue;
}
count = i+1;
X.setsize(count);
for(i = 0 ; i < count ; i++)
{
X.setcoef(i,coef[i]);
X.setexp(i,exp[i]);
}
return is;
}
ostream& operator<<(ostream& os , /*const*/ Polynomial& X)
{
for(int i = 0 ; i < X.getsize() ; i++ )
{
if(X.getcoef(i) > 0 )
{
os << " +";
}
else if(X.getcoef(i) == 0 )
{
os << " +0 ";
continue;
}
else if(X.getexp(i) == 0 )
{
os << X.getcoef(i);
continue;
}
os<< X.getcoef(i) <<"x^"<< X.getexp(i) << " ";
}
return os;
}
Polynomial Polynomial::operator+( /*const*/ Polynomial& B)
{
Polynomial sum;
int loop_limit = 0;
int same=0,c=0;
if( B.getexp (0) >= this -> getexp (0) )
{
for(int i = 0 ,j = 0 , k = 0 ; k < B.getexp(0) ; k++ )
{
if(i >= getsize()&& j < B.getsize() )
{
c++;
j++;
}
else if(j >= B.getsize()&& i < getsize() )
{
c++;
i++;
}
else if(this->getexp(i) > B.getexp(j) && i < getsize())
{
c++;
i++;
}
else if (B.getexp(j) > this->getexp(i)&& j < B.getsize())
{
c++;
j++;
}
else if(B.getexp(j) == this->getexp(i)&& i < getsize()&& j < B.getsize())
{
same++;
i++;
j++;
}
}
sum.setsize(same+c);
}
else if( this->getexp(0) >= B.getexp (0) )
{
for(int i = 0 ,j = 0 , k = 0 ; k < getexp(0) ; k++ )
{
if(this->getexp(i) > B.getexp(j))
{
c++;
i++;
}
else if (B.getexp(j) > this->getexp(i))
{
c++;
j++;
}
else if(B.getexp(j) == this->getexp(i))
{
same++;
i++;
j++;
}
}
sum.setsize(same+c);
}
for(int i = 0 , j = 0 , k = 0 , l = loop_limit ; k < loop_limit ; k ++ )
{
if(this->getexp(i) > B.getexp(j))
{
sum.setexp(k,this->getexp(i));
sum.setcoef(k,this->getcoef(i));
i++;
}
else if (B.getexp(j) > this->getexp(i))
{
sum.setexp(k,B.getexp(j));
sum.setcoef(k,B.getcoef(j));
j++;
}
else if(B.getexp(j) == this->getexp(i))
{
sum.setexp(k,B.getexp(i));
sum.setcoef( k , ( getcoef(i) + B.getcoef(j) ) );
i++;
j++;
}
else
{
sum.setexp(k,l);
sum.setcoef( k , 0 );
i++;
j++;
}
l--;
}
return sum;
}
Polynomial Polynomial::operator-(Polynomial& B)
{
Polynomial dif;
dif = B.neg();
dif = *this + dif;
return dif;
}
Polynomial Polynomial::neg()
{
Polynomial p(*this);
for (int i = 0 ; i < p.getsize() ; i ++ )
{
p.setcoef(i,p.getcoef(i)*(-1));
}
return p;
}
int Polynomial::operator()(int n)
{
int res=0;
int prd=0;
for(int i = 0 ; i < this->getsize() ; i ++ )
{
prd = pow ( n , this->getexp ( i ) );//calculates power of n
res = res + ( prd * this->getcoef(i) ) ; // multiply the answer with respective coefficient and save result
}
return res;
}
//===========================
// Main Driver Program
//===========================
#include<iostream>
#include"Polynomial.h"
using namespace std;
int main()
{
Polynomial P;
Polynomial P1;
int choice;
int c1 = 0,c2 = 0;
int x;
while(1)
{
cout << endl << " What you want to do.";
cout << endl << " 1) Enter First Polynomial.";
cout << endl << " 2) Enter Second Polynomial.";
cout << endl << " 3) Add Polynomials.";
cout << endl << " 4) Subtract Polynomials.";
cout << endl << " 5) Evaluate First Polynomial For a Value Of x.";
cout << endl << " 6) Evaluate Second Polynomial For a Value Of x.";
cout << endl << " 7) Quit.";
cout << endl
<< endl << " Enter Choice :: ";
cin >> choice;
switch(choice)
{
case 1:
if(c1 == 0)
{
cin >>P;
cout << endl
<< " Entered Polynomial Was :: " << P;
cout << endl;
c1=1;
}
else
cout << "Already Entered Second Polynomial.";
break;
case 2:
if(c2 == 0)
{
cin >> P1;
cout << endl
<< " Entered Polynomial Was :: " << P1;
cout << endl;
c2=1;
}
else
cout << "Already Entered Second Polynomial.";
break;
case 3:
if(c1==0)
{
cout << endl << " First Polynomial Wasn\'t Entered.";
}
else
{
if(c2==0)
{
cout << endl << " Second Polynomial Wasn\'t Entered.";
}
else
{
Polynomial P2(P+P1);
cout << endl << " Sum Is :: "<< P2;
}
}
break;
case 4:
if(c2==0)
{
cout << endl << " Second Polynomial Wasn\'t Entered.";
}
else
{
if(c1==0)
{
cout << endl << " First Polynomial Wasn\'t Entered.";
}
else
{
Polynomial P2(P-P1);
cout << endl << " Sum Is :: "<< P2;
}
}
break;
case 5:
if(c1==0)
{
cout << endl << " First Polynomial Wasn\'t Entered.";
}
else
{
cout << " Enter the value of x : ";
cin >> x;
P(x);
}
break;
case 6:
if(c2==0)
{
cout << endl << " Second Polynomial Wasn\'t Entered.";
}
else
{
cout << " Enter the value of x : ";
cin >> x;
P1(x);
}
break;
case 7:
exit(0);
break;
default:
cout << " Invalid Option Was selected.";
break;
}
}
return 0;
}
//Term.h
//===============================
#ifndef TERM_H
#define TERM_H
#include"Polynomial.h"
class Polynomial ; //forward declaration
class Term
{
friend Polynomial;
private:
int exp;
float coef;
};
#endif
//===============================//Polynomial.h
//===============================
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include"Term.h"
#include<iostream>
#include<math.h>
using namespace std;
class Polynomial:public Term
{
private:
Term* termArray;
int size;
public:
// make appropriate constructors.
Polynomial();
Polynomial(float *coef,int *exp, int size);
//functions other than constructors.
friend istream & operator>>(istream& is , Polynomial& X);
friend ostream & operator<<(ostream& os , /*const*/ Polynomial& X);
Polynomial operator+( /*const */Polynomial& B);
Polynomial operator-(Polynomial& B);
Polynomial operator*( const Polynomial& B);
float getcoef(int i);
int getexp(int i);
int getsize();
void setsize(int i);
void setcoef(int i,float val);
void setexp(int i,int val);
Polynomial neg();
int operator()(int n); //evaluates polynomial at ‘n’ and returns result.
};
#endif
//===============================
//Polynomial.cpp
//===============================
#include"Polynomial.h"
Polynomial::Polynomial()
{
size = 0 ;
termArray = NULL;
}
Polynomial::Polynomial(float *coef,int *exp, int size1)
{
this->size = size1;
this->termArray = new Term[size];
for(int i = 0 ; i < size ; i ++ )
{
this->termArray[i].coef = coef [ i ] ;
this->termArray[i].exp = exp [ i ] ;
}
}
float Polynomial::getcoef(int i)
{
return this->termArray[i].coef;
}
int Polynomial::getexp(int i)
{
return this->termArray[i].exp;
}
int Polynomial::getsize()
{
return size;
}
void Polynomial::setcoef(int i,float val)
{
this->termArray[i].coef = val;
}
void Polynomial::setexp(int i,int val)
{
termArray[i].exp = val ;
}
void Polynomial::setsize(int i)
{
delete []termArray;
size = i;
termArray = new Term[size];
for (int j = 0 ; j < size ; j++)
{
termArray[j].coef = 0;
termArray[j].exp = i;
i--;
}
}
istream& operator>>(istream& is , Polynomial& X)
{
int count = 0 ;
float coef[1000];
int exp[1000];
int i = 0;
char choice;
for( ; ; i++ )
{
cout << "Enter coefficient : ";
is>>coef[i];
cout << "Enter power : ";
is>>exp[i];
while(exp[i] < 0 ||( i>=1 && exp [ i ] >= exp [ i - 1 ] ))
{
cout << "Invalid exponent.";
cout << endl << "Enter again : ";
is >> exp[i];
}
if(exp[i] == 0 )
break;
cout << "Do You Want To Enter Another Value (y/n) : ";
cin >> choice;
if(choice == 'n' || choice == 'N')
break;
else if (choice == 'y' || choice == 'Y')
continue;
}
count = i+1;
X.setsize(count);
for(i = 0 ; i < count ; i++)
{
X.setcoef(i,coef[i]);
X.setexp(i,exp[i]);
}
return is;
}
ostream& operator<<(ostream& os , /*const*/ Polynomial& X)
{
for(int i = 0 ; i < X.getsize() ; i++ )
{
if(X.getcoef(i) > 0 )
{
os << " +";
}
else if(X.getcoef(i) == 0 )
{
os << " +0 ";
continue;
}
else if(X.getexp(i) == 0 )
{
os << X.getcoef(i);
continue;
}
os<< X.getcoef(i) <<"x^"<< X.getexp(i) << " ";
}
return os;
}
Polynomial Polynomial::operator+( /*const*/ Polynomial& B)
{
Polynomial sum;
int loop_limit = 0;
int same=0,c=0;
if( B.getexp (0) >= this -> getexp (0) )
{
for(int i = 0 ,j = 0 , k = 0 ; k < B.getexp(0) ; k++ )
{
if(i >= getsize()&& j < B.getsize() )
{
c++;
j++;
}
else if(j >= B.getsize()&& i < getsize() )
{
c++;
i++;
}
else if(this->getexp(i) > B.getexp(j) && i < getsize())
{
c++;
i++;
}
else if (B.getexp(j) > this->getexp(i)&& j < B.getsize())
{
c++;
j++;
}
else if(B.getexp(j) == this->getexp(i)&& i < getsize()&& j < B.getsize())
{
same++;
i++;
j++;
}
}
sum.setsize(same+c);
}
else if( this->getexp(0) >= B.getexp (0) )
{
for(int i = 0 ,j = 0 , k = 0 ; k < getexp(0) ; k++ )
{
if(this->getexp(i) > B.getexp(j))
{
c++;
i++;
}
else if (B.getexp(j) > this->getexp(i))
{
c++;
j++;
}
else if(B.getexp(j) == this->getexp(i))
{
same++;
i++;
j++;
}
}
sum.setsize(same+c);
}
for(int i = 0 , j = 0 , k = 0 , l = loop_limit ; k < loop_limit ; k ++ )
{
if(this->getexp(i) > B.getexp(j))
{
sum.setexp(k,this->getexp(i));
sum.setcoef(k,this->getcoef(i));
i++;
}
else if (B.getexp(j) > this->getexp(i))
{
sum.setexp(k,B.getexp(j));
sum.setcoef(k,B.getcoef(j));
j++;
}
else if(B.getexp(j) == this->getexp(i))
{
sum.setexp(k,B.getexp(i));
sum.setcoef( k , ( getcoef(i) + B.getcoef(j) ) );
i++;
j++;
}
else
{
sum.setexp(k,l);
sum.setcoef( k , 0 );
i++;
j++;
}
l--;
}
return sum;
}
Polynomial Polynomial::operator-(Polynomial& B)
{
Polynomial dif;
dif = B.neg();
dif = *this + dif;
return dif;
}
Polynomial Polynomial::neg()
{
Polynomial p(*this);
for (int i = 0 ; i < p.getsize() ; i ++ )
{
p.setcoef(i,p.getcoef(i)*(-1));
}
return p;
}
int Polynomial::operator()(int n)
{
int res=0;
int prd=0;
for(int i = 0 ; i < this->getsize() ; i ++ )
{
prd = pow ( n , this->getexp ( i ) );//calculates power of n
res = res + ( prd * this->getcoef(i) ) ; // multiply the answer with respective coefficient and save result
}
return res;
}
//===========================
// Main Driver Program
//===========================
#include<iostream>
#include"Polynomial.h"
using namespace std;
int main()
{
Polynomial P;
Polynomial P1;
int choice;
int c1 = 0,c2 = 0;
int x;
while(1)
{
cout << endl << " What you want to do.";
cout << endl << " 1) Enter First Polynomial.";
cout << endl << " 2) Enter Second Polynomial.";
cout << endl << " 3) Add Polynomials.";
cout << endl << " 4) Subtract Polynomials.";
cout << endl << " 5) Evaluate First Polynomial For a Value Of x.";
cout << endl << " 6) Evaluate Second Polynomial For a Value Of x.";
cout << endl << " 7) Quit.";
cout << endl
<< endl << " Enter Choice :: ";
cin >> choice;
switch(choice)
{
case 1:
if(c1 == 0)
{
cin >>P;
cout << endl
<< " Entered Polynomial Was :: " << P;
cout << endl;
c1=1;
}
else
cout << "Already Entered Second Polynomial.";
break;
case 2:
if(c2 == 0)
{
cin >> P1;
cout << endl
<< " Entered Polynomial Was :: " << P1;
cout << endl;
c2=1;
}
else
cout << "Already Entered Second Polynomial.";
break;
case 3:
if(c1==0)
{
cout << endl << " First Polynomial Wasn\'t Entered.";
}
else
{
if(c2==0)
{
cout << endl << " Second Polynomial Wasn\'t Entered.";
}
else
{
Polynomial P2(P+P1);
cout << endl << " Sum Is :: "<< P2;
}
}
break;
case 4:
if(c2==0)
{
cout << endl << " Second Polynomial Wasn\'t Entered.";
}
else
{
if(c1==0)
{
cout << endl << " First Polynomial Wasn\'t Entered.";
}
else
{
Polynomial P2(P-P1);
cout << endl << " Sum Is :: "<< P2;
}
}
break;
case 5:
if(c1==0)
{
cout << endl << " First Polynomial Wasn\'t Entered.";
}
else
{
cout << " Enter the value of x : ";
cin >> x;
P(x);
}
break;
case 6:
if(c2==0)
{
cout << endl << " Second Polynomial Wasn\'t Entered.";
}
else
{
cout << " Enter the value of x : ";
cin >> x;
P1(x);
}
break;
case 7:
exit(0);
break;
default:
cout << " Invalid Option Was selected.";
break;
}
}
return 0;
}
Comments
Post a Comment