N个一元多项式的加减乘除执行代码
N 个一元多项式的加减乘除执行代码 #include
using namespace std;
struct Node
{
public:
};
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 *List::CreatList(Node *Head)
{
int num=0;
Node *Ptr=new Node; Ptr->next=NULL; cout>num; for (int i=1;i>Ptr->coe; cin>>Ptr->exp; if (Ptr->coe==0) { } else { } InsertNode(Head,Ptr); Node *MulList(Node *Head1,Node *Head2); int coe; int exp; Node *next; 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->exp>NowPos->exp) { PrePos->next=Ptr; Ptr->next=NowPos; break; } else if(Ptr->expexp) { } else { if ((Ptr->coe+NowPos->coe)==0) { PrePos->next=NowPos->next; delete NowPos; PrePos=NowPos; NowPos=NowPos->next; break; } else { } NowPos->coe=Ptr->coe+NowPos->coe; break; } if (NowPos==NULL) {
} } } } 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;
} } Ptr1=Ptr1->next; } } else { } while(Ptr1!=NULL) { } while(Ptr2!=NULL) { Ptr->coe=Ptr2->coe; } Ptr->exp=Ptr2->exp; NewHead=InsertNode(NewHead,Ptr); Ptr=new Node; Ptr->next=NULL; Ptr2=Ptr2->next; Ptr->coe=Ptr1->coe; Ptr->exp=Ptr1->exp; NewHead=InsertNode(NewHead,Ptr); Ptr=new Node; Ptr->next=NULL; Ptr1=Ptr1->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; 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) { } while(Ptr2!=NULL) { Ptr->coe=-Ptr2->coe; Ptr->exp=Ptr2->exp; NewHead=InsertNode(NewHead,Ptr); Ptr->coe=Ptr1->coe; Ptr->exp=Ptr1->exp; NewHead=InsertNode(NewHead,Ptr); Ptr=new Node; Ptr->next=NULL; Ptr1=Ptr1->next;
}
} } } 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) { } else { if (Ptr->coe>0) { 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) { } else { coutcoe
} } } else { } if (Ptr->coe==-1) { } else { if (Ptr->exp==0) { } coutcoe; } else if(Ptr->exp==1) { coutcoecoeexp; if (Ptr->exp==0) { coutcoe; } else if(Ptr->exp==1) { } coutexp; } Ptr=Ptr->next; while (Ptr!=NULL) { if (Ptr->coe>0) { if (Ptr->coe==1) {
} else { { coutcoe; } else if(Ptr->exp==1) { } else { } coutexp; coutexp==0) { coutcoe; } else if(Ptr->exp==1) { coutcoecoeexp; } } else { if (Ptr->coe==-1) { if (Ptr->exp==0) { } else if(Ptr->exp==1) { } else { coutexp; coutcoe;
} } } else { if (Ptr->exp==0) { coutcoe; } else if(Ptr->exp==1) { } else { } coutcoeexp; coutcoenext; }
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
cout