/*主函数模块*/ 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) { }
时间: 2024-03-22 09:40:38 浏览: 53
/*主函数模块*/
int main() {
Polylist H;
H = AddPolylist();
ComputePolylist(H);
OutputPolylist(H);
DestroyPolylist(H);
return 0;
}
/*单链表模块*/
void InitPolylist(Polylist *L) {
*L = (Polylist)malloc(sizeof(struct PolyNode));
(*L)->next = NULL;
}
Polylist CreatePolylist()/*输入多项式的系数和指数,用尾插法建立一元多项式的链表*/ {
Polylist L, rear, t;
int c, e;
InitPolylist(&L);
rear = L;
scanf("%d", &c);
scanf("%d", &e);
while(e != -1) {
t = (Polylist)malloc(sizeof(struct PolyNode));
t->coef = c;
t->expon = e;
t->next = NULL;
rear->next = t;
rear = t;
scanf("%d", &c);
scanf("%d", &e);
}
return L;
}
int LengthPolylist(Polylist L) {
int len = 0;
while(L->next) {
len++;
L = L->next;
}
return len;
}
void OutputPolylist(Polylist L) {
int len = LengthPolylist(L);
printf("%d ", len);
L = L->next;
while(L) {
printf("%d %d ", L->coef, L->expon);
L = L->next;
}
printf("\n");
}
Polylist AddPolylist()/*创建两个多项式并相加,完成后显示序列*/ {
Polylist A, B, C, rear;
int lenA, lenB, sum;
A = CreatePolylist();
B = CreatePolylist();
InitPolylist(&C);
rear = C;
while(A->next && B->next) {
if(A->next->expon > B->next->expon) {
rear->next = A->next;
A = A->next;
} else if(A->next->expon < B->next->expon) {
rear->next = B->next;
B = B->next;
} else {
sum = A->next->coef + B->next->coef;
if(sum) {
A->next->coef = sum;
rear->next = A->next;
A = A->next;
B = B->next;
} else {
A = A->next;
B = B->next;
}
}
rear = rear->next;
}
while(A->next) {
rear->next = A->next;
A = A->next;
rear = rear->next;
}
while(B->next) {
rear->next = B->next;
B = B->next;
rear = rear->next;
}
return C;
}
void ComputePolylist(Polylist L)/*计算多项式在x=%d的值*/ {
int x, sum = 0;
scanf("%d", &x);
while(L->next) {
sum += L->next->coef * pow(x, L->next->expon);
L = L->next;
}
printf("%d\n", sum);
}
void DestroyPolylist(Polylist L) {
Polylist temp;
while(L) {
temp = L;
L = L->next;
free(temp);
}
}
阅读全文