Tuesday, 26 August 2014

Polynomial Operations

#include "stdafx.h"
#include<iostream>
using namespace std;
struct poly
{
int ex,co;
poly *next;
};
void sum(poly *a,poly *b)
{
bool t=0,s=0,c=0;
cout<<" S U M    :\n";
while(true)
{
if(a->ex==b->ex)
{
cout<<a->co+b->co<<" X^"<<a->ex;
a=a->next;
b=b->next;
}
else
{
if(a->ex>b->ex)
{
cout<<a->co<<" X^"<<a->ex;
a=a->next;
}
else
{
cout<<b->co<<" X"<<b->ex;
b=b->next;
}
}
if(c==1)
{
cout<<endl;
break;
}
if(a->next->next==NULL && b->next==NULL)
c=1;
cout<<" + ";
}
}
int main()
{
poly *first[2],*temp,*prev;
first[0]=new poly;
first[1]=new poly;
first[0]->next=first[1]->next=NULL;

for(int i=0;i<2;i++)
{
if(i==0)
cout<<"ENTER FIRST POLYNOMIAL OR PRESS 0 TO END\n";
if(i==1)
cout<<"ENTER SECOND POLYNOMIAL OR PRESS 0 TO END\n";
temp=first[i];
prev=temp;
do
{
cin>>temp->co;
system("cls");
 if(temp->co==0)
{
break;
}
temp=first[i];
while(true)
{
// if(i==0)
// {
if(temp->co>0)
              cout<<temp->co<<" X^";
else
cout<<"("<<temp->co<<") X^";
// }
/* if(i==1)
{
if(temp->co>0)
              cout<<temp->co<<" Y^";
else
cout<<"("<<temp->co<<") Y^";
}*/
 if(temp->next!=NULL)
 cout<<temp->ex<<" + ";
 else
 break;
 temp=temp->next;
}
cin>>temp->ex;
prev=temp;
temp->next=new poly;
temp=temp->next;
temp->next=NULL;
system("cls");
}
while(prev->co!=0);
}
for(int i=0;i<2;i++)
{
int x;
prev=temp=first[i];
while(temp->next->next!=NULL)
{
prev=temp;
while(true)//prev->next!=NULL)
{
if(temp->ex<prev->ex)
{
x=temp->ex;
temp->ex=prev->ex;
prev->ex=x;
x=temp->co;
temp->co=prev->co;
prev->co=x;
}
if(prev->next==NULL)
break;
prev=prev->next;
}
temp=temp->next;
}
}
cout<<"POLYNOMIALS  :"<<endl<<endl;
for(int i=0;i<2;i++)
{
temp=first[i];
while(true)
{
//if(i==0)
// {
if(temp->co>0)
cout<<temp->co<<" X^ "<<temp->ex;
else
cout<<"("<<temp->co<<") X^ "<<temp->ex;
//}
/*else
{
if(temp->co>0)
cout<<temp->co<<" Y^ "<<temp->ex;
else
cout<<"("<<temp->co<<") Y^ "<<temp->ex;
}*/
if(temp->next->next!=NULL)
cout<<" + ";
else
break;
temp=temp->next;
}
cout<<endl<<endl;
}
sum(first[0],first[1]);
return 0;
}

No comments:

Post a Comment