《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“);