weixin_41333159 2018-10-17 01:43 采纳率: 100%

#include "pch.h"
#include
using namespace std;
struct polynomial
{
int coe;
int exp;
};
typedef struct polynomial
poly;
class polynode
{
private:
poly rear;
int number;
void attach(int x, int y);
void connection(int x, int y, poly& p);
public:
polynode();
polynode(const polynode &p);
polynode operator+(polynode &p);
polynode& operator=(const polynode& p);
void printpoly();
~polynode();
};
polynode::polynode()
{
number = 0;
}
polynode::polynode(const polynode &p)
{
int k = p.number;
while (k--)
{

``````    rear->link = new polynomial;
}
``````

}

void polynode::attach(int x, int y)
{
poly p;
p = new struct polynomial;
p->coe = x;
p->exp = y;
rear = p;
number++;
}
void polynode::connection(int x, int y, poly& p)
{
poly t;
t = new struct polynomial;
t->coe = x;
t->exp = y;
number++;
}
//读入函数试下来因该是没有问题的
{
int num,ix,iy;
poly p;
cout << "有多少项？" << endl;
cin >> num;
cout << "请输入：" << endl;
cin >> ix >> iy;
attach(ix, iy);
num--;
while (num--)
{
cin >> ix >> iy;
if (p->exp == iy)
{
p->coe += ix;
p->exp = iy;
}
while (p->exp > iy)
{
if (p == rear)
{
attach(ix, iy);
break;
}
else
{
else
connection(ix, iy, p); break;
}
}
if (p->exp < iy)

``````}
``````

}
//加法算法试下来也没有
polynode polynode::operator+(polynode &p)
{
int m, n;
static polynode result;
poly b, q;
m = number;
n = p.number;
while (m != 0 && n != 0)
{
if (b->exp == q->exp)
{
result.attach(b->coe + q->coe, b->exp);
m--; n--;
}
else {
if (b->exp > q->exp)
{
result.attach(b->coe, b->exp);
m--;
}
else {

``````                result.attach( q->coe, q->exp);
n--;
}
}
}
if (m == 0 && n == 0)
{
result.printpoly();
return result;
}
else {
if (m == 0)
{
while (n--)
{
result.attach(q->coe, q->exp);
}
}
else
while (m--)
{
result.attach(b->coe, b->exp);
}
result.printpoly();
return result;
}
``````

}
polynode& polynode::operator=(const polynode& p)
{

``````int k = p.number;
poly temp;

while (k--)
{

}

return *this;
``````

}
void polynode::printpoly()
{
cout << "print:" << endl;
int m;
m = number;
while (m--)
{
cout << p->coe << p->exp << " ";
}
cout << endl;
}
polynode::~polynode()
{

``````while (number--) {
delete p;
}
``````

}

int main()
{
polynode p1,p2,p3;
p1.printpoly();
p3=p1 + p2;
p3.printpoly();
system("pause");
}

• 写回答

#### 1条回答默认 最新

• threenewbee 2018-10-17 02:35
关注

polynode::polynode(const polynode &p)
{
int k = p.number;
while (k--)
{

``````    rear->link = new polynomial;
}
``````

}

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论