C语言数据结构编写一元多项式计算器,输入多项式系数与指数,排序使其按指数升序,实现加减乘,并输出结果
时间: 2024-03-05 20:50:05 浏览: 118
好的,下面是一元多项式计算器的 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义多项式结构体
typedef struct {
float coef; // 系数
int exp; // 指数
} Term;
// 定义多项式链表结构体
typedef struct _poly_node {
Term term; // 链表节点存储的项
struct _poly_node *next; // 指向下一个节点的指针
} PolyNode, *PolyList;
// 函数声明
void create_poly(PolyList *poly);
void sort_poly(PolyList poly);
void add_poly(PolyList poly1, PolyList poly2, PolyList *result);
void subtract_poly(PolyList poly1, PolyList poly2, PolyList *result);
void multiply_poly(PolyList poly1, PolyList poly2, PolyList *result);
void print_poly(PolyList poly);
// 主函数
int main() {
PolyList poly1 = NULL, poly2 = NULL, result = NULL;
int choice;
// 创建多项式 1
printf("Create polynomial 1...\n");
create_poly(&poly1);
// 创建多项式 2
printf("Create polynomial 2...\n");
create_poly(&poly2);
// 排序多项式
sort_poly(poly1);
sort_poly(poly2);
// 显示多项式
printf("\nPolynomial 1:\n");
print_poly(poly1);
printf("\nPolynomial 2:\n");
print_poly(poly2);
// 执行加减乘操作
do {
printf("\nOperations:\n");
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_poly(poly1, poly2, &result);
printf("\nResult of addition:\n");
print_poly(result);
break;
case 2:
subtract_poly(poly1, poly2, &result);
printf("\nResult of subtraction:\n");
print_poly(result);
break;
case 3:
multiply_poly(poly1, poly2, &result);
printf("\nResult of multiplication:\n");
print_poly(result);
break;
case 4:
printf("\nExiting...\n");
break;
default:
printf("\nInvalid choice!\n");
}
} while (choice != 4);
return 0;
}
// 创建多项式
void create_poly(PolyList *poly) {
int n;
float coef;
int exp;
PolyNode *node, *tail;
printf("Enter number of terms: ");
scanf("%d", &n);
// 创建头节点
*poly = (PolyNode *)malloc(sizeof(PolyNode));
(*poly)->term.coef = 0;
(*poly)->term.exp = -1;
(*poly)->next = NULL;
tail = *poly;
// 输入每一项
for (int i = 0; i < n; i++) {
printf("Enter term %d:\n", i + 1);
printf("Coefficient: ");
scanf("%f", &coef);
printf("Exponent: ");
scanf("%d", &exp);
// 创建节点
node = (PolyNode *)malloc(sizeof(PolyNode));
node->term.coef = coef;
node->term.exp = exp;
node->next = NULL;
// 将节点插入链表中
tail->next = node;
tail = node;
}
}
// 排序多项式
void sort_poly(PolyList poly) {
PolyNode *p, *q;
Term temp;
// 冒泡排序
for (p = poly->next; p != NULL; p = p->next) {
for (q = p->next; q != NULL; q = q->next) {
if (p->term.exp > q->term.exp) {
temp = p->term;
p->term = q->term;
q->term = temp;
}
}
}
}
// 加法运算
void add_poly(PolyList poly1, PolyList poly2, PolyList *result) {
PolyNode *p1, *p2, *node, *tail;
// 创建头节点
*result = (PolyNode *)malloc(sizeof(PolyNode));
(*result)->term.coef = 0;
(*result)->term.exp = -1;
(*result)->next = NULL;
tail = *result;
// 遍历两个多项式的链表
p1 = poly1->next;
p2 = poly2->next;
while (p1 != NULL && p2 != NULL) {
if (p1->term.exp < p2->term.exp) {
node = (PolyNode *)malloc(sizeof(PolyNode));
node->term = p1->term;
node->next = NULL;
tail->next = node;
tail = node;
p1 = p1->next;
} else if (p1->term.exp > p2->term.exp) {
node = (PolyNode *)malloc(sizeof(PolyNode));
node->term = p2->term;
node->next = NULL;
tail->next = node;
tail = node;
p2 = p2->next;
} else {
node = (PolyNode *)malloc(sizeof(PolyNode));
node->term.coef = p1->term.coef + p2->term.coef;
node->term.exp = p1->term.exp;
node->next = NULL;
tail->next = node;
tail = node;
p1 = p1->next;
p2 = p2->next;
}
}
// 将剩余的项插入结果链表中
while (p1 != NULL) {
node = (PolyNode *)malloc(sizeof(PolyNode));
node->term = p1->term;
node->next = NULL;
tail->next = node;
tail = node;
p1 = p1->next;
}
while (p2 != NULL) {
node = (PolyNode *)malloc(sizeof(PolyNode));
node->term = p2->term;
node->next = NULL;
tail->next = node;
tail = node;
p2 = p2->next;
}
}
// 减法运算
void subtract_poly(PolyList poly1, PolyList poly2, PolyList *result) {
PolyNode *p1, *p2, *node, *tail;
// 创建头节点
*result = (PolyNode *)malloc(sizeof(PolyNode));
(*result)->term.coef = 0;
(*result)->term.exp = -1;
(*result)->next = NULL;
tail = *result;
// 遍历两个多项式的链表
p1 = poly1->next;
p2 = poly2->next;
while (p1 != NULL && p2 != NULL) {
if (p1->term.exp < p2->term.exp) {
node = (PolyNode *)malloc(sizeof(PolyNode));
node->term = p1->term;
node->next = NULL;
tail->next = node;
tail = node;
p1 = p1->next;
} else if (p1->term.exp > p2->term.exp) {
node = (PolyNode *)malloc(sizeof(PolyNode));
node->term.coef = -p2->term.coef;
node->term.exp = p2->term.exp;
node->next = NULL;
tail->next = node;
tail = node;
p2 = p2->next;
} else {
node = (PolyNode *)malloc(sizeof(PolyNode));
node->term.coef = p1->term.coef - p2->term.coef;
node->term.exp = p1->term.exp;
node->next = NULL;
tail->next = node;
tail = node;
p1 = p1->next;
p2 = p2->next;
}
}
// 将剩余的项插入结果链表中
while (p1 != NULL) {
node = (PolyNode *)malloc(sizeof(PolyNode));
node->term = p1->term;
node->next = NULL;
tail->next = node;
tail = node;
p1 = p1->next;
}
while (p2 != NULL) {
node = (PolyNode *)malloc(sizeof(PolyNode));
node->term.coef = -p2->term.coef;
node->term.exp = p2->term.exp;
node->next = NULL;
tail->next = node;
tail = node;
p2 = p2->next;
}
}
// 乘法运算
void multiply_poly(PolyList poly1, PolyList poly2, PolyList *result) {
PolyNode *p1, *p2, *node, *tail, *temp1, *temp2;
Term temp;
// 创建头节点
*result = (PolyNode *)malloc(sizeof(PolyNode));
(*result)->term.coef = 0;
(*result)->term.exp = -1;
(*result)->next = NULL;
tail = *result;
// 遍历多项式 1 的每一项
for (p1 = poly1->next; p1 != NULL; p1 = p1->next) {
// 遍历多项式 2 的每一项
for (p2 = poly2->next; p2 != NULL; p2 = p2->next) {
// 计算乘积
node = (PolyNode *)malloc(sizeof(PolyNode));
node->term.coef = p1->term.coef * p2->term.coef;
node->term.exp = p1->term.exp + p2->term.exp;
node->next = NULL;
// 将乘积插入结果链表中
tail->next = node;
tail = node;
}
}
// 合并同类项
sort_poly(*result);
tail = *result;
for (p1 = tail->next; p1 != NULL; p1 = p1->next) {
for (p2 = p1->next; p2 != NULL; p2 = p2->next) {
if (p1->term.exp == p2->term.exp) {
p1->term.coef += p2->term.coef;
temp1 = tail;
for (temp2 = p2; temp2->next != NULL; temp2 = temp2->next) {
temp1 = temp1->next;
}
temp1->next = temp2->next;
free(temp2);
}
}
}
}
// 输出多项式
void print_poly(PolyList poly) {
PolyNode *p;
// 遍历链表中的每一项
for (p = poly->next; p != NULL; p = p->next) {
if (p->term.coef > 0 && p != poly->next) {
printf("+");
}
printf("%.2fx^%d", p->term.coef, p->term.exp);
}
printf("\n");
}
```
在这个代码中,我们使用了多项式链表来存储多项式。create\_poly 函数用于创建多项式,sort\_poly 函数用于将多项式按指数升序排序,add\_poly 函数用于实现加法运算,subtract\_poly 函数用于实现减法运算,multiply\_poly 函数用于实现乘法运算,print\_poly 函数用于输出多项式。主函数中,我们先创建两个多项式,然后排序并输出它们。接下来,我们提供了一个菜单,让用户选择加减乘操作,根据用户的选择执行相应的操作并输出结果。
阅读全文