求大佬帮忙看看这个函数哪里有问题啊?该怎么修改?

Polynomial * AddPolyn(Polynomial *ha,Polynomial *hb){
Polynomial *L,*pa,*pb;
int a,b;
L=NULL;
pa=ha;
pb=hb;
while(pa&&pb){
a=pa->expn;
b=pb->expn;
switch(cmp(*pa,*pb)){
case -1:
L->next=pa;
L=pa;
pa=pa->next;
case 0:
if(pa->coef+pb->coef==0){
pa=pa->next;
pb=pb->next;
}
else{
pa->coef+=pb->coef;
L->next=pa;
L=pa;
pa=pa->next;
pb=pb->next;
}
case 1:
L->next=pb;
pb=pb->next;
L=pb;
L->next=pa;
}
}
if(pa) L->next=pa;
else L->next=pb;
return(L);
}

慕少森
浏览 54回答 1
1回答

慕娘9325324

//AddPolyn.cpp//Add Polynomial Pa and Pb&nbsp;#include <iostream.h>#include <malloc.h>#include <conio.h>#include <stdio.h>struct Term{ float coef;int expn;struct Term *next;}*LinkList;//typedef LinkList with polynomialvoid CreateList_L(struct Term * &L,int n){ //To Creatre a LinkList L with HeadNode&nbsp;int i;struct Term *p;L=(struct Term *)malloc(sizeof(Term));L->next=NULL;for(i=n;i>0;--i){p=(struct Term *)malloc(sizeof(Term));cout<<"coef = ";cin>>p->coef; //Reverse order inputing for Creating a LinkListcout<<"expn = ";cin>>p->expn;&nbsp;p->next=L->next;L->next=p;}//end of for&nbsp;&nbsp;if(n) cout<<"Success to Create a LinkList !"<<endl;else cout<<"A NULL LinkList have been created !"<<endl;} //end of function CreateListchar cmp(int qa_expn,int qb_expn) //cmp() function&nbsp;&nbsp;{//compare qa->expn and qb->expnif(qa_expn==qb_expn)&nbsp;return '=';else&nbsp;if(qa_expn>qb_expn) return'>';else return '<';}//end of cmp() functionvoid AddPolyn(struct Term * &Pa,struct Term * &Pb) //AddPolyn() function{ //Add the polynomial Pa and Pb&nbsp;&nbsp;struct Term *qa,*qb,*pre,*u;&nbsp;&nbsp;float sum;qa=Pa->next; //qa point to current node of Pa&nbsp;qb=Pb->next; //qb point to current node of Pb&nbsp;pre=Pa; //pre point to the HeadNode of Pawhile(qa&&qb) //neither qa nor qb are NULLswitch(cmp(qa->expn,qb->expn)){ case '<':pre=qa;qa=qa->next;break;case '=': //when the expn of qa == the expn of qb&nbsp;sum=qa->coef+qb->coef;if(sum!=0.0)&nbsp;&nbsp;{ qa->coef=sum;pre=qa; //modify the current Node coef values of Pa} //end of ifelse{ pre->next=qa->next; //otherwise qa->expn=qb->expn then delete current Node in Pafree(qa);} //end of elseqa=pre->next;u=qb;qb=qb->next;free(u);break;case '>':u=qb->next;qb->next=qa;pre->next=qb;pre=qb;qb=u;break;} //end of switchif(!qa) pre->next=qb; //to link the rest of node of Pbfree(Pb);cout<<endl;}//end of AddPolyn() functionvoid main() //main() function{&nbsp;&nbsp;struct Term *Pa,*Pb,*p;&nbsp;int InitLNodeNum_Pa,InitLNodeNum_Pb; //the Init LinkNode Numberscout<<"AddPolyn.cpp"<<endl<<"============="<<endl;&nbsp;&nbsp;cout<<endl<<"How many sessions of polynomial Pa do you want ? (eg. 4): ";cin>>InitLNodeNum_Pa;&nbsp;&nbsp;cout<<"Please input the Pa.coef and Pa.expn in descent order:"<<endl;cout<<"For example: (coef=3,expn=21);(coef=7,expn=9);(coef=4,expn=1);(coef=5,expn=0)"<<endl;CreateList_L(Pa,InitLNodeNum_Pa); //to create polynomial Pa LinkList with&nbsp;//the InitLNodeNum_Pa=input number +1&nbsp;&nbsp;cout<<endl<<"How many sessions of polynomial Pb do you want ? (eg. 3): ";cin>>InitLNodeNum_Pb;&nbsp;&nbsp;cout<<"Please input the Pb.coef and Pb.expn in descent order:"<<endl;cout<<"For example: (coef=-7,expn=9);(coef=13,expn=6);(coef=8,expn=1)"<<endl;CreateList_L(Pb,InitLNodeNum_Pb); //to create polynomial Pb LinkList with//the InitLNodeNum_Pb=input number +1&nbsp;&nbsp;AddPolyn(Pa,Pb); //AddPolyn Pa and Pbp=Pa;cout<<"Result:"<<endl;while(p->next) //output Pa=Pa+Pb{ p=p->next;cout<<"(Pc.coef="<<p->coef<<",";cout<<"Pc.expn="<<p->expn<<")"<<endl;&nbsp;}// end of whilecout<<endl<<"OK...!"<<endl;getch();}//end of main() function&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python