void InitPolylist(Polylist *L) {*L = (Polylist)malloc(sizeof(Polylist)); (*L)->next = NULL; } Polylist CreatePolylist()/*输入多项式的系数和指数,用尾插法建立一元多项式的链表*/ {Polylist L; Polynode *s; InitPolylist(&L); double coef; int exp; Polynode *r = L; scanf("%lf %d",&coef,&exp); while(coef != 0) { s = (Polynode *)malloc(sizeof(Polynode)); s->coef = coef; s->exp = exp; s->next = NULL; r->next = s; r = s; scanf("%lf %d",&coef,&exp); } r->next = NULL; return L; } int LengthPolylist(Polylist L) {Polylist r = L; int n = 0; while(r->next != NULL) { n++; r = r->next; } return n; } void OutputPolylist(Polylist L) { Polylist r = L->next; printf("inlcude %d coef/exp list is:\n",LengthPolylist(L)); while(r != NULL) { printf("%.2f,%d\n",r->coef,r->exp); r = r->next; } } Polylist AddPolylist()/*创建两个多项式并相加,完成后显示序列*/ {Polylist l1 = CreatePolylist(); Polylist l2 = CreatePolylist(); Polylist l3; InitPolylist(&l3); Polynode *s; Polylist r1 = l1->next, r2 = l2->next, r3 = l3; double sum; while(r1 != NULL && r2 != NULL) { s = (Polynode *)malloc(sizeof(Polynode)); if(r1->exp < r2->exp) { s->coef = r1->coef; s->exp = r1->exp; r3->next = s; r3 = s; r1 = r1->next; } else if (r1->exp == r2->exp) { sum = r1->coef + r2->coef; if(sum != 0) { s->coef = sum; s->exp = r1->exp; r3->next = s; r3 = s; r1 = r1->next; r2 = r2->next; } else { return l3; } } else { s->coef = r2->coef; s->exp = r2->exp; r3->next = s; r3 = s; r2 = r2->next; } } if(r1 != NULL){ r3->next = r1; } else { r3->next = r2; } return l3; } void ComputePolylist(Polylist L)/*计算多项式在x=%d的值*/ {Polylist r = L->next; double x = 0; double result = 0; scanf("%lf",&x); while(r != NULL) { result += r->coef * pow(x, r->exp); r = r->next; } printf("ComputePolylist in x=%.2f result is:%.2f\n",x,result); } void DestroyPolylist(Polylist L) { Polynode *p = L ,*q = L->next; while(q) { free(p); p = q; q = q->next; } free(p); printf("destroy"); } 解释一下这个代码
时间: 2024-02-14 08:22:06 浏览: 67
这段代码是一个用于处理一元多项式的链表操作程序。其中,Polylist 是一个指向 Polynode 的指针,而 Polynode 则定义了多项式中的每一个节点,包括系数和指数。程序提供了创建多项式、计算多项式值、相加多项式等功能。具体来说,CreatePolylist 函数用于输入多项式的系数和指数,用尾插法建立一元多项式的链表;AddPolylist 函数用于创建两个多项式并相加,完成后显示序列;ComputePolylist 函数用于计算多项式在给定值 x 上的值;DestroyPolylist 函数用于销毁多项式链表。该程序中的链表操作用到了常见的链表操作技巧,如链表的初始化、节点的创建、节点移动等。
相关问题
void InitPolylist(Polylist *L) { (*L) = NULL; } Polylist CreatePolylist() { Polylist L=NULL, p=NULL, q=NULL; double c; int e; InitPolylist(&L); scanf("%lf%d", &c, &e); while (c != 0) { p=(Polylist)malloc(sizeof(Polynode)); p->coef=c; p->exp=e; p->next=NULL; if(L==NULL) { L=p; } else { q=L; while(q->next!=NULL) q=q->next; q->next=p; } scanf("%lf%d", &c, &e); } return L; } int LengthPolylist(Polylist L) { int n=0; Polylist p=L; while(p!=NULL) { n++; p=p->next; } return n; } void OutputPolylist(Polylist L) { Polylist p=L; if(p!=NULL) { OutputPolylist(p->next); printf("%.2f,%d", p->coef, p->exp); } printf("\n"); } Polylist AddPolylist() { Polylist A=NULL, B=NULL, C=NULL, p=NULL, q=NULL, r=NULL; A=CreatePolylist(); B=CreatePolylist(); InitPolylist(&C); p=A; q=B; while(p!=NULL && q!=NULL) { if(p->exp<q->exp) { r=(Polylist)malloc(sizeof(Polynode)); r->coef=q->coef; r->exp=q->exp; r->next=C; C=r; q=q->next; } else { if(p->exp==q->exp) { if(fabs(p->coef + q->coef)>1e-6) { r=(Polylist)malloc(sizeof(Polynode)); r->coef=p->coef+q->coef; r->exp=p->exp; r->next=C; C=r; } p=p->next; q=q->next; } else { r=(Polylist)malloc(sizeof(Polynode)); r->coef=p->coef; r->exp=p->exp; r->next=C; C=r; p=p->next; } } } while(p!=NULL) { r=(Polylist)ma
lloc(sizeof(Polynode)); r->coef=p->coef; r->exp=p->exp; r->next=C; C=r; p=p->next; } while(q!=NULL) { r=(Polylist)malloc(sizeof(Polynode)); r->coef=q->coef; r->exp=q->exp; r->next=C; C=r; q=q->next; } return C; }
请问这段代码实现了什么功能?
Polylist AddPolylist() { Polylist A=NULL, B=NULL, C=NULL, p=NULL, q=NULL, r=NULL; A=CreatePolylist(); B=CreatePolylist(); InitPolylist(&C); p=A; q=B; while(p!=NULL && q!=NULL) { if(p->exp<q->exp) { r=(Polylist)malloc(sizeof(Polynode)); r->coef=q->coef; r->exp=q->exp; r->next=C; C=r; q=q->next; } else { if(p->exp==q->exp) { if(fabs(p->coef + q->coef)>1e-6) { r=(Polylist)malloc(sizeof(Polynode)); r->coef=p->coef+q->coef; r->exp=p->exp; r->next=C; C=r; } p=p->next; q=q->next; } else { r=(Polylist)malloc(sizeof(Polynode)); r->coef=p->coef; r->exp=p->exp; r->next=C; C=r; p=p->next; } } } while(p!=NULL) { r=(Polylist)malloc(sizeof(Polynode)); r->coef=p->coef; r->exp=p->exp; r->next=C; C=r; p=p->next; } while(q!=NULL) { r=(Polylist)malloc(sizeof(Polynode)); r->coef=q->coef; r->exp=q->exp; r->next=C; C=r; q=q->next; } return C; }
这段代码实现了两个多项式相加的操作,其中Polylist是一个链表结构,每个节点包含一个系数和指数。代码首先调用CreatePolylist函数分别创建了两个多项式链表A和B,然后初始化结果链表C为空。接下来使用p和q指向A和B的头节点,依次遍历两个链表的每个节点,根据节点的指数大小关系进行不同的操作。
如果A的当前节点的指数小于B的当前节点的指数,则将B的当前节点的系数与指数插入到结果链表C的头部,并将q指向下一个节点。
如果A的当前节点的指数等于B的当前节点的指数,则将A和B的当前节点的系数相加,判断和是否不为0,如果不为0,则将和与指数插入到结果链表C的头部,并将p和q指向下一个节点。
如果A的当前节点的指数大于B的当前节点的指数,则将A的当前节点的系数与指数插入到结果链表C的头部,并将p指向下一个节点。
当其中一个链表遍历完毕后,将另一个链表中剩余的节点插入到结果链表C的头部。
最后返回结果链表C。
阅读全文