用c语言写一个数据结构的一元多项式计算器实现加法减法乘法的功能
时间: 2023-11-18 21:04:19 浏览: 106
好的,以下是一个用 C 语言实现的一元多项式计算器,包括加法、减法和乘法功能。它使用链表来存储多项式,每个节点表示一个项,包括系数和指数。其中,每个节点都有一个指向下一个节点的指针。代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
/* 多项式项的结构体 */
typedef struct PolyNode {
int coef; // 系数
int expn; // 指数
struct PolyNode* next; // 指向下一个节点的指针
} PolyNode, *PolyList;
/* 创建多项式 */
void createPoly(PolyList* L) {
int n, coef, expn;
PolyNode* rear = *L;
printf("请输入多项式的项数:");
scanf("%d", &n);
printf("请依次输入每一项的系数和指数:\n");
for (int i = 0; i < n; i++) {
scanf("%d %d", &coef, &expn);
PolyNode* p = (PolyNode*)malloc(sizeof(PolyNode));
p->coef = coef;
p->expn = expn;
p->next = NULL;
rear->next = p;
rear = p;
}
}
/* 输出多项式 */
void printPoly(PolyList L) {
PolyNode* p = L->next;
while (p != NULL) {
printf("%d x^%d ", p->coef, p->expn);
if (p->next != NULL) {
printf("+ ");
}
p = p->next;
}
printf("\n");
}
/* 多项式相加 */
void addPoly(PolyList La, PolyList Lb, PolyList* Lc) {
PolyNode *pa = La->next, *pb = Lb->next, *pc;
*Lc = (PolyNode*)malloc(sizeof(PolyNode));
pc = *Lc;
while (pa != NULL && pb != NULL) {
if (pa->expn < pb->expn) {
pc->next = pa;
pc = pa;
pa = pa->next;
} else if (pa->expn > pb->expn) {
pc->next = pb;
pc = pb;
pb = pb->next;
} else {
int sum = pa->coef + pb->coef;
if (sum != 0) {
PolyNode* p = (PolyNode*)malloc(sizeof(PolyNode));
p->coef = sum;
p->expn = pa->expn;
pc->next = p;
pc = p;
}
pa = pa->next;
pb = pb->next;
}
}
pc->next = (pa != NULL) ? pa : pb;
}
/* 多项式相减 */
void subPoly(PolyList La, PolyList Lb, PolyList* Lc) {
PolyNode *pa = La->next, *pb = Lb->next, *pc;
*Lc = (PolyNode*)malloc(sizeof(PolyNode));
pc = *Lc;
while (pa != NULL && pb != NULL) {
if (pa->expn < pb->expn) {
pc->next = pa;
pc = pa;
pa = pa->next;
} else if (pa->expn > pb->expn) {
PolyNode* p = (PolyNode*)malloc(sizeof(PolyNode));
p->coef = -pb->coef;
p->expn = pb->expn;
pc->next = p;
pc = p;
pb = pb->next;
} else {
int diff = pa->coef - pb->coef;
if (diff != 0) {
PolyNode* p = (PolyNode*)malloc(sizeof(PolyNode));
p->coef = diff;
p->expn = pa->expn;
pc->next = p;
pc = p;
}
pa = pa->next;
pb = pb->next;
}
}
while (pa != NULL) {
pc->next = pa;
pc = pa;
pa = pa->next;
}
while (pb != NULL) {
PolyNode* p = (PolyNode*)malloc(sizeof(PolyNode));
p->coef = -pb->coef;
p->expn = pb->expn;
pc->next = p;
pc = p;
pb = pb->next;
}
}
/* 多项式相乘 */
void mulPoly(PolyList La, PolyList Lb, PolyList* Lc) {
PolyNode *pa = La->next, *pb = Lb->next, *pc = NULL, *temp;
*Lc = (PolyNode*)malloc(sizeof(PolyNode));
(*Lc)->next = NULL;
while (pa != NULL) {
pb = Lb->next;
while (pb != NULL) {
int coef = pa->coef * pb->coef;
int expn = pa->expn + pb->expn;
temp = (*Lc)->next;
pc = *Lc;
while (temp != NULL && temp->expn > expn) {
pc = temp;
temp = temp->next;
}
if (temp != NULL && temp->expn == expn) {
temp->coef += coef;
} else {
PolyNode* p = (PolyNode*)malloc(sizeof(PolyNode));
p->coef = coef;
p->expn = expn;
p->next = temp;
pc->next = p;
}
pb = pb->next;
}
pa = pa->next;
}
}
int main() {
PolyList L1, L2, L3;
/* 创建多项式 */
L1 = (PolyNode*)malloc(sizeof(PolyNode));
L2 = (PolyNode*)malloc(sizeof(PolyNode));
createPoly(&L1);
createPoly(&L2);
/* 输出多项式 */
printf("多项式 1:");
printPoly(L1);
printf("多项式 2:");
printPoly(L2);
/* 相加 */
addPoly(L1, L2, &L3);
printf("多项式 1 + 多项式 2:");
printPoly(L3);
/* 相减 */
subPoly(L1, L2, &L3);
printf("多项式 1 - 多项式 2:");
printPoly(L3);
/* 相乘 */
mulPoly(L1, L2, &L3);
printf("多项式 1 * 多项式 2:");
printPoly(L3);
return 0;
}
```
注:此代码只是一个简单的实现,可能存在一些问题和不足。
阅读全文