数据结构 一元多项式加减法计算器

cpp;gutter:true;</p> <h1>include</h1> <p>using namespace std;</p> <p>struct Node//定义双向链表,方便降幂输出</p> <p>{</p> <pre><code>int C; int index; struct Node*prior; struct Node*next; </code></pre> <p>};</p> <p>void init(struct Node* &List)</p> <p>{</p> <p>List=(struct Node*)malloc(sizeof(struct Node));</p> <pre><code>List->next=NULL; </code></pre> <p>}</p> <p>void Sort(struct Node<em> List,struct Node</em>p)//链表幂从小到大排序,进来一个排一个</p> <p>{</p> <pre><code>struct Node*p1,*p2; int flag=0; if(List->next==NULL) { p->next=List->next; List->next=p; } else { p1=List; p2=List->next; while(p2!=NULL) { if(p->indexindex) { p1->next=p; p->next=p2; flag=1; break; } else if(p->index==p2->index) { p2->C+=p->C; flag=1; break; } else { p1=p2; p2=p2->next; } } if(flag==0) { p1->next=p; p->next=NULL; } } </code></pre> <p>}</p> <p>void Addition(struct Node<em>List1,struct Node</em>List2,struct Node*List3)//链表相加</p> <p>{</p> <pre><code>struct Node*p1,*p2,*p3,*p; p1=List1->next; p2=List2->next; p3=List3;//有意思 List3->next=NULL;//避免运行加减函数互相影响 while(p1!=NULL&&p2!=NULL) { p=(struct Node*)malloc(sizeof(struct Node)); p->next=NULL; if(p1->index==p2->index) { if(p1->C+p2->C!=0)//链表相加为0,直接不录入 { p->C=p1->C+p2->C; p->index=p1->index; p3->next=p; p3=p; } p1=p1->next; p2=p2->next; } else if(p1->indexindex) { p->C=p1->C; p->index=p1->index; p3->next=p; p3=p; p1=p1->next; } else { p->C=p2->C; p->index=p2->index; p3->next=p; p3=p; p2=p2->next; } } if(p1==NULL) { while(p2!=NULL) { p=(struct Node*)malloc(sizeof(struct Node)); p->next=NULL; p->C=p2->C; p->index=p2->index; p3->next=p; p3=p; p2=p2->next; } } else { while(p1!=NULL) { p=(struct Node*)malloc(sizeof(struct Node)); p->next=NULL; p->C=p1->C; p->index=p1->index; p3->next=p; p3=p; p1=p1->next; } } </code></pre> <p>}</p> <p>void Subtraction (struct Node<em>List1,struct Node</em>List2,struct Node*List3)//链表相减,只需在加的基础上改+为-</p> <p>{</p> <pre><code>struct Node*p1,*p2,*p3,*p; p1=List1->next; p2=List2->next; p3=List3;//有意思 List3->next=NULL;//重要 while(p1!=NULL&&p2!=NULL) { p=(struct Node*)malloc(sizeof(struct Node)); p->next=NULL; if(p1->index==p2->index) { if(p1->C-p2->C!=0) { p->C=p1->C-p2->C; p->index=p1->index; p3->next=p; p3=p; } p1=p1->next; p2=p2->next; } else if(p1->indexindex) { p->C=-p1->C; p->index=p1->index; p3->next=p; p3=p; p1=p1->next; } else { p->C=-p2->C; p->index=p2->index; p3->next=p; p3=p; p2=p2->next; } } if(p1==NULL) { while(p2!=NULL) { p=(struct Node*)malloc(sizeof(struct Node)); p->next=NULL; p->C=-p2->C; p->index=p2->index; p3->next=p; p3=p; p2=p2->next; } } else { while(p1!=NULL) { p=(struct Node*)malloc(sizeof(struct Node)); p->next=NULL; p->C=-p1->C; p->index=p1->index; p3->next=p; p3=p; p1=p1->next; } } </code></pre> <p>}</p> <p>void print(struct Node<em> List3,struct Node</em>p,struct Node*p3,int flag){</p> <p>cout<next;</p> <pre><code> p3=List3; while(p) { cout<C<index<prior=p3; p3=p; p=p->next; flag=1; } if(flag==0) cout<C<index<prior; } if(flag==0) cout<>n1; while(n1>n1; } cout<>e>>d>>f; p=(struct Node*)malloc(sizeof(struct Node)); if(e!=0) { p->C=e; p->index=f; Sort(List1,p); } } cout<>n2; while(n2>n2; } cout<>e>>d>>f; p=(struct Node*)malloc(sizeof(struct Node)); if(e!=0) { p->C=e; p->index=f; p2=(struct Node*)malloc(sizeof(struct Node)); p2->next=NULL; p1->next=p2; p1=p2; Sort(List2,p); } } cout<>sum1; while(sum1!=1&&sum1!=2&&sum1!=3) { cout<>sum1; } cout<>sum1; while(sum1!=1&&sum1!=2&&sum1!=3&&sum1!=0) { cout<>sum1; } if(sum1==0) { cout< </code></pre> <p>

Original: https://www.cnblogs.com/linlinmailbox/p/16492825.html
Author: 霖霖的信箱
Title: 数据结构 一元多项式加减法计算器

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/607363/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球