#include<stdio.h>
#include<stdlib.h>
typedef struct PolyNode *Polynomial;
struct PolyNode{
int coef;
int expon;
Polynomial link;
};
void Attach(int c,int e,Polynomial *pRear)
{
Polynomial P;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->coef = c;
P->expon = e;
P->link = NULL;
(*pRear)->link = P;
*pRear = P;
}
Polynomial ReadPoly()
{
Polynomial P,Rear,t;
int c,e;
P = (Polynomial)malloc(sizeof(struct PolyNode));Rear = P;
while(scanf("%d %d",&c,&e) != EOF){
Attach(c,e,&Rear);
}
t = P;P = P->link;free(t);
return P;
}
Polynomial PolyDifferent(Polynomial P)
{
Polynomial P1,P2;
//处理一般项
P2 = P;P1 = NULL;
while(P2&&P2->expon)
{
P2->coef *= P2->expon;
P2->expon--;
P1 = P2;P2 = P2->link;
}
//处理最后的常数项
if(P2){
if(P1){
free(P2);
P1->link = NULL;
}
else{
P2->coef = 0;
}
}
return P;
}
void PrintPoly(Polynomial P)
{
printf("%d %d",P->coef,P->expon);
P = P->link;
while(P){
printf(" %d %d",P->coef,P->expon);
P = P->link;
}
printf("\n");
}
int main()
{
Polynomial P;
P = ReadPoly();
P = PolyDifferent(P);
PrintPoly(P);
}
多项式求导
最新推荐文章于 2021-11-19 19:31:14 发布