调了好久啊……问题不少,自己看看注释吧。
#include <iostream>
using namespace std;
struct point
{
double pi;
double qi;
point* next;
};
class line
{
point* head;
int size;
public:
int gets() { return size; }
point* getp() { return head; }//名字改成getHead更好理解吧
line(int t = 0)
{
size = t;
head = new point;
}
line(line & l)
{
point* p = new point;
head = p;//光把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++; }
else if (t1->qi == t2->qi)
{ t->qi = t1->qi; t->pi = t1->pi + t2->pi; t1 = t1->next; t2 = t2->next; l.size++; }
else if (t1->qi < t2->qi)
{ t->qi = t2->qi; t->pi = t2->pi; t2 = t2->next; l.size++; }
//上面第二句if 成立的时候,t1和t2可能已经到尽头成为NULL了
//而最后的if继续访问t1->qi或t2->qi,明显是非法的。改成else if就不会访问了。
}
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;//不new哪里有空间
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;
}
};
int main() {
line l1(3);
cin >> l1;
//line l2(l1)
line l2(3);
cin >> l2;
line l3 = l1 + l2;
cout << endl << endl << l3;
}
图片版: