📄 add_multiply_polyn.cpp
字号:
//Add_Multiply_Polyn.cpp
//Add Polynomial Pa and Pb or Multiply Pa and Pb
#include <conio.h>
#include <iostream.h>
#include <malloc.h>
struct term
{ //Typedef LinkList with Polynomial
float coef;
int expn;
struct term *next;
}*Polynomial;
void CreatPolyn(struct term * &Polyn,int m)
{ //To Creatre a polynomial Polyn with HeadNode
int i;
struct term *q;
Polyn=(struct term *)malloc(sizeof(term));
Polyn->next=NULL;
for(i=m;i>0;--i)
{ //Reverse order inputing for Creating a Polynomial
q=(struct term *)malloc(sizeof(term));
cout<<"coef=";
cin>>q->coef;
cout<<"expn=";
cin>>q->expn;
q->next=Polyn->next;
Polyn->next=q;
}//end of for
if(m) cout<<"Success to Create a Polynomial!"<<endl;
else cout<<"A NULL LinkList have been created!"<<endl;
} //end of CreatPolyn() function
void IsDescending(struct term * &Polyn)
{ //To descend the polynomial Polyn
int temp0,temp1;
struct term *q0,*q1;
q0=Polyn->next;
do
{
q1=Polyn->next;
do
{
if(q0->expn>q1->expn)
{
temp0=q0->coef;q0->coef=q1->coef;q1->coef=temp0;
temp1=q0->expn;q0->expn=q1->expn;q1->expn=temp1;
}
q1=q1->next;
}while(q1!=NULL);//end of the second do---while
q0=q0->next;
}while(q0!=NULL);//end of the first do---while
}//end of IsDescending() function
void MergePolyn(struct term * &Polyn,struct term * &L)
{ //To merge like terms
struct term *q0,*temp;
L=(struct term *)malloc(sizeof(term));
L->next=NULL;
q0=Polyn->next;
temp=(struct term *)malloc(sizeof(term));
temp->coef=q0->coef;temp->expn=q0->expn;
temp->next=L->next;L->next=temp;
q0=q0->next;
while(q0)
{
if(q0->expn==temp->expn)
{
temp->coef+=q0->coef;
}
else
{
temp=(struct term *)malloc(sizeof(term));
temp->coef=q0->coef;temp->expn=q0->expn;
temp->next=L->next;L->next=temp;
}//end of if---else
q0=q0->next;
} //end of while
} //end of MergePolyn() function
void AddPolyn(struct term * &Pa,struct term * &Pb,struct term * &Pc)
{ //To add Polynomial Pa and Pb
struct term *qa,*qb,*qc;
Pc=(struct term *)malloc(sizeof(term));
Pc->next=NULL;
qa=Pa->next;
qb=Pb->next;
while(qa&&qb)
{
qc=(struct term *)malloc(sizeof(term));
if(qa->expn>qb->expn)
{
qc->coef=qa->coef;qc->expn=qa->expn;
qa=qa->next;
}
else if(qa->expn==qb->expn)
{
qc->coef=qa->coef+qb->coef;qc->expn=qa->expn;
qa=qa->next;qb=qb->next;
}
else
{
qc->coef=qb->coef;qc->expn=qb->expn;
qb=qb->next;
}//end of if---else
qc->next=Pc->next;Pc->next=qc;
}//end of while
if(qa&&!qb)
{
while(qa)
{
qc=(struct term *)malloc(sizeof(term));
qc->coef=qa->coef;qc->expn=qa->expn;qa=qa->next;
qc->next=Pc->next;Pc->next=qc;
}//end of while
}//end of if
if(!qa&&qb)
{
while(qb)
{
qc=(struct term *)malloc(sizeof(term));
qc->coef=qb->coef;qc->expn=qb->expn;qb=qb->next;
qc->next=Pc->next;Pc->next=qc;
}//end of while
}//end of else
}//end of AddPolyn() function
void MultiplyPolyn(struct term * &Pa,struct term * &Pb,struct term * &Pd)
{ //To multiply Polynomial Pa and Pb
struct term *qa,*qb,*qd;
Pd=(struct term *)malloc(sizeof(term));
Pd->next=NULL;
qa=Pa->next;
qb=Pb->next;
for(qa=Pa->next;qa!=NULL;)
{
qb=Pb->next;
for(;qb!=NULL;qb=qb->next)
{
qd=(struct term *)malloc(sizeof(term));
qd->coef=(qa->coef)*(qb->coef);
qd->expn=(qa->expn)+(qb->expn);
qd->next=Pd->next;Pd->next=qd;
}//end of the second for
qb=Pb->next;qa=qa->next;
}//end of the first for
}//end of MultiplyPolyn() function
void DisplayPolyn(struct term * &Polyn)
{ //To display the result
struct term *temp;
temp=Polyn;
while(temp->next)
{
temp=temp->next;
cout<<"(Pc.coef="<<temp->coef<<",";
cout<<"Pc.expn="<<temp->expn<<")"<<endl;
}//end of while
cout<<endl<<"OK...!"<<endl;
} //end of DisplayPolyn() function
void main()
{ //The main() function
char x;
struct term *Pa,*Pb,*Pc,*Pd,*La,*Lb,*Lc,*Ld,*add,*multiply;
int PolynLength_Pa,PolynLength_Pb;
cout<<"Add_Multiply_Polyn.cpp"<<endl<<"========================="<<endl;
cout<<endl<<"How many sessions of polynomial Pa do you want? (eg. 4): ";
cin>>PolynLength_Pa;
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;
CreatPolyn(Pa,PolynLength_Pa);
IsDescending(Pa);
MergePolyn(Pa,La);
cout<<endl<<"How many sessions of polynomial Pb do you want ? (eg. 3): ";
cin>>PolynLength_Pb;
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;
CreatPolyn(Pb,PolynLength_Pb);
IsDescending(Pb);
MergePolyn(Pb,Lb);
cout<<"Which operation do you want to run,add or multiply? (eg. + or *): ";
cin>>x;
if(x=='+')
{
AddPolyn(Pa,Pb,Pc);
IsDescending(Pc);
MergePolyn(Pc,Lc);
cout<<"Result:"<<endl;
DisplayPolyn(Lc);
}
if(x=='*')
{
MultiplyPolyn(Pa,Pb,Pd);
IsDescending(Pd);
MergePolyn(Pd,Ld);
cout<<"Result:"<<endl;
DisplayPolyn(Ld);
}
getch();
}//end of main() function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -