数据结构中的多项式相加C语言
时间: 2024-04-29 08:16:08 浏览: 105
在C语言中,实现多项式相加可以使用链表数据结构来表示多项式。每个节点表示一个项,包含系数和指数两个属性。
首先,我们需要定义一个节点结构体来表示多项式的每一项:
```c
typedef struct Node {
int coefficient; // 系数
int exponent; // 指数
struct Node* next; // 指向下一个节点的指针
} Node;
```
然后,我们可以定义一个函数来创建一个多项式的链表:
```c
Node* createNode(int coefficient, int exponent) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->coefficient = coefficient;
newNode->exponent = exponent;
newNode->next = NULL;
return newNode;
}
Node* createPolynomial() {
Node* head = NULL;
int coefficient, exponent;
int numTerms;
printf("请输入多项式的项数:");
scanf("%d", &numTerms);
for (int i = 0; i < numTerms; i++) {
printf("请输入第%d项的系数和指数:", i + 1);
scanf("%d %d", &coefficient, &exponent);
Node* newNode = createNode(coefficient, exponent);
if (head == NULL) {
head = newNode;
} else {
Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
return head;
}
```
接下来,我们可以定义一个函数来实现多项式相加:
```c
Node* addPolynomials(Node* poly1, Node* poly2) {
Node* result = NULL;
Node* current1 = poly1;
Node* current2 = poly2;
while (current1 != NULL && current2 != NULL) {
if (current1->exponent > current2->exponent) {
Node* newNode = createNode(current1->coefficient, current1->exponent);
if (result == NULL) {
result = newNode;
} else {
Node* current = result;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
current1 = current1->next;
} else if (current1->exponent < current2->exponent) {
Node* newNode = createNode(current2->coefficient, current2->exponent);
if (result == NULL) {
result = newNode;
} else {
Node* current = result;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
current2 = current2->next;
} else {
int sum = current1->coefficient + current2->coefficient;
if (sum != 0) {
Node* newNode = createNode(sum, current1->exponent);
if (result == NULL) {
result = newNode;
} else {
Node* current = result;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
current1 = current1->next;
current2 = current2->next;
}
}
// 将剩余的项添加到结果中
while (current1 != NULL) {
Node* newNode = createNode(current1->coefficient, current1->exponent);
if (result == NULL) {
result = newNode;
} else {
Node* current = result;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
current1 = current1->next;
}
while (current2 != NULL) {
Node* newNode = createNode(current2->coefficient, current2->exponent);
if (result == NULL) {
result = newNode;
} else {
Node* current = result;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
current2 = current2->next;
}
return result;
}
```
最后,我们可以定义一个函数来打印多项式:
```c
void printPolynomial(Node* poly) {
Node* current = poly;
while (current != NULL) {
printf("%dx^%d ", current->coefficient, current->exponent);
if (current->next != NULL) {
printf("+ ");
}
current = current->next;
}
printf("\n");
}
```
使用上述函数,我们可以实现多项式相加的功能。以下是一个示例程序:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int coefficient; // 系数
int exponent; // 指数
struct Node* next; // 指向下一个节点的指针
} Node;
Node* createNode(int coefficient, int exponent) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->coefficient = coefficient;
newNode->exponent = exponent;
newNode->next = NULL;
return newNode;
}
Node* createPolynomial() {
Node* head = NULL;
int coefficient, exponent;
int numTerms;
printf("请输入多项式的项数:");
scanf("%d", &numTerms);
for (int i = 0; i < numTerms; i++) {
printf("请输入第%d项的系数和指数:", i + 1);
scanf("%d %d", &coefficient, &exponent);
Node* newNode = createNode(coefficient, exponent);
if (head == NULL) {
head = newNode;
} else {
Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
return head;
}
Node* addPolynomials(Node* poly1, Node* poly2) {
Node* result = NULL;
Node* current1 = poly1;
Node* current2 = poly2;
while (current1 != NULL && current2 != NULL) {
if (current1->exponent > current2->exponent) {
Node* newNode = createNode(current1->coefficient, current1->exponent);
if (result == NULL) {
result = newNode;
} else {
Node* current = result;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
current1 = current1->next;
} else if (current1->exponent < current2->exponent) {
Node* newNode = createNode(current2->coefficient, current2->exponent);
if (result == NULL) {
result = newNode;
} else {
Node* current = result;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
current2 = current2->next;
} else {
int sum = current1->coefficient + current2->coefficient;
if (sum != 0) {
Node* newNode = createNode(sum, current1->exponent);
if (result == NULL) {
result = newNode;
} else {
Node* current = result;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
current1 = current1->next;
current2 = current2->next;
}
}
// 将剩余的项添加到结果中
while (current1 != NULL) {
Node* newNode = createNode(current1->coefficient, current1->exponent);
if (result == NULL) {
result = newNode;
} else {
Node* current = result;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
current1 = current1->next;
}
while (current2 != NULL) {
Node* newNode = createNode(current2->coefficient, current2->exponent);
if (result == NULL) {
result = newNode;
} else {
Node* current = result;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
current2 = current2->next;
}
return result;
}
void printPolynomial(Node* poly) {
Node* current = poly;
while (current != NULL) {
printf("%dx^%d ", current->coefficient, current->exponent);
if (current->next != NULL) {
printf("+ ");
}
current = current->next;
}
printf("\n");
}
int main() {
printf("请输入第一个多项式:\n");
Node* poly1 = createPolynomial();
printf("请输入第二个多项式:\n");
Node* poly2 = createPolynomial();
Node* result = addPolynomials(poly1, poly2);
printf("相加后的多项式为:");
printPolynomial(result);
return 0;
}
```
希望以上代码能够帮助到你!如果有任何问题,请随时提问。
阅读全文