#include <iostream.h>
#include <conio.h>
/*List Class Declaration -----------------------------------------------------*/
class linklist
{
private:
struct poly1
{
int coe,exp;
poly1 *link;
}*p1;
struct poly2
{
int coe,exp;
poly2 *link;
}*p2;
struct poly3
{
int coe,exp;
poly3 *link;
}*p3;
/*Function Declaration -------------------------------------------------------*/
public:
linklist();
void append( int coe1,int exp1,int select1 );
void calc();
void display();
};
/*List Constructor -----------------------------------------------------------*/
linklist::linklist()
{
p1=NULL;
p2=NULL;
p3=NULL;
}
/*Insert Node ----------------------------------------------------------------*/
void linklist::append(int coe1,int exp1,int select1)
{
if (select1==0)
{
poly1 *q1,*t1;
if( p1 == NULL )
{
p1 = new poly1;
p1->coe = coe1;
p1->exp = exp1;
p1->link = NULL;
}
else
{
q1 = p1;
while( q1->link != NULL )
q1 = q1->link;
t1 = new poly1;
t1->coe = coe1;
t1->exp = exp1;
t1->link = NULL;
q1->link = t1;
}
}
else if (select1==1)
{
poly2 *q2,*t2;
if( p2 == NULL )
{
p2 = new poly2;
p2->coe = coe1;
p2->exp = exp1;
p2->link = NULL;
}
else
{
q2 = p2;
while( q2->link != NULL )
q2 = q2->link;
t2 = new poly2;
t2->coe = coe1;
t2->exp = exp1;
t2->link = NULL;
q2->link = t2;
}
}
else if (select1==2)
{
poly3 *q3,*t3;
if( p3 == NULL )
{
p3 = new poly3;
p3->coe = coe1;
p3->exp = exp1;
p3->link = NULL;
}
else
{
q3 = p3;
while( q3->link != NULL )
q3 = q3->link;
t3 = new poly3;
t3->coe = coe1;
t3->exp = exp1;
t3->link = NULL;
q3->link = t3;
}
}
}
/*Polynomial Calculation -----------------------------------------------------*/
void linklist::calc()
{
int coe2,exp2,select2=2,count;
linklist ll;
poly1 *q1;
poly2 *q2;
for( q1 = p1 ; q1 != NULL ; q1 = q1->link )
{
count=0 ;
for( q2 = p2 ; q2 != NULL ; q2 = q2->link )
{
if(q1->exp==q2->exp)
{
coe2=q1->coe+q2->coe ;
exp2=q1->exp ;
cout << coe2 << " " << exp2 << endl ;
ll.append(coe2,exp2,select2);
count=1;
}
}
if (count==0)
{
ll.append(q1->coe,q1->exp,select2);
cout << q1->coe << " " << q1->exp << endl ;
}
}
for( q2 = p2 ; q2 != NULL ; q2 = q2->link )
{
count=0;
for( q1 = p1 ; q1 != NULL ; q1 = q1->link )
{
if(q2->exp==q1->exp)
{
count=1;
}
}
if(count==0)
{
ll.append(q2->coe,q2->exp,select2);
cout << q2->coe << " " << q2->exp << endl ;
}
}
}
/*Display Node ---------------------------------------------------------------*/
void linklist::display()
{
poly1 *q1;
cout<<endl;
for( q1 = p1 ; q1 != NULL ; q1 = q1->link )
cout << endl << "Coefficient = " << q1->coe << "\tExponent = " << q1->exp;
poly2 *q2;
cout<<endl;
for( q2 = p2 ; q2 != NULL ; q2 = q2->link )
cout << endl << "Coefficient = " << q2->coe << "\tExponent = " << q2->exp;
poly3 *q3;
cout<<endl;
for( q3 = p3 ; q3 != NULL ; q3 = q3->link )
cout << endl << "Coefficient = " << q3->coe << "\tExponent = " << q3->exp;
}
/*Main -----------------------------------------------------------------------*/
void main()
{
int looper,coe,exp,select;
linklist ll;
for(select=0;select<2;select++)
{
cout << "\nPolynomial " << (select+1) << endl ;
for (looper=0;looper<3;looper++)
{
cout << "Enter Coefficient : " ;
cin >> coe ;
cout << "Enter Exponent : " ;
cin >> exp ;
ll.append(coe,exp,select);
}
}
ll.calc();
ll.display();
getch() ;
}
coding tu aku buat utk bina program utk kira hasil tmbah polinomial,nk mskkn input tu jd la,tp mslhnye nk mskkn input ke dalam poly3 slps tmbah poly1 ngan poly2,hsil dia tu nk mskkn dlm poly3 tp xmsk pn dlm linked list poly3,bl run xde error pn,ape yg salahnye eh?tolong la otai2 putera,sy kebuntuan.....