资源预览内容
第1页 / 共11页
第2页 / 共11页
第3页 / 共11页
第4页 / 共11页
第5页 / 共11页
第6页 / 共11页
第7页 / 共11页
第8页 / 共11页
第9页 / 共11页
第10页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 *Head2); ;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-next; 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-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 while(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) 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 *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; 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!=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-next; 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 ShowList(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 if(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) coutcoe; 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“);
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号