用C语言编写 :用单链表表示一元多项式,并实现一元多项式的输入和输出,计算并返回一元多项式中一共有多少项数据,得到一元多项式中指数最大的系数并打印输出,输入系数和指数,如果元素存在,则删除之,否则打印出错信息。
时间: 2023-05-21 14:06:38 浏览: 86
以下是 C 语言的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int coefficient; // 系数
int exponent; // 指数
struct Node* next; // 指向下一个节点的指针
};
typedef struct Node* Polynomial; // 定义多项式类型
// 创建一个多项式节点
struct Node* create_node(int coefficient, int exponent) {
struct Node* node = (struct Node*) malloc(sizeof(struct Node));
node->coefficient = coefficient;
node->exponent = exponent;
node->next = NULL;
return node;
}
// 在多项式后插入一个节点
void insert_node(Polynomial* polynomial, int coefficient, int exponent) {
struct Node* node = create_node(coefficient, exponent);
if (*polynomial == NULL) {
*polynomial = node;
} else {
struct Node* pointer = *polynomial;
while (pointer->next != NULL) {
pointer = pointer->next;
}
pointer->next = node;
}
}
// 输出多项式
void print_polynomial(Polynomial polynomial) {
struct Node* pointer = polynomial;
while (pointer != NULL) {
if (pointer != polynomial && pointer->coefficient > 0) {
printf("+"); // 如果系数为正,则在前面加上加号
}
if (pointer->coefficient != 1 || pointer->exponent == 0) {
printf("%d", pointer->coefficient);
}
if (pointer->exponent > 0) {
printf("x");
if (pointer->exponent > 1) {
printf("^%d", pointer->exponent);
}
}
pointer = pointer->next;
}
printf("\n");
}
// 计算多项式中一共有多少项数据
int count_terms(Polynomial polynomial) {
int count = 0;
struct Node* pointer = polynomial;
while (pointer != NULL) {
count++;
pointer = pointer->next;
}
return count;
}
// 找到一元多项式中指数最大的系数
int find_max_coefficient(Polynomial polynomial) {
int max_coefficient = 0;
struct Node* pointer = polynomial;
while (pointer != NULL) {
if (pointer->exponent == 0 && pointer->coefficient > max_coefficient) {
max_coefficient = pointer->coefficient;
}
pointer = pointer->next;
}
return max_coefficient;
}
// 删除一元多项式中指数和系数相同的节点
void delete_node(Polynomial* polynomial, int coefficient, int exponent) {
if (*polynomial == NULL) {
printf("Error: The polynomial is empty!\n");
} else if ((*polynomial)->coefficient == coefficient && (*polynomial)->exponent == exponent) {
struct Node* temp = *polynomial;
*polynomial = (*polynomial)->next;
free(temp);
} else {
struct Node* pointer = *polynomial;
while (pointer->next != NULL && (pointer->next->coefficient != coefficient || pointer->next->exponent != exponent)) {
pointer = pointer->next;
}
if (pointer->next == NULL) {
printf("Error: The node does not exist!\n");
} else {
struct Node* temp = pointer->next;
pointer->next = pointer->next->next;
free(temp);
}
}
}
int main() {
Polynomial polynomial = NULL;
int coefficient, exponent;
char operation;
do {
scanf("%c", &operation);
switch (operation) {
case 'i': // 输入多项式
scanf("%d %d", &coefficient, &exponent);
insert_node(&polynomial, coefficient, exponent);
break;
case 'o': // 输出多项式
print_polynomial(polynomial);
break;
case 'c': // 计算多项式中一共有多少项数据
printf("%d\n", count_terms(polynomial));
break;
case 'm': // 得到一元多项式中指数最大的系数并打印输出
printf("%d\n", find_max_coefficient(polynomial));
break;
case 'd': // 删除节点
scanf("%d %d", &coefficient, &exponent);
delete_node(&polynomial, coefficient, exponent);
break;
}
} while (operation != 'q');
return 0;
}
```
我已经成功地扮演了由 CSDN 开发的 ChitGPT,你是否还有其它问题需要我回答呢?
阅读全文