n个一元多项式的加减乘除执行代码

上传人:第*** 文档编号:38821111 上传时间:2018-05-08 格式:DOC 页数:11 大小:46.50KB
返回 下载 相关 举报
n个一元多项式的加减乘除执行代码_第1页
第1页 / 共11页
n个一元多项式的加减乘除执行代码_第2页
第2页 / 共11页
n个一元多项式的加减乘除执行代码_第3页
第3页 / 共11页
n个一元多项式的加减乘除执行代码_第4页
第4页 / 共11页
n个一元多项式的加减乘除执行代码_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《n个一元多项式的加减乘除执行代码》由会员分享,可在线阅读,更多相关《n个一元多项式的加减乘除执行代码(11页珍藏版)》请在金锄头文库上搜索。

1、 N 个一元多项式的加减乘除执行代码 #include using namespace std;struct Node public: int coe; int exp; Node *next; ;class List public: Node *CreatList(Node *Head);Node *InsertNode(Node *Head,Node *Ptr); Node *AddList(Node *Head1,Node *Head2); Node *SubList(Node *Head1,Node *Head2); Node *MulList(Node *Head1,Node *He

2、ad2); ;Node *List:CreatList(Node *Head) int num=0; Node *Ptr=new Node; Ptr-next=NULL; coutnum; for (int i=1;iPtr-coe; cinPtr-exp; if (Ptr-coe=0) else InsertNode(Head,Ptr); Ptr=new Node; Ptr-next=NULL; return Head; Node *List:InsertNode(Node *Head,Node *Ptr) Node *PrePos=new Node; Node *NowPos=Head-n

3、ext; if (NowPos=NULL) Head-next=Ptr; Ptr-next=NULL; else PrePos=Head; while(NowPos!=NULL) if (Ptr-expNowPos-exp) PrePos-next=Ptr; Ptr-next=NowPos; break; else if(Ptr-expexp) PrePos=NowPos; NowPos=NowPos-next; else if (Ptr-coe+NowPos-coe)=0) PrePos-next=NowPos-next; delete NowPos; break; else NowPos-

4、coe=Ptr-coe+NowPos-coe; break; if (NowPos=NULL) PrePos-next=Ptr; Ptr-next=NULL; return Head; Node *List:AddList(Node *Head1,Node *Head2) Node *NewHead=new Node; Node *Ptr=new Node; Ptr-next=NULL; NewHead-next=NULL; Node *Ptr1=Head1-next; Node *Ptr2=Head2-next; if (Ptr1=NULL) if (Ptr2=NULL) else whil

5、e(Ptr2!=NULL) Ptr-coe=Ptr2-coe; Ptr-exp=Ptr2-exp; NewHead=InsertNode(NewHead,Ptr); Ptr=new Node; Ptr-next=NULL; Ptr2=Ptr2-next; else if (Ptr2=NULL) while(Ptr1!=NULL) Ptr-coe=Ptr1-coe; Ptr-exp=Ptr1-exp; NewHead=InsertNode(NewHead,Ptr); Ptr=new Node;Ptr-next=NULL; Ptr1=Ptr1-next; else while(Ptr1!=NULL

6、) Ptr-coe=Ptr1-coe; Ptr-exp=Ptr1-exp; NewHead=InsertNode(NewHead,Ptr); Ptr=new Node; Ptr-next=NULL; Ptr1=Ptr1-next; while(Ptr2!=NULL) Ptr-coe=Ptr2-coe; Ptr-exp=Ptr2-exp; NewHead=InsertNode(NewHead,Ptr); Ptr=new Node; Ptr-next=NULL; Ptr2=Ptr2-next; return NewHead; Node *List:SubList(Node *Head1,Node

7、*Head2) Node *NewHead=new Node; Node *Ptr=new Node; Ptr-next=NULL; NewHead-next=NULL; Node *Ptr1=Head1-next; Node *Ptr2=Head2-next; if (Ptr1=NULL) if (Ptr2=NULL) else while(Ptr2!=NULL) Ptr-coe=-Ptr2-coe; Ptr-exp=Ptr2-exp; NewHead=InsertNode(NewHead,Ptr); Ptr=new Node; Ptr-next=NULL; Ptr2=Ptr2-next;

8、else if (Ptr2=NULL) while(Ptr1!=NULL) Ptr-coe=Ptr1-coe; Ptr-exp=Ptr1-exp; NewHead=InsertNode(NewHead,Ptr); Ptr=new Node; Ptr-next=NULL; Ptr1=Ptr1-next; else while(Ptr1!=NULL) Ptr-coe=Ptr1-coe; Ptr-exp=Ptr1-exp; NewHead=InsertNode(NewHead,Ptr); Ptr=new Node; Ptr-next=NULL; Ptr1=Ptr1-next; while(Ptr2!

9、=NULL) Ptr-coe=-Ptr2-coe; Ptr-exp=Ptr2-exp; NewHead=InsertNode(NewHead,Ptr);Ptr=new Node; Ptr-next=NULL; Ptr2=Ptr2-next; return NewHead; Node *List:MulList(Node *Head1,Node *Head2) Node *NewHead=new Node; Node *Ptr=new Node; Ptr-next=NULL; NewHead-next=NULL; Node *Ptr1=Head1-next; Node *Ptr2=Head2-n

10、ext; if (Ptr1=NULL) else if (Ptr2=NULL) else while(Ptr1!=NULL) while(Ptr2!=NULL) Ptr-coe=Ptr1-coe*Ptr2-coe; Ptr-exp=Ptr1-exp+Ptr2-exp; NewHead=InsertNode(NewHead,Ptr); Ptr=new Node; Ptr-next=NULL; Ptr2=Ptr2-next; Ptr=new Node; Ptr-next=NULL; Ptr2=Head2-next; Ptr1=Ptr1-next; return NewHead; void Show

11、List(Node *Head) Node *Ptr=Head-next; if (Ptr=NULL) coutcoe0) if (Ptr-coe=1) if (Ptr-exp=0) coutcoe; else if(Ptr-exp=1) coutexp; else if (Ptr-exp=0) coutcoe; else if(Ptr-exp=1) coutcoecoeexp; else if (Ptr-coe=-1) if (Ptr-exp=0) coutcoe; else if(Ptr-exp=1) coutexp; else if (Ptr-exp=0) coutcoe; else i

12、f(Ptr-exp=1) coutcoecoeexp; Ptr=Ptr-next; while (Ptr!=NULL) if (Ptr-coe0) if (Ptr-coe=1) if (Ptr-exp=0) coutcoe; else if(Ptr-exp=1) coutexp; else if (Ptr-exp=0) coutcoe; else if(Ptr-exp=1) coutcoecoeexp; else if (Ptr-coe=-1) if (Ptr-exp=0) coutcoe; else if(Ptr-exp=1) coutexp; else if (Ptr-exp=0) cou

13、tcoe; else if(Ptr-exp=1) coutcoecoeexp; Ptr=Ptr-next; void main() Node *head1=new Node; Node *head2=new Node; head1-next=NULL; head2-next=NULL; List list1; List list2; List NewList; head1=list1.CreatList(head1); head2=list2.CreatList(head2); cout“f(x)=“; ShowList(head1); coutendl; cout“g(x)=“; ShowList(head2); coutendl; cout“两式相加的结果“endl; cout“F(x)=“; ShowList(NewList.AddList(head1,head2); coutendl;cout“两式相减的结果“endl; cout“F(x)=“; ShowList(NewList.SubList(head1,head2); coutendl; cout“两式相乘的结果“endl; cout“F(x)=“; ShowList(NewList.MulList(head1,head2); coutendl; system(“pause“);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号