typedef struct Polynode { double coef; int exp; struct Polynode *next; }Polynode,* Polylist;
时间: 2024-03-22 10:40:39 浏览: 82
这段代码定义了一个名为Polynode的结构体,里面包含了三个成员变量:一个双精度浮点数coef、一个整型exp和一个指向Polynode结构体的指针next。同时,使用typedef将Polynode重命名为Polylist,方便后续代码中的使用。整段代码的作用是定义一个多项式链表,每个节点代表多项式中的一项。
相关问题
#include <stdio.h> #include <malloc.h> #include <math.h> typedef struct Polynode { double coef; int exp; struct Polynode *next; }Polynode,* Polylist; Polylist H=NULL;/*一元多项式头指针的全局变量*/ /*单链表模块*/ void InitPolylist(Polylist *L); Polylist CreatePolylist(); /*输入多项式的系数和指数,用尾插法建立一元多项式的链表*/ int LengthPolylist(Polylist L); void OutputPolylist(Polylist L); Polylist AddPolylist();/*创建两个多项式并相加,完成后显示序列*/ void ComputePolylist(Polylist L);/*计算多项式在x=%d的值*/ void DestroyPolylist(Polylist L); /*注解1:形式参数Polylist L是链表的头指针*/ /*主函数模块*/ int main() { H=AddPolylist(); ComputePolylist(H); OutputPolylist(H); DestroyPolylist(H); return 0; } /* 请在这里填写答案 */ /*单链表模块*/ void InitPolylist(Polylist *L) { } Polylist CreatePolylist()/*输入多项式的系数和指数,用尾插法建立一元多项式的链表*/ { } int LengthPolylist(Polylist L) { } void OutputPolylist(Polylist L) { } Polylist AddPolylist()/*创建两个多项式并相加,完成后显示序列*/ { } void ComputePolylist(Polylist L)/*计算多项式在x=%d的值*/ { } void DestroyPolylist(Polylist L) { }补全代码
/*单链表模块*/
/*初始化链表*/
void InitPolylist(Polylist *L) {
*L = NULL;
}
/*创建一元多项式的链表*/
Polylist CreatePolylist() {
Polylist L, p, q;
int n, i;
double coef;
int exp;
InitPolylist(&L);
printf("请依次输入多项式的项数、系数和指数:\n");
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%lf%d", &coef, &exp);
p = (Polylist)malloc(sizeof(Polynode));
p->coef = coef;
p->exp = exp;
p->next = NULL;
if (L == NULL) {
L = p;
} else {
q = L;
while (q->next != NULL) {
q = q->next;
}
q->next = p;
}
}
return L;
}
/*计算链表的长度*/
int LengthPolylist(Polylist L) {
int len = 0;
Polylist p = L;
while (p != NULL) {
len++;
p = p->next;
}
return len;
}
/*输出链表*/
void OutputPolylist(Polylist L) {
Polylist p = L;
if (p == NULL) {
printf("多项式为空!\n");
} else {
printf("多项式的各项系数和指数分别为:\n");
while (p != NULL) {
printf("%.2lfX^%d ", p->coef, p->exp);
p = p->next;
}
printf("\n");
}
}
/*创建两个多项式并相加,完成后返回相加后的链表*/
Polylist AddPolylist() {
Polylist L1, L2, L3, p1, p2, p3, q;
InitPolylist(&L1);
InitPolylist(&L2);
InitPolylist(&L3);
printf("请输入第1个多项式:\n");
L1 = CreatePolylist();
printf("请输入第2个多项式:\n");
L2 = CreatePolylist();
p1 = L1;
p2 = L2;
p3 = L3;
while (p1 != NULL && p2 != NULL) {
q = (Polylist)malloc(sizeof(Polynode));
if (p1->exp == p2->exp) {
q->coef = p1->coef + p2->coef;
q->exp = p1->exp;
p1 = p1->next;
p2 = p2->next;
} else if (p1->exp > p2->exp) {
q->coef = p1->coef;
q->exp = p1->exp;
p1 = p1->next;
} else {
q->coef = p2->coef;
q->exp = p2->exp;
p2 = p2->next;
}
q->next = NULL;
p3->next = q;
p3 = q;
}
while (p1 != NULL) {
q = (Polylist)malloc(sizeof(Polynode));
q->coef = p1->coef;
q->exp = p1->exp;
q->next = NULL;
p3->next = q;
p3 = q;
p1 = p1->next;
}
while (p2 != NULL) {
q = (Polylist)malloc(sizeof(Polynode));
q->coef = p2->coef;
q->exp = p2->exp;
q->next = NULL;
p3->next = q;
p3 = q;
p2 = p2->next;
}
return L3;
}
/*计算多项式在x=%d的值*/
void ComputePolylist(Polylist L) {
int x;
double result = 0.0;
printf("请输入x的值:\n");
scanf("%d", &x);
Polylist p = L;
while (p != NULL) {
result += p->coef * pow(x, p->exp);
p = p->next;
}
printf("多项式在x=%d的值为:%.2lf\n", x, result);
}
/*销毁链表*/
void DestroyPolylist(Polylist L) {
Polylist p = L, q;
while (p != NULL) {
q = p->next;
free(p);
p = q;
}
}
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' 相加( 多项式链表中指数必须由小到大排列 ) 完整代码是什么
这是一段多项式加法的代码,完整代码如下:
```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` 函数实现了多项式的加法。
阅读全文