Polylist AddPolylist()/*创建两个多项式并相加,完成后显示序列*/ { Polylist x,y,a,b,tail,tl,temp; double sum=0; x=CreatePolylist(); y=CreatePolylist(); a=x->next;/*x->next赋给a*/ b=y->next; tail =(Polylist)malloc(sizeof(Polynode)) ; tail->next=NULL; tl=tail; while(a!=NULL&&b!=NULL){ if(a->exp<b->exp){/*a的指数小于b的指数*/ tl->next=a; tl=tl->next; a=a->next; }else if(a->exp=b->exp){/*a的指数等于b的指数*/ sum = a->coef+b->coef; if(sum!=0){ a->coef=sum; tl->next=a; tl=tl->next; a=a->next; b=b->next; }else{ temp =a; a=a->next; free(temp);/*释放temp*/ temp=b; b=b->next; free(temp); } }else{ tl->next=b; tl=tl->next; b=b->next; } } if(a!=NULL){ tl->next=a; }else tl->next=b; return tail; } void ComputePolylist(Polylist L)/*计算多项式在x=%d的值*/ { double x; double sum=0; scanf("%lf",&x); Polylist temp=L->next; while(temp->next!=NULL){ sum=sum+(temp->coef*x); temp=temp->next; } sum=sum+(temp->coef*x); printf("ComputePolylist in x=%.2f result is:%.2f\n",x,sum); } void DestroyPolylist(Polylist L) { Polylist list = L; list=NULL; printf("destroy"); }什么意思
时间: 2024-03-22 15:40:45 浏览: 76
这是一段 C 语言代码,主要是实现多项式的加法、计算多项式在某个值上的结果以及销毁多项式的功能。其中,CreatePolylist() 函数用于创建多项式链表;AddPolylist() 函数用于创建两个多项式并相加;ComputePolylist() 函数用于计算多项式在某个值上的结果;DestroyPolylist() 函数用于销毁多项式链表。具体实现过程可以参考代码注释。
相关问题
/*单链表模块*/ 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是链表的头指针*/
/*单链表模块*/
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是链表的头指针*/
#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;
}
}
阅读全文