c语言 求两个多项式的乘积(1)编写函数实现一个多项式的输入,返回多项式链表的头指针。(2)编写函数实现两个多项式相乘,返回结果多项式链表的头指针。(3)编写函数输出一个多项式的二元组序列。(4)在main函数
时间: 2024-02-25 07:54:37 浏览: 66
中调用以上函数,实现两个多项式的输入和输出结果。
以下是实现代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct PolyNode *PtrToPolyNode;
struct PolyNode {
int coef; // 系数
int expon; // 指数
PtrToPolyNode next; // 下一项
};
typedef PtrToPolyNode Polynomial;
Polynomial CreatePoly(); // 创建多项式
Polynomial PolyAdd(Polynomial P1, Polynomial P2); // 多项式加法
Polynomial PolyMultiply(Polynomial P1, Polynomial P2); // 多项式乘法
void PrintPoly(Polynomial P); // 输出多项式
int main()
{
Polynomial P1, P2, PMul;
P1 = CreatePoly();
P2 = CreatePoly();
PMul = PolyMultiply(P1, P2);
printf("P1: ");
PrintPoly(P1);
printf("P2: ");
PrintPoly(P2);
printf("P1 * P2: ");
PrintPoly(PMul);
return 0;
}
Polynomial CreatePoly()
{
int n, coef, expon;
PtrToPolyNode p, rear, head;
head = (Polynomial)malloc(sizeof(struct PolyNode));
head->next = NULL;
rear = head;
scanf("%d", &n);
while (n--) {
scanf("%d %d", &coef, &expon);
p = (PtrToPolyNode)malloc(sizeof(struct PolyNode));
p->coef = coef;
p->expon = expon;
p->next = NULL;
rear->next = p;
rear = p;
}
return head;
}
Polynomial PolyAdd(Polynomial P1, Polynomial P2)
{
Polynomial P, rear, t1, t2, p;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->next = NULL;
rear = P;
t1 = P1->next;
t2 = P2->next;
while (t1 && t2) {
if (t1->expon > t2->expon) {
p = (PtrToPolyNode)malloc(sizeof(struct PolyNode));
p->coef = t1->coef;
p->expon = t1->expon;
p->next = NULL;
rear->next = p;
rear = p;
t1 = t1->next;
} else if (t1->expon < t2->expon) {
p = (PtrToPolyNode)malloc(sizeof(struct PolyNode));
p->coef = t2->coef;
p->expon = t2->expon;
p->next = NULL;
rear->next = p;
rear = p;
t2 = t2->next;
} else {
if (t1->coef + t2->coef != 0) {
p = (PtrToPolyNode)malloc(sizeof(struct PolyNode));
p->coef = t1->coef + t2->coef;
p->expon = t1->expon;
p->next = NULL;
rear->next = p;
rear = p;
}
t1 = t1->next;
t2 = t2->next;
}
}
while (t1) {
p = (PtrToPolyNode)malloc(sizeof(struct PolyNode));
p->coef = t1->coef;
p->expon = t1->expon;
p->next = NULL;
rear->next = p;
rear = p;
t1 = t1->next;
}
while (t2) {
p = (PtrToPolyNode)malloc(sizeof(struct PolyNode));
p->coef = t2->coef;
p->expon = t2->expon;
p->next = NULL;
rear->next = p;
rear = p;
t2 = t2->next;
}
return P;
}
Polynomial PolyMultiply(Polynomial P1, Polynomial P2)
{
Polynomial P, rear, t1, t2, p;
int coef, expon;
if (!P1 || !P2)
return NULL;
t1 = P1->next;
t2 = P2->next;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->next = NULL;
rear = P;
while (t2) {
p = (Polynomial)malloc(sizeof(struct PolyNode));
p->coef = t1->coef * t2->coef;
p->expon = t1->expon + t2->expon;
p->next = NULL;
rear->next = p;
rear = p;
t2 = t2->next;
}
t1 = t1->next;
while (t1) {
t2 = P2->next;
rear = P;
while (t2) {
expon = t1->expon + t2->expon;
coef = t1->coef * t2->coef;
while (rear->next && rear->next->expon > expon) {
rear = rear->next;
}
if (rear->next && rear->next->expon == expon) {
if (rear->next->coef + coef != 0) {
rear->next->coef += coef;
} else {
p = rear->next;
rear->next = p->next;
free(p);
}
} else {
p = (Polynomial)malloc(sizeof(struct PolyNode));
p->coef = coef;
p->expon = expon;
p->next = rear->next;
rear->next = p;
}
t2 = t2->next;
}
t1 = t1->next;
}
p = P;
P = P->next;
free(p);
return P;
}
void PrintPoly(Polynomial P)
{
if (!P) {
printf("0 0\n");
return;
}
PtrToPolyNode p = P->next;
if (!p) {
printf("0 0\n");
return;
}
while (p) {
printf("%d %d", p->coef, p->expon);
p = p->next;
if (p) {
printf(" ");
} else {
printf("\n");
}
}
}
```
以上代码实现了多项式的输入、加法、乘法和输出操作。在 `main` 函数中,调用 `CreatePoly()` 函数输入两个多项式,再调用 `PolyMultiply()` 函数求出它们的乘积,最后分别调用 `PrintPoly()` 函数输出三个多项式。
阅读全文