用于记录答案
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。
输入样例:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
输出样例:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0
include<stdio.h>
include<stdlib.h>
typedef struct Node *PtrToNode;
struct Node{
int Exponential;
int Coefficient;
PtrToNode Next;
};
typedef PtrToNode Polynomial;
void Attach(int c, int e, Polynomial pRear) {
Polynomial P;
P = (Polynomial)malloc(sizeof(struct Node));
P->Coefficient = c;
P->Exponential = e;
P->Next = NULL;
(pRear)->Next = P;
(*pRear) = P;
}
Polynomial readPolynomial() {
Polynomial P, Rear, t;
int num, c, e;
P = (Polynomial)malloc(sizeof(struct Node));
P->Next = NULL;
Rear = P;
scanf("%d", &num);
while (num--) {
scanf("%d %d", &c, &e);
Attach(c, e, &Rear);
}
t = P;P = P->Next; free(t);
return P;
}
Polynomial plus(Polynomial p1, Polynomial p2) {
Polynomial po1 = p1;
Polynomial po2 = p2;
Polynomial sum = (Polynomial)malloc(sizeof(struct Node));
sum->Next = NULL;
Polynomial plus = sum;
Polynomial t;
while(po1!=NULL && po2!=NULL) {
if(po1->Exponential==po2->Exponential) {
int sum = po1->Coefficient + po2->Coefficient;
if(sum){
Attach(sum, po1->Exponential, &plus);
}
po1 = po1->Next;
po2 = po2->Next;
} else if (po1->Exponential >po2->Exponential) {
Attach(po1->Coefficient, po1->Exponential, &plus);
po1 = po1->Next;
} else {
Attach(po2->Coefficient, po2->Exponential, &plus);
po2 = po2->Next;
}
}
while(po1!=NULL) {
Attach(po1->Coefficient, po1->Exponential, &plus);
po1 = po1->Next;
}
while(po2!=NULL) {
Attach(po2->Coefficient, po2->Exponential, &plus);
po2 = po2->Next;
}
plus ->Next = NULL;
t = sum;
sum = sum->Next;
free(t);
return sum;
}
void addToPolynomial(Polynomial *P,Polynomial *product) {
Polynomial temp = *product;
Polynomial unitP = *P;
while (temp->Next !=NULL) {
if (temp->Next->Exponential < unitP->Exponential) {
unitP->Next = temp->Next;
temp->Next = unitP;
return;
} else if (temp->Next->Exponential > unitP->Exponential) {
temp = temp->Next;
} else {
int t = temp->Next->Coefficient + unitP->Coefficient;
if (t == 0) {
temp->Next = temp->Next->Next;
free(unitP);
return;
} else {
temp->Next->Coefficient = t;
free(unitP);
return;
}
}
}
if (temp->Next == NULL) {
temp->Next = unitP;
}
}
void printPolynomial(Polynomial pol) {
int flag = 0;
if (pol == NULL) {
printf("%d %d", 0, 0);
}
while(pol != NULL){
if (!flag) {
printf("%d %d", pol->Coefficient, pol->Exponential);
flag = 1;
} else{
printf(" %d %d", pol->Coefficient, pol->Exponential);
}
pol = pol->Next;
}
}
Polynomial multiply(Polynomial po1, Polynomial po2) {
Polynomial product,p1, p2, rear, t;
p1 = po1;
p2 = po2;
if (p1==NULL || p2 ==NULL) {
return NULL;
}
product = (Polynomial)malloc(sizeof(struct Node));
rear = product;
rear->Next = NULL;
while(po2!=NULL) {
Polynomial P = malloc(sizeof(struct Node));
P->Coefficient = po1->Coefficient * po2->Coefficient;
P->Exponential = po1->Exponential + po2->Exponential;
P->Next = NULL;
rear->Next = P;
po2 = po2->Next;
rear = rear->Next;
}
while (po1->Next!=NULL) {
po1 = po1->Next;
po2 = p2;
while (po2!=NULL) {
Polynomial P = malloc(sizeof(struct Node));
P->Coefficient = po1->Coefficient * po2->Coefficient;
P->Exponential = po1->Exponential + po2->Exponential;
P->Next = NULL;
addToPolynomial(&P, &product);
po2 = po2->Next;
}
}
if (product->Next == NULL) {
product->Exponential = 0;
product->Coefficient = 0;
} else{
t = product;
product = product->Next;
free(t);
}
return product;
}
int main() {
//todo 判0多项式输入,常熟多项式
Polynomial P1 = readPolynomial();
Polynomial P2 = readPolynomial();
Polynomial Sum = plus(P1, P2);
Polynomial Product = multiply(P1, P2);
printPolynomial(Product);
printf("\n");
printPolynomial(Sum);
return 0;
}