You are on page 1of 7

#include<iostream.

h>
#include<stdlib.h>
#include<math.h>
/// Calculate ploynomial Equation with One variable
///Using Doubly linked List

class list
{
protected:
struct node
{
int co;
int pow;
node *next;
node *prev;

};

typedef struct node *nodeptr;


nodeptr listptr, lastptr;

public:
list();
~list();
bool emptylist();
void push(int co, int pow);
double add();
double multiply();
void display();

};
list::list()
{ listptr=NULL;
lastptr=NULL;
}

list::~list()
{
nodeptr p,q;
if(emptylist())
exit(0);

for(p=listptr, q=p->next; p!=NULL; p=q, p->next)


delete p;
}

bool list:: emptylist()


{
if(listptr==NULL)
return 1;
else
return 0;
}

void list:: push(int co, int pow)


{
nodeptr p;
p=new node;
p->co=co;
p->pow=pow;
p->prev=NULL;
if(listptr==NULL)
{
listptr=lastptr=p;
p->next=NULL;
}
else
{
p->next=listptr;
listptr->prev=p;
listptr=p;
}
}
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
double list:: add()
{
int x;
double ans=0;
nodeptr p,q;
cout<<"Enter Value of 'X' :: ";
cin>>x;

/// Displays Equation with '+' sign


for(q=lastptr; q!=NULL; q=q->prev)
{
if(q->co==1)
cout<<"x^"<<q->pow;
else
cout<< q->co<<"x^"<<q->pow;

if(q->prev!=NULL)
cout<<" + ";
}
/// Add All
for(p=listptr; p!=NULL; p=p->next)
{
ans=ans+p->co*pow(x,p->pow);
}
return ans;
}
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
double list:: multiply()
{
int x;
double ans=1;
nodeptr p,q;

cout<<"Enter Value of 'X' :: ";


cin>>x;

/// Displays Equation with '*' sign


for(q=lastptr; q!=NULL; q=q->prev)
{
if(q->co==1)
cout<<"x^"<<q->pow;
else
cout<< q->co<<"x^"<<q->pow;

if(q->prev!=NULL)
cout<<" * ";
}

///////////////////////////////////////////////////////////////////
/// Multiply all
for(p=listptr; p!=NULL; p=p->next)
{
ans=ans*p->co*pow(x,p->pow);
}
return ans;
}

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
void list :: display()
{
int i=1;
nodeptr p;

for(p=lastptr; p!=NULL; p=p->prev, i++)


{
if(p->co==1)
cout<<i<<". "<<"x^"<<p->pow<<endl ;
else
cout<<i<<". "<<p->co<<"x^"<<p->pow<<endl;

}
}

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

void main()
{
list l;
char ch='0';
while(ch!='5')
{
cout<<"\n1 :: Enter a Value :: ";
cout<<"\n2 :: Add Entered Ploynomials:: ";
cout<<"\n3 :: Muiltply Entered Ploynomials :: ";
cout<<"\n4 :: Display Entered Ploynomials";
cout<<"\n5 :: Exit";
cout<<"\n\nEnter Your choice:: ";
cin>>ch;

switch(ch)
{
case '1':
{
int x,y;
cout<<"\n\nEnter Co-officient :: ";
cin>>x;
cout<<"\n Enter it's power :: ";
cin>>y;

l.push(x,y);
break;
}

case '2':
{
cout<<"\nAnswer :: "<<l.add()<<endl<<endl;
break;
}

case '3':
{
cout<<"\nAnswer :: "<< l.multiply()<<endl<<endl;
break;
}

case'4':
{
l.display(); break;
}

case'5':
{
ch='5';
break;
}

default:
{
cout<<"\n!!! Wrong choice !!!\n\n\n";
}
}}
}

You might also like