两个多项式相加相乘.docx
- 文档编号:13884126
- 上传时间:2023-06-18
- 格式:DOCX
- 页数:7
- 大小:14.97KB
两个多项式相加相乘.docx
《两个多项式相加相乘.docx》由会员分享,可在线阅读,更多相关《两个多项式相加相乘.docx(7页珍藏版)》请在冰点文库上搜索。
两个多项式相加相乘
#include
usingnamespacestd;
#defineNULL0
structdxs
{
intxishu;
intzhishu;
structdxs*next;
};
//输入多项式的系数和指数
dxs*creat(void)
{
dxs*head=NULL,*p,*q;
intn,i;
cout<<"多项式的指数从小到大输入:
"< cout<<"输入多项式的个数为: ";cin>>n; for(i=1;i<=n;i++) { p=newdxs; cout<<"输入第"< ";cin>>p->xishu; cout<<"输入第"< ";cin>>p->zhishu; if(i==1){head=p;q=p;} else{q->next=p;q=p;} } p->next=NULL;q->next=NULL; returnhead; } //输出多项式 voidprint(dxs*head) { dxs*p=head; while(p->next! =NULL) { if(p->next->xishu>0)cout< elseif(p->next->xishu<0)cout< p=p->next; } cout< } //多项式相加 dxs*add(dxs*head1,dxs*head2) { dxs*head,*q,*p1,*p2,*t; p1=head1;p2=head2; head=newdxs;head->xishu=NULL;head->zhishu=NULL;//head包含两项,是每一项都为空 //头指针的加法 if(p1->zhishu { head=p1;p1=p1->next; } elseif(p1->zhishu>p2->zhishu) { head=p2;p2=p2->next; } else { head->xishu=p1->xishu+p2->xishu;head->zhishu=p2->zhishu; p1=p1->next;p2=p2->next; } t=head; //中间项的加法 while(head! =NULL&&p1! =NULL&&p2! =NULL) { q=newdxs; if(p1->zhishu>p2->zhishu)//p1指数大与p2的指数; { q->xishu=p2->xishu;q->zhishu=p2->zhishu; p2=p2->next;t->next=q;t=t->next; } elseif(p1->zhishu { q->xishu=p1->xishu;q->zhishu=p1->zhishu; p1=p1->next;t->next=q;t=t->next; } elseif(p1->zhishu=p2->zhishu)//p1指数与p2的指数相等; { if((p1->xishu+p2->xishu)! =0)//指数相同,系数之和不等于0; { q->xishu=p1->xishu+p2->xishu;q->zhishu=p2->zhishu; p1=p1->next;p2=p2->next;t->next=q;t=t->next; } elseif((p1->xishu+p2->xishu)==0) { p1=p1->next;p2=p2->next;//指数相同,系数之和等于0; } } } if(p1==NULL&&p2==NULL)t->next=NULL; elseif(p1==NULL)t->next=p2; elseif(p2==NULL)t->next=p1; if(head->xishu==0)head=head->next; returnhead; } //多项式相加 /*dxs*add(dxs*head1,dxs*head2) { dxs*head,*p1,*p2,*t,*p; p1=head1;p2=head2; head=newdxs; head->xishu=NULL;head->zhishu=NULL; t=head; p=newdxs;p->xishu=NULL;p->zhishu=NULL;p=p->next; while(head1! =NULL&&head2! =NULL) { p=newdxs; if(p2->zhishu p->xishu=p2->zhishu;p->zhishu=p2->zhishu; t->next=p; t=t->next;p2=p2->next;} elseif(p1->zhishu p->xishu=p1->xishu;p->zhishu=p1->zhishu; t->next=p; t=t->next;p1=p1->next;} elseif(p1->zhishu==p2->zhishu) { if((p1->xishu+p2->xishu)==0){ p1=p1->next;p2=p2->next; } elseif((p1->xishu+p2->xishu)! =0) {p->xishu=p1->xishu+p2->xishu; p->zhishu=p1->zhishu; t->next=p;t=t->next;p1=p1->next;p2=p2->next;} } if(p1==NULL&&p2==NULL)t->next=NULL; elseif(p1==NULL)t->next=p2; elseif(p2==NULL)t->next=p1; returnhead; }*/ //多项式的相乘 dxs*chef(dxs*head1,dxs*head2) { dxs*head,*p1,*p2,*t,*a; head=newdxs;head->xishu=NULL;head->zhishu=NULL; t=head; for(p1=head1;p1! =NULL;p1=p1->next) for(p2=head2;p2! =NULL;p2=p2->next) { a=newdxs; a->xishu=p1->xishu*p2->xishu; a->zhishu=p1->zhishu+p2->zhishu; t->next=a;t=a; } t->next=NULL; returnhead->next; } //主函数 voidmain() { dxs*head1=NULL,*head2=NULL,*head=NULL; inti; head1=creat(); cout<<"第一个多项式为: "; print(head1);cout< head2=creat(); cout<<"第二个多项式为: "; print(head2);cout< cout<<"1.求和! "< cout<<"2.求积! "< switch(i) { case1: { head=add(head1,head2); cout<<"两个多项式的和为: "; print(head);cout< } case2: { head=chef(head1,head2); cout<<"两个多项式的积为: "; print(head);break; } } cout< }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 两个 多项式 相加 相乘