C语言数据结构编写一元多项式计算器,输入多项式系数与指数,排序使其按指数升序,实现加减乘,并输出结果
时间: 2024-03-06 15:49:07 浏览: 137
好的,下面是C语言实现一元多项式计算器的代码:
```
#include <stdio.h>
#include <stdlib.h>
// 定义多项式结构体
typedef struct Node {
float coef; // 系数
int exp; // 指数
struct Node *next;
} Node, *Polynomial;
// 创建多项式
Polynomial create() {
Polynomial p, q, head;
head = (Polynomial)malloc(sizeof(Node));
head->next = NULL;
q = head;
float c;
int e;
printf("请输入多项式系数和指数(以-1结束):\n");
scanf("%f%d", &c, &e);
while (e != -1) {
p = (Polynomial)malloc(sizeof(Node));
p->coef = c;
p->exp = e;
q->next = p;
q = p;
scanf("%f%d", &c, &e);
}
q->next = NULL;
return head;
}
// 打印多项式
void print(Polynomial p) {
if (p->next == NULL) {
printf("0\n");
return;
}
while (p->next != NULL) {
p = p->next;
if (p->coef > 0 && p != NULL) {
printf("+");
}
if (p->coef == 1) {
if (p->exp == 0) {
printf("1");
} else if (p->exp == 1) {
printf("x");
} else {
printf("x^%d", p->exp);
}
} else if (p->coef == -1) {
if (p->exp == 0) {
printf("-1");
} else if (p->exp == 1) {
printf("-x");
} else {
printf("-x^%d", p->exp);
}
} else {
if (p->exp == 0) {
printf("%g", p->coef);
} else if (p->exp == 1) {
printf("%gx", p->coef);
} else {
printf("%gx^%d", p->coef, p->exp);
}
}
}
printf("\n");
}
// 多项式相加
Polynomial add(Polynomial p1, Polynomial p2) {
Polynomial p, q, head;
head = (Polynomial)malloc(sizeof(Node));
head->next = NULL;
q = head;
while (p1->next != NULL && p2->next != NULL) {
p = (Polynomial)malloc(sizeof(Node));
if (p1->next->exp < p2->next->exp) {
p->coef = p1->next->coef;
p->exp = p1->next->exp;
p1 = p1->next;
} else if (p1->next->exp > p2->next->exp) {
p->coef = p2->next->coef;
p->exp = p2->next->exp;
p2 = p2->next;
} else if (p1->next->exp == p2->next->exp) {
p->coef = p1->next->coef + p2->next->coef;
p->exp = p1->next->exp;
p1 = p1->next;
p2 = p2->next;
}
if (p->coef != 0) {
q->next = p;
q = p;
} else {
free(p);
}
}
while (p1->next != NULL) {
p = (Polynomial)malloc(sizeof(Node));
p->coef = p1->next->coef;
p->exp = p1->next->exp;
q->next = p;
q = p;
p1 = p1->next;
}
while (p2->next != NULL) {
p = (Polynomial)malloc(sizeof(Node));
p->coef = p2->next->coef;
p->exp = p2->next->exp;
q->next = p;
q = p;
p2 = p2->next;
}
q->next = NULL;
return head;
}
// 多项式相减
Polynomial sub(Polynomial p1, Polynomial p2) {
Polynomial p, q, head;
head = (Polynomial)malloc(sizeof(Node));
head->next = NULL;
q = head;
while (p1->next != NULL && p2->next != NULL) {
p = (Polynomial)malloc(sizeof(Node));
if (p1->next->exp < p2->next->exp) {
p->coef = p1->next->coef;
p->exp = p1->next->exp;
p1 = p1->next;
} else if (p1->next->exp > p2->next->exp) {
p->coef = -p2->next->coef;
p->exp = p2->next->exp;
p2 = p2->next;
} else if (p1->next->exp == p2->next->exp) {
p->coef = p1->next->coef - p2->next->coef;
p->exp = p1->next->exp;
p1 = p1->next;
p2 = p2->next;
}
if (p->coef != 0) {
q->next = p;
q = p;
} else {
free(p);
}
}
while (p1->next != NULL) {
p = (Polynomial)malloc(sizeof(Node));
p->coef = p1->next->coef;
p->exp = p1->next->exp;
q->next = p;
q = p;
p1 = p1->next;
}
while (p2->next != NULL) {
p = (Polynomial)malloc(sizeof(Node));
p->coef = -p2->next->coef;
p->exp = p2->next->exp;
q->next = p;
q = p;
p2 = p2->next;
}
q->next = NULL;
return head;
}
// 多项式相乘
Polynomial mul(Polynomial p1, Polynomial p2) {
Polynomial p, q, head, t;
head = (Polynomial)malloc(sizeof(Node));
head->next = NULL;
q = head;
while (p2->next != NULL) {
p = (Polynomial)malloc(sizeof(Node));
p->coef = p1->next->coef * p2->next->coef;
p->exp = p1->next->exp + p2->next->exp;
p2 = p2->next;
t = head;
while (t->next != NULL && t->next->exp > p->exp) {
t = t->next;
}
if (t->next == NULL) {
q->next = p;
q = p;
p->next = NULL;
} else if (t->next->exp < p->exp) {
p->next = t->next;
t->next = p;
} else if (t->next->exp == p->exp) {
t->next->coef += p->coef;
if (t->next->coef == 0) {
t->next = t->next->next;
free(p);
} else {
free(p);
}
}
}
p1 = p1->next;
while (p1 != NULL) {
p2 = p2->next;
head = add(head, mul(p1, p2));
p1 = p1->next;
}
return head;
}
// 主函数
int main() {
printf("请输入第一个多项式:\n");
Polynomial p1 = create();
printf("第一个多项式:");
print(p1);
printf("请输入第二个多项式:\n");
Polynomial p2 = create();
printf("第二个多项式:");
print(p2);
printf("两个多项式相加:");
print(add(p1, p2));
printf("两个多项式相减:");
print(sub(p1, p2));
printf("两个多项式相乘:");
print(mul(p1, p2));
return 0;
}
```
具体实现逻辑见代码注释。
阅读全文