题目:编写一个算法,求一元多项式之和
考纲:一元多项式的表示与相加
题型:代码填空或算法设计
难度:★★★
参考代码
typedef struct node{float coef;//系数int exp;//次数struct node *next;
}polynode;
polynode *p,*q;//用单链表存储两个一元多项式
//
void polyadd(polynode &pa,polynode &pb){pc=pa;p=pa->next;q=pb->next;pre=pa;while(p&&q){switch(compare(p->exp,q->exp)){case -1:{pre=p;p=p->next;//(1)break;}case 0:{x=p->coef+q->coef;if(x!=0){p->coef=x;pre=p;}else{pre=p;//(2)free(p);}p=pre->next;r=q;q=q->next;free(r);break;}case 1:{r=q->next;q->next=p;pre->next=q;pre=q;q=r;break;}}if(q) p->next=q;//(3)free(pb);}
}
参考文章
一元多项式求和https://blog.csdn.net/weixin_44814196/article/details/115954734?ops_request_misc=&request_id=&biz_id=102&utm_term=%E6%B1%82%E4%B8%80%E5%85%83%E5%A4%9A%E9%A1%B9%E5%BC%8F%E4%B9%8B%E5%92%8C&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-115954734.142%5Ev96%5Epc_search_result_base2&spm=1018.2226.3001.4187