设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。
#include<iostream>
using namespace std;
typedef struct PolyNode* Polynomial;
struct PolyNode {
int coef;
int expon;
Polynomial link;
};
Polynomial ReadPoly();
Polynomial Mult(Polynomial, Polynomial);
void PrintPoly(Polynomial);
Polynomial Sum(Polynomial, Polynomial);
void Attach(int, int, Polynomial*);
int main()
{
Polynomial P1, P2, PP, PS;
P1 = ReadPoly();
P2 = ReadPoly();
PP = Mult(P1, P2);
PrintPoly(PP);
PS = Sum(P1, P2);
PrintPoly(PS);
return 0;
}
Polynomial ReadPoly()
{
Polynomial P, Rear, t;
int c, e, N;
cin >> N;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->link = NULL;
Rear = P;
while (N--) {
cin >> c >> e;
Attach(c, e, &Rear);
}
Rear->link = NULL;
t = P;
P = P->link;
free(t);
return P;
}
void Attach(int c, int e, Polynomial* pRear)
{
Polynomial P;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->coef = c;
P->expon = e;
P->link = NULL;
(*pRear)->link = P;
*pRear = P;
}
Polynomial Sum(Polynomial P1, Polynomial P2)
{
Polynomial P, Rear, t; //未使用t,c语言是值传递,不会改变指针位置.
int sum;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->link = NULL;
Rear = P;
while (P1 && P2) {
if (P1->expon > P2->expon) {
Attach(P1->coef, P1->expon, &Rear);
P1 = P1->link;
}
else if (P1->expon < P2->expon) {
Attach(P2->coef, P2->expon ,&Rear);
P2 = P2->link;
}
else {
sum = P1->coef + P2->coef;
if (sum) Attach(sum, P2->expon, &Rear); //注意sum为0的情况!合并同类项系数为0抵消!
P1 = P1->link;
P2 = P2->link;
}
}
while (P1) { //最后项补全到结果链表中.
Attach(P1->coef, P1->expon, &Rear);
P1 = P1->link;
}
while (P2) {
Attach(P2->coef, P2->expon, &Rear);
P2 = P2->link;
}
Rear->link = NULL;
t = P;
P = P->link;
free(t); //free头节点
return P;
}
Polynomial Mult(Polynomial P1, Polynomial P2)
{
Polynomial t1, t2, P, Rear, t;
int c, e;
t1 = P1;
t2 = P2;
if (!P1 || !P2) return NULL; //P1 P2其中一个为空返回NUULL
P = (Polynomial)malloc(sizeof(struct PolyNode));
Rear = P;
while (t2) {
Attach(t1->coef * t2->coef, t1->expon + t2->expon, &Rear); //先将t1第一项乘t2各项
t2 = t2->link;
}
t1 = t1->link; //t1移至下一项
while (t1) { //t1各项(除第一项外)乘t2各项
t2 = P2; //每次循环t2重新归头
Rear = P; //每次循环尾指针归头(指数递减排列)
while (t2) { //乘t2各项
c = t1->coef * t2->coef;
e = t1->expon + t2->expon;
while (Rear->link && Rear->link->expon > e) { //下一项不为NULL且大于指数移至下一位
Rear = Rear->link;
}
if (Rear->link && Rear->link->expon == e) { //指数相等,相应指数上加系数
if (Rear->link->coef + c) { //系数不为零
Rear->link->coef += c;
}
else { //系数为零free节点
t = Rear->link;
Rear->link = t->link;
free(t);
}
}
else { //指数不等创捷节点
t = (Polynomial)malloc(sizeof(struct PolyNode));
t->coef = c;
t->expon = e;
t->link = Rear->link;
Rear->link = t;
Rear = Rear->link;
}
t2 = t2->link; //注意循环退出,t2指向下一位,否则死循环
}
t1 = t1->link; //t1指向下一位
}
t2 = P;
P = P->link;
free(t2);
return P;
}
void PrintPoly(Polynomial P)
{
int flag = 0; //第一项前不要空格
if (!P) {
cout << 0 << " " << 0 << endl;
return;
}
while (P) {
if (!flag) {
flag = 1;
}
else { //不是第一项多加空格.
cout << " ";
}
cout << P->coef << " " << P->expon;
P = P->link;
}
cout << endl;
}