#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) { }补全代码
时间: 2023-12-07 10:04:05 浏览: 77
/*单链表模块*/
/*初始化链表*/
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;
}
}
阅读全文