C++ PROGRAM FOR POLYNOMIAL OBJECT AND NECESSARY OVERLOADED OPERATORS


#include<iostream.h>
#include<conio.h>
#define MAX 50
class arrpoly
{
private:
int co_arr[MAX];
int de_arr[MAX];
int polysize;
public:
void createpoly();
void showpoly();
arrpoly operator-();
};
arrpoly arrpoly::operator-()
{
arrpoly temp;
for(int i=0;i<polysize;i++)
{
temp.co_arr[i]=-co_arr[i];
temp.de_arr[i]=de_arr[i];
temp.polysize=polysize;
}
return temp;
}
void arrpoly::createpoly()
{
cout<<"enter the no:of nodes in the polynomial..."<<endl;
cin>>polysize;
if(polysize>MAX)
{
cout<<"polynomial is too big.....try again"<<endl;
createpoly();
}
for(int i=0;i<polysize;i++)
{
cout<<"enter the coefficient"<<endl;
cin>>co_arr[i];
cout<<"enter the degree...."<<endl;
cin>>de_arr[i];
}
}
void arrpoly::showpoly()
{
for(int i=0;i<polysize;i++)
{
cout<<co_arr[i]<<"x^"<<de_arr[i];
if(i!=polysize-1)
{
if(co_arr[i+1]>=0)
cout<<"+";
else
cout<<"";
}
}
}
void main()
{
arrpoly a;
clrscr();
a.createpoly();
cout<<"the polynomial is..."<<endl;
a.showpoly();
a=a;
cout<<"\n The negated polynomial is ..."<<endl;
a.showpoly();
getch();
}

OUTPUT:
enter the no:of nodes in the polynomial...
3
enter the coefficient
3
enter the degree....
2
enter the coefficient
1
enter the degree....
0
enter the coefficient
2
enter the degree....
1
the polynomial is...
3x^2+1x^0+2x^1the negated polynomial is ...
3x^2+1x^0+2x^1
































No comments:

Post a Comment