一元多项式的乘法与加法运算,C语言完整代码
时间: 2023-06-19 11:02:54 浏览: 195
以下是一元多项式的乘法与加法运算的 C 语言完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct node {
int coef; // 系数
int exp; // 指数
struct node *next;
};
typedef struct node LinkNode;
typedef LinkNode *LinkList;
// 创建多项式
void create_polynomial(LinkList *L) {
int coef, exp;
LinkNode *p, *r = *L;
printf("请输入一元多项式的系数和指数(输入0 0结束):\n");
scanf("%d %d", &coef, &exp);
while (coef != 0 || exp != 0) {
p = (LinkNode *)malloc(sizeof(LinkNode));
p->coef = coef;
p->exp = exp;
r->next = p;
r = p;
printf("请输入一元多项式的系数和指数(输入0 0结束):\n");
scanf("%d %d", &coef, &exp);
}
r->next = NULL;
}
// 输出多项式
void print_polynomial(LinkList L) {
LinkNode *p = L->next;
while (p) {
if (p->exp == 0)
printf("%d", p->coef);
else if (p->exp == 1)
printf("%dx", p->coef);
else
printf("%dx^%d", p->coef, p->exp);
p = p->next;
if (p)
printf(" + ");
}
printf("\n");
}
// 多项式相加
void add_polynomial(LinkList A, LinkList B, LinkList *C) {
LinkNode *pa = A->next, *pb = B->next;
LinkNode *pc, *rc = (LinkNode *)malloc(sizeof(LinkNode));
*C = rc;
while (pa && pb) {
if (pa->exp == pb->exp) {
int sum = pa->coef + pb->coef;
if (sum != 0) {
pc = (LinkNode *)malloc(sizeof(LinkNode));
pc->coef = sum;
pc->exp = pa->exp;
rc->next = pc;
rc = pc;
}
pa = pa->next;
pb = pb->next;
} else if (pa->exp > pb->exp) {
pc = (LinkNode *)malloc(sizeof(LinkNode));
pc->coef = pa->coef;
pc->exp = pa->exp;
rc->next = pc;
rc = pc;
pa = pa->next;
} else {
pc = (LinkNode *)malloc(sizeof(LinkNode));
pc->coef = pb->coef;
pc->exp = pb->exp;
rc->next = pc;
rc = pc;
pb = pb->next;
}
}
while (pa) {
pc = (LinkNode *)malloc(sizeof(LinkNode));
pc->coef = pa->coef;
pc->exp = pa->exp;
rc->next = pc;
rc = pc;
pa = pa->next;
}
while (pb) {
pc = (LinkNode *)malloc(sizeof(LinkNode));
pc->coef = pb->coef;
pc->exp = pb->exp;
rc->next = pc;
rc = pc;
pb = pb->next;
}
rc->next = NULL;
}
// 多项式相乘
void multiply_polynomial(LinkList A, LinkList B, LinkList *C) {
LinkNode *pa = A->next, *pb, *pc, *pd, *rd = (LinkNode *)malloc(sizeof(LinkNode));
*C = rd;
while (pa) {
pb = B->next;
pc = (LinkNode *)malloc(sizeof(LinkNode));
if (!pc) {
printf("内存分配失败!\n");
exit(1);
}
pc->coef = pa->coef;
pc->exp = pa->exp;
pc->next = NULL;
pd = rd;
while (pb) {
int coef = pa->coef * pb->coef;
int exp = pa->exp + pb->exp;
while (pd->next && pd->next->exp > exp)
pd = pd->next;
if (pd->next && pd->next->exp == exp) {
if (pd->next->coef + coef != 0)
pd->next->coef += coef;
else {
LinkNode *temp = pd->next;
pd->next = temp->next;
free(temp);
}
} else {
LinkNode *temp = (LinkNode *)malloc(sizeof(LinkNode));
if (!temp) {
printf("内存分配失败!\n");
exit(1);
}
temp->coef = coef;
temp->exp = exp;
temp->next = pd->next;
pd->next = temp;
}
pb = pb->next;
pd = rd;
}
pa = pa->next;
}
}
int main() {
LinkList A, B, C;
printf("请输入第一个多项式:\n");
A = (LinkNode *)malloc(sizeof(LinkNode));
A->next = NULL;
create_polynomial(&A);
printf("第一个多项式为:");
print_polynomial(A);
printf("请输入第二个多项式:\n");
B = (LinkNode *)malloc(sizeof(LinkNode));
B->next = NULL;
create_polynomial(&B);
printf("第二个多项式为:");
print_polynomial(B);
add_polynomial(A, B, &C);
printf("两个多项式相加为:");
print_polynomial(C);
multiply_polynomial(A, B, &C);
printf("两个多项式相乘为:");
print_polynomial(C);
return 0;
}
```
这段代码实现了创建多项式、输出多项式、多项式相加和多项式相乘四个功能。其中,多项式相加使用了链表的合并排序思想,多项式相乘使用了链表的插入排序思想。
阅读全文