struct point
{
double pi;
double qi;
point* next;
};
class line
{
point* head;
int size;
public:
int gets() { return size;}
point* getp() { return head;}
line(int t = 0)
{
size = t;
head = new point;
}
line(line & l)
{
point* p = head;
point* q = l.getp();
size = l.gets();
while (q->next != NULL)
{
p->next = new point;
p = p->next;
q = q->next;
p->pi = q->pi;
p->qi = q->qi;
}
p->next = NULL;
}
friend line operator+(line & l1, line & l2)
{
line l;
point* t = l.head;
point* t1 = l1.head->next;
point* t2 = l2.head->next;
while (t1 && t2)
{
t->next = new point;
t = t->next;
if ( t1->qi > t2->qi ) { t->qi = t1->qi; t->pi = t1->pi; t1 = t1->next; l.size ++; }
if ( t1->qi == t2->qi ) { t->qi = t1->qi; t->pi = t1->pi + t2->pi; t1 = t1->next; t2 = t2->next; l.size ++; }
if ( t1->qi < t2->qi ) { t->qi = t2->qi; t->pi = t2->pi; t2 = t2->next; l.size ++; }
}
while (t1)
{
t->next = new point;
t = t->next;
t->qi = t1->qi;
t->pi = t1->pi;
t1 = t1->next;
l.size ++;
}
while (t2)
{
t->next = new point;
t = t->next;
t->qi = t2->qi;
t->pi = t2->pi;
t2 = t2->next;
l.size ++;
}
t->next = NULL;
t = NULL;
return l;
}
/*friend line operator-(const line & l1,const line & l2)
{
line l(0);
point* t = l.head;
point* t1 = l1.head->next;
point* t2 = l2.head->next;
while (t1 && t2)
{
t->next = new point;
t = t->next;
if ( t1->qi > t2->qi ) { t->qi = t1->qi; t->pi = t1->pi; t1 = t1->next; l.size ++; }
if ( t1->qi == t2->qi ) { t->qi = t1->qi; t->pi = t1->pi - t2->pi; t1 = t1->next; t2 = t2->next; l.size ++; }
if ( t1->qi < t2->qi ) { t->qi = t2->qi; t->pi = -t2->pi; t2 = t2->next; l.size ++; }
}
while (t1)
{
t->next = new point;
t = t->next;
t->qi = t1->qi;
t->pi = t1->pi;
t1 = t1->next;
l.size ++;
}
while (t2)
{
t->next = new point;
t = t->next;
t->qi = t2->qi;
t->pi = -t2->pi;
t2 = t2->next;
l.size ++;
}
delete t;
t = NULL;
return l;
}
friend line operator*(const line & l1, const line & l2)
{
}*/
friend istream& operator>>(istream & input,line & l)
{
point* pt=l.head;
cout << "Please input elements with descending order" << endl;
for ( int i = 0 ; i < l.size ; i ++ )
{
pt->next = new point;
pt = pt->next;
cout << "Please input the " << i+1 <<"th element's pi" << endl;
input >> pt->pi;
cout << "Please input the " << i+1 <<"th element's qi" << endl;
input >> pt->qi;
}
pt->next = NULL;
return input;
}
friend ostream& operator<<(ostream & output,line & l)
{
point* pt = l.head->next;
int b = 0;
for ( int i = 0; i < l.size-1; i ++)
{
if (pt->pi)
{
cout << pt->pi << "X^" << pt->qi << " + ";
pt = pt->next;
b ++;
}
else
{
pt = pt->next;
continue;
}
}
if (pt->pi)
{
cout << pt->pi << "X^" << pt->qi << endl;
b ++;
}
if (!b) cout << 0 << endl;
return output;
}
};