C语言编写数据结构的一元多项式计算器,输入多项式,序列按指数升序排列,实现两个多项式的相加相减相乘,输出结果
时间: 2023-07-11 19:27:37 浏览: 149
一个一元多项式简单的计算器
5星 · 资源好评率100%
好的,以下是一个用C语言编写的数据结构一元多项式计算器,实现了多项式的输入、排序以及加减乘运算:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义多项式结构体
typedef struct term {
int coeff; // 系数
int exp; // 指数
struct term *next; // 指向下一个节点的指针
} Polynomial;
// 创建新节点
Polynomial *createNode(int coeff, int exp) {
Polynomial *newNode = (Polynomial *)malloc(sizeof(Polynomial));
newNode->coeff = coeff;
newNode->exp = exp;
newNode->next = NULL;
return newNode;
}
// 插入节点
void insertNode(Polynomial **head, int coeff, int exp) {
Polynomial *newNode = createNode(coeff, exp);
if (*head == NULL) {
*head = newNode;
} else {
Polynomial *current = *head;
Polynomial *prev = NULL;
while (current != NULL && current->exp < exp) {
prev = current;
current = current->next;
}
if (current != NULL && current->exp == exp) {
current->coeff += coeff;
free(newNode);
} else {
newNode->next = current;
if (prev == NULL) {
*head = newNode;
} else {
prev->next = newNode;
}
}
}
}
// 输入多项式
Polynomial *inputPolynomial() {
Polynomial *head = NULL;
printf("请输入多项式的项数:");
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int coeff, exp;
printf("请输入第%d项的系数和指数:", i + 1);
scanf("%d %d", &coeff, &exp);
insertNode(&head, coeff, exp);
}
return head;
}
// 输出多项式
void outputPolynomial(Polynomial *head) {
Polynomial *current = head;
while (current != NULL) {
printf("%dx^%d ", current->coeff, current->exp);
if (current->next != NULL) {
printf("+ ");
}
current = current->next;
}
printf("\n");
}
// 相加多项式
Polynomial *addPolynomial(Polynomial *p1, Polynomial *p2) {
Polynomial *result = NULL;
while (p1 != NULL && p2 != NULL) {
if (p1->exp < p2->exp) {
insertNode(&result, p1->coeff, p1->exp);
p1 = p1->next;
} else if (p1->exp > p2->exp) {
insertNode(&result, p2->coeff, p2->exp);
p2 = p2->next;
} else {
int coeff = p1->coeff + p2->coeff;
insertNode(&result, coeff, p1->exp);
p1 = p1->next;
p2 = p2->next;
}
}
while (p1 != NULL) {
insertNode(&result, p1->coeff, p1->exp);
p1 = p1->next;
}
while (p2 != NULL) {
insertNode(&result, p2->coeff, p2->exp);
p2 = p2->next;
}
return result;
}
// 相减多项式
Polynomial *subtractPolynomial(Polynomial *p1, Polynomial *p2) {
Polynomial *result = NULL;
while (p1 != NULL && p2 != NULL) {
if (p1->exp < p2->exp) {
insertNode(&result, p1->coeff, p1->exp);
p1 = p1->next;
} else if (p1->exp > p2->exp) {
insertNode(&result, -p2->coeff, p2->exp);
p2 = p2->next;
} else {
int coeff = p1->coeff - p2->coeff;
insertNode(&result, coeff, p1->exp);
p1 = p1->next;
p2 = p2->next;
}
}
while (p1 != NULL) {
insertNode(&result, p1->coeff, p1->exp);
p1 = p1->next;
}
while (p2 != NULL) {
insertNode(&result, -p2->coeff, p2->exp);
p2 = p2->next;
}
return result;
}
// 相乘多项式
Polynomial *multiplyPolynomial(Polynomial *p1, Polynomial *p2) {
Polynomial *result = NULL;
while (p1 != NULL) {
Polynomial *current = p2;
while (current != NULL) {
int coeff = p1->coeff * current->coeff;
int exp = p1->exp + current->exp;
insertNode(&result, coeff, exp);
current = current->next;
}
p1 = p1->next;
}
return result;
}
int main() {
Polynomial *p1 = inputPolynomial();
printf("第一个多项式为:");
outputPolynomial(p1);
Polynomial *p2 = inputPolynomial();
printf("第二个多项式为:");
outputPolynomial(p2);
Polynomial *sum = addPolynomial(p1, p2);
printf("两个多项式的和为:");
outputPolynomial(sum);
Polynomial *difference = subtractPolynomial(p1, p2);
printf("两个多项式的差为:");
outputPolynomial(difference);
Polynomial *product = multiplyPolynomial(p1, p2);
printf("两个多项式的积为:");
outputPolynomial(product);
return 0;
}
```
该程序使用了链表数据结构来存储多项式,包含了输入、输出、排序、相加、相减、相乘等功能。
阅读全文