C# · 12月 20, 2021

多项式的储存与运算c++

#include

using namespace std;

template

struct Node

{

float coef;//系数

int exp;//指数

Node *next;//指针

};

template

class Linklist

{

Node *head;

public:

Linklist(); //不含参数构造

Linklist(T a[],T b[],int i);//含参

~Linklist(); // 析构函数

void output();//输出

void sort();//排序

void add(Linklist &b);//多项式+

void sub(Linklist &b);//多项式-

};

template

void Linklist::sort()

{

Node *p = head->next;

Node *q = p->next;

while (p&&q)

{

if (p->exp exp)

{

q = q->next;

p = p->next;

}

else if (p->exp == q->exp)

{

p->coef = p->coef + q->coef;

p->next = q->next;

delete q;

q = p->next;

}

else

{

T a = p->coef;

T b = p->exp;

p->coef = q->coef;

p->exp = q->exp;

q->coef = a;

q->exp = b;

p = q;

q = q->next;

}

}

}

template

void Linklist::add(Linklist &b)

{

Node *pre_pa = this->head; Node *pa = pre_pa->next;

Node *pre_pb = b.head; Node *pb = pre_pb->next;

while (pa&&pb)

{

if (pa->exp exp)//如果指数小于指数b

{

pre_pa = pa;

pa = pa->next;

}

else if (pa->exp == pb->exp)//等于

{

pa->coef = pa->coef + pb->coef;

if (pa->coef == 0)//系数相加为0

{

pre_pa->next = pa->next;

delete pa;

pa = pre_pa->next;

}

else//不为0

{

pre_pa = pa;

pa = pa->next;

}

pre_pb->next = pb->next;//删除pb结点

delete pb;

pb = pre_pb->next;

}

else//pb插入pa前

{

Node *r = pb->next;

pre_pa->next = pb;

pb->next = pa;

pre_pa = pb;

pb = r;

pre_pb->next = pb;

}

}

if (pb)

{

pa->next = pb;

delete b.head;

}

}

template

void Linklist::sub(Linklist &b)

{

Node *pre_pa = this->head; Node *pa = pre_pa->next;

Node *pb = b.head->next; Node *pre_pb = b.head;

while (pa&&pb)

{

if (pa->exp exp)//如果指数a小于指数b

{

pre_pa = pa;

pa = pa->next;

}

else if (pa->exp == pb->exp)//等于

{

pa->coef = pa->coef – pb->coef;

if (pa->coef == 0)//系数相减为0

{

pre_pa->next = pa->next;

delete pa;

pa = pre_pa->next;

}

else//不为0

{

pre_pa = pa;

pa = pa->next;

}

pre_pb->next = pb->next;//删除pb结点

delete pb;

pb = pre_pb->next;

}

else//pb插入pa前

{

Node *r = pb->next;

pre_pa->next = pb;

pb->coef = -pb->coef;

pb->next = pa;

pre_pa = pb;

pb = r;

pre_pb->next = pb;

}

}

if (pb)

{

pre_pa->next = pb;

delete b.head;

}

}

template

Linklist::Linklist()

{

head = new Node;

head->next = NULL;

}

template

Linklist::Linklist(T a[],int i)

{

head = new Node;

Node *p = head;

for (int j = 0; j < i; j++)

{

Node *s = new Node;

s->coef = a[j];

s->exp = b[j];

p->next = s;

p = s;

;

}

p->next = NULL;

}

template

Linklist::~Linklist()

{

Node *p = head;

while (p)

{

Node *q = p;

p = p->next;

delete q;

}

head = NULL;

}

template

void Linklist::output()

{

Node *p;

p = head->next;

while (p)

{

cout << "(" <coef << "," <exp << ")";

p = p->next;

if (p)

cout << "+";

}

cout << endl;

}

int main()

{

int a[4] = { 3,7,5,9 };

int b[4] = { 1,17,18 };

int c[3] = { -9,8,22 };

int d[3] = { 8,1,7 };

Linklist x1(a,b,4),x2(c,d,3),x3(a,x4(c,3);

cout << "原多项式x1为";

x1.output();

cout << "原多项式x2为";

x2.output();

x1.sort();

x2.sort();

cout << "多项式相加为";

x1.add(x2);

x1.output();

x3.sort();

x4.sort();

cout << "多项式相减为";

x3.sub(x4);

x3.output();

system(“pause”);

return 0;

}

测试结果: