在C语言中,表示一元多项式并进行相加操作可以通过多种数据结构实现。常见的方法是使用链表,其中每个节点表示多项式中的一项。节点包含系数、指数和指向下一个节点的指针。
下面是一个简单的实现示例:
定义多项式节点结构体
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
|
|
typedef struct PolyNode { |
|
int coef; // 系数 |
|
int exp; // 指数 |
|
struct PolyNode *next; // 指向下一个节点的指针 |
|
} PolyNode, *Polynomial; |
创建多项式节点
|
PolyNode* CreateNode(int coef, int exp) { |
|
PolyNode* newNode = (PolyNode*)malloc(sizeof(PolyNode)); |
|
if (!newNode) { |
|
printf("Memory allocation failed.\n"); |
|
exit(1); |
|
} |
|
newNode->coef = coef; |
|
newNode->exp = exp; |
|
newNode->next = NULL; |
|
return newNode; |
|
} |
插入多项式项
|
void InsertTerm(Polynomial *poly, int coef, int exp) { |
|
PolyNode *newNode = CreateNode(coef, exp); |
|
PolyNode *current = *poly; |
|
PolyNode *prev = NULL; |
|
|
|
// 找到插入位置 |
|
while (current != NULL && current->exp > exp) { |
|
prev = current; |
|
current = current->next; |
|
} |
|
|
|
// 插入新节点 |
|
if (prev == NULL) { |
|
newNode->next = *poly; |
|
*poly = newNode; |
|
} else { |
|
prev->next = newNode; |
|
newNode->next = current; |
|
} |
|
} |
多项式相加
|
Polynomial PolyAdd(Polynomial poly1, Polynomial poly2) { |
|
Polynomial result = NULL; |
|
PolyNode *current1 = poly1; |
|
PolyNode *current2 = poly2; |
|
PolyNode *prev = NULL; |
|
PolyNode *newNode; |
|
|
|
while (current1 != NULL && current2 != NULL) { |
|
if (current1->exp > current2->exp) { |
|
newNode = CreateNode(current1->coef, current1->exp); |
|
if (prev == NULL) { |
|
newNode->next = result; |
|
result = newNode; |
|
} else { |
|
prev->next = newNode; |
|
newNode->next = prev->next; |
|
} |
|
prev = newNode; |
|
current1 = current1->next; |
|
} else if (current1->exp < current2->exp) { |
|
newNode = CreateNode(current2->coef, current2->exp); |
|
if (prev == NULL) { |
|
newNode->next = result; |
|
result = newNode; |
|
} else { |
|
prev->next = newNode; |
|
newNode->next = prev->next; |
|
} |
|
prev = newNode; |
|
current2 = current2->next; |
|
} else { // 指数相同,合并系数 |
|
int sumCoef = current1->coef + current2->coef; |
|
if (sumCoef != 0) { // 如果系数和不为0,则创建新节点 |
|
newNode = CreateNode(sumCoef, current1->exp); |
|
if (prev == NULL) { |
|
newNode->next = result; |
|
result = newNode; |
|
} else { |
|
prev->next = newNode; |
|
newNode->next = prev->next; |
|
} |
|
prev = newNode; |
|
} |
|
current1 = current1->next; |
|
current2 = current2->next; |
|
} |
|
} |
|
|
|
// 处理剩余节点 |
|
while (current1 != NULL) { |
|
newNode = CreateNode(current1->coef, current1->exp); |
|
prev->next = newNode; |
|
prev = newNode; |
|
current1 = current1->next; |
|
} |
|
|
|
while (current2 != NULL) { |
|
newNode = CreateNode(current2->coef, current2->exp); |
|
prev->next = newNode; |
|
prev = newNode; |
|
current2 = current2->next; |
|
} |
|
|
|
return result; |
|
} |
打印多项式
|
void PrintPoly(Polynomial poly) { |
|
PolyNode *current = poly; |
|
int isFirst = 1; |
|
while (current != NULL) { |
|
if (!isFirst && current->coef > 0) { |
|
printf("+"); |
|
} |
|
printf("%dx^%d", current->coef |
一元多项式在C语言中通常可以用链表来表示,其中链表的每个节点代表多项式的一个项。每个节点包含系数(coefficient)、指数(exponent)以及指向下一个节点的指针。多项式相加时,我们需要遍历两个多项式链表,对应指数的项相加或合并,处理完所有项后得到相加后的多项式。
下面是一个简单的C语言实现示例:
多项式节点的定义2
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
|
|
// 多项式项的定义 |
|
typedef struct PolyNode { |
|
int coef; // 系数 |
|
int exp; // 指数 |
|
struct PolyNode *next; // 指向下一个节点的指针 |
|
} PolyNode, *Polynomial; |
创建多项式节点2
|
// 创建多项式节点 |
|
PolyNode* CreateNode(int coef, int exp) { |
|
PolyNode* newNode = (PolyNode*)malloc(sizeof(PolyNode)); |
|
if (!newNode) { |
|
exit(1); // 内存分配失败 |
|
} |
|
newNode->coef = coef; |
|
newNode->exp = exp; |
|
newNode->next = NULL; |
|
return newNode; |
|
} |
多项式相加2
|
// 多项式相加 |
|
Polynomial PolyAdd(Polynomial poly1, Polynomial poly2) { |
|
Polynomial result = NULL, tail = NULL; |
|
PolyNode *p1 = poly1->next, *p2 = poly2->next; |
|
int sum; |
|
|
|
// 遍历两个多项式链表 |
|
while (p1 && p2) { |
|
if (p1->exp == p2->exp) { // 指数相同,系数相加 |
|
sum = p1->coef + p2->coef; |
|
if (sum) { // 如果系数和不为0,则创建新节点 |
|
if (!result) { |
|
result = tail = CreateNode(sum, p1->exp); |
|
} else { |
|
tail->next = CreateNode(sum, p1->exp); |
|
tail = tail->next; |
|
} |
|
} |
|
p1 = p1->next; |
|
p2 = p2->next; |
|
} else if (p1->exp > p2->exp) { // p1的指数大于p2的指数,将p1的节点加入结果链表 |
|
if (!result) { |
|
result = tail = CreateNode(p1->coef, p1->exp); |
|
} else { |
|
tail->next = CreateNode(p1->coef, p1->exp); |
|
tail = tail->next; |
|
} |
|
p1 = p1->next; |
|
} else { // p1的指数小于p2的指数,将p2的节点加入结果链表 |
|
if (!result) { |
|
result = tail = CreateNode(p2->coef, p2->exp); |
|
} else { |
|
tail->next = CreateNode(p2->coef, p2->exp); |
|
tail = tail->next; |
|
} |
|
p2 = p2->next; |
|
} |
|
} |
|
|
|
// 将剩余节点(如果有)加入结果链表 |
|
while (p1) { |
|
if (!result) { |
|
result = tail = CreateNode(p1->coef, p1->exp); |
|
} else { |
|
tail->next = CreateNode(p1->coef, p1->exp); |
|
tail = tail->next; |
|
} |
|
p1 = p1->next; |
|
} |
|
|
|
while (p2) { |
|
if (!result) { |
|
result = tail = CreateNode(p2->coef, p2->exp); |
|
} else { |
|
tail->next = CreateNode(p2->coef, p2->exp); |
|
tail = tail->next; |
|
} |
|
p2 = p2->next; |
|
} |
|
|
|
return result; |
|
} |
打印多项式2
|
// 打印多项式 |
|
|
void PrintPoly(Polynomial poly) { |
|
|
PolyNode *p = poly->next; |
|
|
int isFirst = 1; // 标记是否为第一个项,以决定是否打印加号 |
|
|
|
|
|
while (p) { |
|
|
if (!isFirst && p->coef > 0) { |
|
|
printf("+"); |
|
|
} |
|
|
printf("%dx^%d", p->coef, p->exp); |
|
|
isFirst = 0; |
|
|
p = p->next; |
|
|
} |
|
|
printf("\n"); |
|
|
} |
|
|
|