c语言编写数据结构的一元多项式计算器,输入多项式的系数和指数,排序使其按指数升序排列,实现两个多项式的相加相减相乘,并输出相加相减相乘结果
时间: 2023-10-30 08:23:20 浏览: 99
好的,我可以为您提供一个基于链表实现的一元多项式计算器的代码。这个计算器可以实现输入多项式的系数和指数,排序使其按指数升序排列,实现两个多项式的相加相减相乘,并输出相加相减相乘结果。
以下是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int coef; // 系数
int expn; // 指数
struct node *next;
} node, *linklist;
// 创建多项式
void create_polyn(linklist *L, int n) {
*L = (linklist)malloc(sizeof(node));
(*L)->next = NULL;
linklist p, q;
q = *L;
for (int i = 0; i < n; i++) {
p = (linklist)malloc(sizeof(node));
scanf("%d %d", &(p->coef), &(p->expn));
p->next = NULL;
q->next = p;
q = p;
}
}
// 排序多项式
void sort_polyn(linklist L) {
linklist p, q, pre;
pre = L;
for (p = L->next; p != NULL; p = p->next) {
for (q = pre; q != p; q = q->next) {
if (q->next->expn > p->expn) {
linklist tmp = q->next;
q->next = p;
pre->next = p->next;
p->next = tmp;
p = tmp;
}
}
pre = p;
}
}
// 打印多项式
void print_polyn(linklist L) {
linklist p = L->next;
while (p != NULL) {
if (p->coef > 0) {
printf("%d", p->coef);
} else {
printf("(%d)", p->coef);
}
if (p->expn > 1) {
printf("x^%d", p->expn);
} else if (p->expn == 1) {
printf("x");
}
if (p->next != NULL && p->next->coef > 0) {
printf("+");
}
p = p->next;
}
printf("\n");
}
// 多项式相加
void add_polyn(linklist *L1, linklist *L2, linklist *L3) {
linklist p1, p2, p3;
p1 = (*L1)->next;
p2 = (*L2)->next;
*L3 = (linklist)malloc(sizeof(node));
(*L3)->next = NULL;
p3 = *L3;
while (p1 != NULL && p2 != NULL) {
if (p1->expn < p2->expn) {
p3->next = p1;
p3 = p1;
p1 = p1->next;
} else if (p1->expn > p2->expn) {
p3->next = p2;
p3 = p2;
p2 = p2->next;
} else {
int sum = p1->coef + p2->coef;
if (sum != 0) {
linklist tmp = (linklist)malloc(sizeof(node));
tmp->coef = sum;
tmp->expn = p1->expn;
tmp->next = NULL;
p3->next = tmp;
p3 = tmp;
}
p1 = p1->next;
p2 = p2->next;
}
}
while (p1 != NULL) {
p3->next = p1;
p3 = p1;
p1 = p1->next;
}
while (p2 != NULL) {
p3->next = p2;
p3 = p2;
p2 = p2->next;
}
}
// 多项式相减
void sub_polyn(linklist *L1, linklist *L2, linklist *L3) {
linklist p1, p2, p3;
p1 = (*L1)->next;
p2 = (*L2)->next;
*L3 = (linklist)malloc(sizeof(node));
(*L3)->next = NULL;
p3 = *L3;
while (p1 != NULL && p2 != NULL) {
if (p1->expn < p2->expn) {
p3->next = p1;
p3 = p1;
p1 = p1->next;
} else if (p1->expn > p2->expn) {
p3->next = (linklist)malloc(sizeof(node));
p3->next->coef = -1 * p2->coef;
p3->next->expn = p2->expn;
p3->next->next = NULL;
p3 = p3->next;
p2 = p2->next;
} else {
int diff = p1->coef - p2->coef;
if (diff != 0) {
linklist tmp = (linklist)malloc(sizeof(node));
tmp->coef = diff;
tmp->expn = p1->expn;
tmp->next = NULL;
p3->next = tmp;
p3 = tmp;
}
p1 = p1->next;
p2 = p2->next;
}
}
while (p1 != NULL) {
p3->next = p1;
p3 = p1;
p1 = p1->next;
}
while (p2 != NULL) {
p3->next = (linklist)malloc(sizeof(node));
p3->next->coef = -1 * p2->coef;
p3->next->expn = p2->expn;
p3->next->next = NULL;
p3 = p3->next;
p2 = p2->next;
}
}
// 多项式相乘
void mul_polyn(linklist *L1, linklist *L2, linklist *L3) {
linklist p1, p2, p3;
p1 = (*L1)->next;
p2 = (*L2)->next;
*L3 = (linklist)malloc(sizeof(node));
(*L3)->next = NULL;
p3 = *L3;
while (p1 != NULL) {
while (p2 != NULL) {
int coef = p1->coef * p2->coef;
int expn = p1->expn + p2->expn;
linklist tmp = (linklist)malloc(sizeof(node));
tmp->coef = coef;
tmp->expn = expn;
tmp->next = NULL;
p3->next = tmp;
p3 = tmp;
p2 = p2->next;
}
p1 = p1->next;
p2 = (*L2)->next;
}
sort_polyn(*L3);
linklist pre, cur;
pre = (*L3);
cur = (*L3)->next;
while (cur != NULL) {
if (cur->expn == pre->expn) {
pre->coef += cur->coef;
pre->next = cur->next;
free(cur);
cur = pre->next;
} else {
pre = cur;
cur = cur->next;
}
}
}
int main() {
linklist L1, L2, L3;
int n1, n2;
printf("请输入第一个多项式的项数:");
scanf("%d", &n1);
printf("请依次输入第一个多项式的系数和指数:\n");
create_polyn(&L1, n1);
sort_polyn(L1);
printf("第一个多项式为:");
print_polyn(L1);
printf("请输入第二个多项式的项数:");
scanf("%d", &n2);
printf("请依次输入第二个多项式的系数和指数:\n");
create_polyn(&L2, n2);
sort_polyn(L2);
printf("第二个多项式为:");
print_polyn(L2);
printf("多项式相加结果为:");
add_polyn(&L1, &L2, &L3);
print_polyn(L3);
printf("多项式相减结果为:");
sub_polyn(&L1, &L2, &L3);
print_polyn(L3);
printf("多项式相乘结果为:");
mul_polyn(&L1, &L2, &L3);
print_polyn(L3);
return 0;
}
```
以上代码实现了一个基于链表的一元多项式计算器,可以实现输入多项式的系数和指数,排序使其按指数升序排列,实现两个多项式的相加相减相乘,并输出相加相减相乘结果。
阅读全文