02-线性结构1. 一元多项式求导 链表
#include<stdio.h> #include<algorithm> using namespace std; 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,temp; int c,e; p=(polynomial)malloc(sizeof(struct polynode));//建立空结点 rear=p; while(~scanf("%d%d",&c,&e)) { temp==(polynomial)malloc(sizeof(struct polynode)); Attach(c,e,&rear);//生成新的项,并插入链表的尾部 } temp=p;//删除临时结点 p=p->link; free(temp); return p; } polynomial Polydifferentiation(polynomial p) { polynomial p1,p2;//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 Printfpoly(polynomial p) { bool flag=0; while(p) { if(flag) printf(" "); else flag++; printf("%d %d",p->coef,p->expon); p=p->link; } printf("\n"); } int main() { polynomial p; p=Readpoly();//读入多项式 p=Polydifferentiation(p);//求导 Printfpoly(p);//输入求导后结果 return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/