typedef struct PolyNode { int Coef; // 多项式系数 ... int Exp; // 多项式指数 ... struct PolyNode* next; // 链接指针 ... }PolyNode, * PolyList; PolyList PolyA_HeadPtr; // 多项式 A( x ) 的链表头指针 ... PolyList PolyB_HeadPtr; // 多项式 B( x ) 的链表头指针 ... PolyList CreateEmptyPoly(PolyList Head_Ptr); void ClearPoly(PolyList HeadPtr); void DestroyPoly(PolyList HeadPtr); void PrintPoly(PolyList HeadPtr); void CreatePolyByKeyboard(PolyList HeadPtr); void AddPoly(PolyList PolyA_Head, PolyList PolyB_Head) { PolyNode* p, * q, * r, * t; int sum; p = PolyA_Head->next; q = PolyB_Head->next; r = PolyA_Head; // 'r' 指针总是指向 'p' 的前一个节点 ... t = NULL; // 对 'B' 多项式链表中节点的处理, 要么释放掉, 要么加入 'A' 多项式链表, 所以直接将其置空 ... PolyB_Head->next = NULL; while ((p != NULL) && (q != NULL)) { if ((p->Exp) < (q->Exp)) { } else if (p->Exp == q->Exp) { } else // 'p -> Exp > q -> Exp' ... { } } // end 'while ( ( p != NULL ) && ( q != NULL ) )' ... // 若 'B' 链表为空, 将剩下的节点链接起来 ... if (p != NULL) r->next = p; // 若 'A' 链表为空, 将剩下的节点链接起来 ... if (q != NULL) r->next = q; }多项式 'A' 和 'B' 相加( 多项式链表中指数必须由小到大排列 ) 完整代码是什么
时间: 2024-03-26 22:38:05 浏览: 55
MINIX_SOURCE_CODE.pdf
这是一段多项式加法的代码,完整代码如下:
```c++
#include <stdio.h>
#include <stdlib.h>
typedef struct PolyNode {
int Coef; // 多项式系数
int Exp; // 多项式指数
struct PolyNode* next; // 链接指针
}PolyNode, * PolyList;
PolyList CreateEmptyPoly(PolyList Head_Ptr);
void ClearPoly(PolyList HeadPtr);
void DestroyPoly(PolyList HeadPtr);
void PrintPoly(PolyList HeadPtr);
void CreatePolyByKeyboard(PolyList HeadPtr);
void AddPoly(PolyList PolyA_Head, PolyList PolyB_Head);
int main()
{
PolyList PolyA_HeadPtr, PolyB_HeadPtr;
PolyA_HeadPtr = CreateEmptyPoly(PolyA_HeadPtr);
PolyB_HeadPtr = CreateEmptyPoly(PolyB_HeadPtr);
printf("请输入多项式 A 的系数和指数:\n");
CreatePolyByKeyboard(PolyA_HeadPtr);
printf("多项式 A = ");
PrintPoly(PolyA_HeadPtr);
printf("请输入多项式 B 的系数和指数:\n");
CreatePolyByKeyboard(PolyB_HeadPtr);
printf("多项式 B = ");
PrintPoly(PolyB_HeadPtr);
AddPoly(PolyA_HeadPtr, PolyB_HeadPtr);
printf("多项式 A + B = ");
PrintPoly(PolyA_HeadPtr);
DestroyPoly(PolyA_HeadPtr);
DestroyPoly(PolyB_HeadPtr);
return 0;
}
PolyList CreateEmptyPoly(PolyList Head_Ptr)
{
PolyNode* p = (PolyNode*)malloc(sizeof(PolyNode));
p->next = NULL;
Head_Ptr = p;
return Head_Ptr;
}
void ClearPoly(PolyList HeadPtr)
{
PolyNode* p, * q;
p = HeadPtr->next;
while (p != NULL) {
q = p->next;
free(p);
p = q;
}
HeadPtr->next = NULL;
}
void DestroyPoly(PolyList HeadPtr)
{
ClearPoly(HeadPtr);
free(HeadPtr);
}
void PrintPoly(PolyList HeadPtr)
{
PolyNode* p = HeadPtr->next;
int flag = 0;
if (p == NULL) {
printf("0\n");
return;
}
while (p != NULL) {
if (flag == 0) {
printf("%d*x^%d", p->Coef, p->Exp);
flag = 1;
}
else {
if (p->Coef > 0)
printf(" + %d*x^%d", p->Coef, p->Exp);
else if (p->Coef < 0)
printf(" - %d*x^%d", -p->Coef, p->Exp);
}
p = p->next;
}
printf("\n");
}
void CreatePolyByKeyboard(PolyList HeadPtr)
{
int coef, exp;
PolyNode* p = HeadPtr;
scanf_s("%d %d", &coef, &exp);
while (exp != -1) {
PolyNode* q = (PolyNode*)malloc(sizeof(PolyNode));
q->Coef = coef;
q->Exp = exp;
q->next = NULL;
p->next = q;
p = q;
scanf_s("%d %d", &coef, &exp);
}
}
void AddPoly(PolyList PolyA_Head, PolyList PolyB_Head)
{
PolyNode* p, * q, * r, * t;
int sum;
p = PolyA_Head->next;
q = PolyB_Head->next;
r = PolyA_Head; // 'r' 指针总是指向 'p' 的前一个节点 ...
t = NULL; // 对 'B' 多项式链表中节点的处理, 要么释放掉, 要么加入 'A' 多项式链表, 所以直接将其置空 ...
PolyB_Head->next = NULL;
while ((p != NULL) && (q != NULL)) {
if ((p->Exp) < (q->Exp)) {
r = p;
p = p->next;
}
else if (p->Exp == q->Exp) {
sum = p->Coef + q->Coef;
if (sum != 0) {
p->Coef = sum;
r = p;
p = p->next;
t = q;
q = q->next;
free(t);
}
else {
t = p;
p = p->next;
free(t);
t = q;
q = q->next;
free(t);
}
}
else // 'p -> Exp > q -> Exp' ...
{
t = q->next;
q->next = p;
r->next = q;
r = q;
q = t;
}
} // end 'while ( ( p != NULL ) && ( q != NULL ) )' ...
// 若 'B' 链表为空, 将剩下的节点链接起来 ...
if (p != NULL)
r->next = p;
// 若 'A' 链表为空, 将剩下的节点链接起来 ...
if (q != NULL)
r->next = q;
}
```
这段代码实现了多项式的加法,其中 `PolyNode` 为结构体类型,存储了多项式的系数和指数,`PolyList` 为指向 `PolyNode` 的指针类型,表示多项式链表的头指针。`CreateEmptyPoly` 函数用于创建一个空的多项式链表,`ClearPoly` 函数用于清空多项式链表中的所有节点,`DestroyPoly` 函数用于销毁整个多项式链表,`PrintPoly` 函数用于输出多项式,`CreatePolyByKeyboard` 函数用于通过键盘输入创建多项式,`AddPoly` 函数实现了多项式的加法。
阅读全文