这里由于读题不仔细,导致在实现的时候把多项式表达成了不同数值的系数与指数的操作,在实现的时候也走了弯路,只实现了加法,乘法觉得难以进行。回过头来仔细读题才发现是同一x的多项式操作。对于后者的实现就放到下一篇吧。这一篇就先凑合看看。
#include <iostream>
#include <malloc.h>
using namespace std;
typedef int Elementtype;
struct polynomial {
Elementtype count;
Elementtype coenum;
Elementtype index;
polynomial *next;
};
typedef polynomial *POLY;
POLY Add(POLY P1, POLY P2) {
POLY P3;
P3 = (POLY)malloc(sizeof(polynomial));
polynomial *s, *r = P3, *p = P1->next, *q = P2->next;
while (p != NULL && q != NULL) {
if (p->count == q->count && p->index == q->index) {
s = (polynomial *)malloc(sizeof(polynomial));
s->coenum = p->coenum + q->coenum;
s->count = p->count;
s->index = p->index;
r->next = s;
r = s;
p = p->next;
q = q->next;
} else if (p->index <= q->index) {
s = (polynomial *)malloc(sizeof(polynomial));
s->coenum = p->coenum;
s->count = p->count;
s->index = p->index;
r->next = s;
r = s;
p = p->next;
} else {
s = (polynomial *)malloc(sizeof(polynomial));
s->coenum = q->coenum;
s->count = q->count;
s->index = q->index;
r->next = s;
r = s;
q = q->next;
}
}
while (p != NULL) {
s = (polynomial *)malloc(sizeof(polynomial));
s->coenum = p->coenum;
s->count = p->count;
s->index = p->index;
r->next = s;
r = s;
p = p->next;
}
while (q != NULL) {
s = (polynomial *)malloc(sizeof(polynomial));
s->coenum = q->coenum;
s->count = q->count;
s->index = q->index;
r->next = s;
r = s;
q = q->next;
}
return P3;
}
int main() {
POLY P1, P2;
P1 = (POLY)malloc(sizeof(polynomial));
P2 = (POLY)malloc(sizeof(polynomial));
polynomial *s1, *r1 = P1, *print1 = P1;
polynomial *s2, *r2 = P2, *print2 = P2;
//测试数据
Elementtype a1[5] = {1, 2, 3, 4, 5};
Elementtype c1[5] = {1, 2, 3, 4, 5};
Elementtype b1[5] = {1, 3, 6, 3, 4};
//测试数据
Elementtype a2[5] = {1, 2, 4, 5, 6};
Elementtype c2[5] = {1, 2, 3, 4, 5};
Elementtype b2[5] = {2, 4, 7, 3, 5};
for (int i = 0; i < 5; i++) {
s1 = (polynomial *)malloc(sizeof(polynomial));
s1->count = a1[i];
s1->coenum = b1[i];
s1->index = c1[i];
r1->next = s1;
r1 = s1;
}
r1->next = NULL;
for (int j = 0; j < 5; j++) {
s2 = (polynomial *)malloc(sizeof(polynomial));
s2->count = a2[j];
s2->coenum = b2[j];
s2->index = c2[j];
r2->next = s2;
r2 = s2;
}
r2->next = NULL;
cout << "The First Polynomial is :" << endl;
while (print1->next != NULL) {
cout << print1->next->coenum << "*" << print1->next->count << "^" << print1->next->index ;
if (print1->next->next != NULL)
cout << "+";
print1 = print1->next;
}
cout << "\n" << "The Second Polynomial is :" << endl;
while (print2->next != NULL) {
cout << print2->next->coenum << "*" << print2->next->count << "^" << print2->next->index ;
if (print2->next->next != NULL)
cout << "+";
print2 = print2->next;
}
POLY P3;
P3 = Add(P1, P2);
polynomial *print3 = P3;
cout << "\n" << "The Sum of Polynomial is :" << endl;
while (print3->next != NULL) {
cout << print3->next->coenum << "*" << print3->next->count << "^" << print3->next->index ;
if (print3->next->next != NULL)
cout << "+";
print3 = print3->next;
}
return 0;
}
主要参考: 《数据结构与算法》(第五版)张岩
《数据结构考研复习指导》(王道)