作业帮 > 英语 > 作业

数据结构的二元多项式计算

来源:学生作业帮 编辑:百度作业网作业帮 分类:英语作业 时间:2024/07/09 02:32:30
数据结构的二元多项式计算
要求:1.能够按照指数和升序排列建立并输出多项式;2.能够完成两个多项式的加法、减法、乘法,并将结果存储于一个新的多项式中;
数据结构的二元多项式计算
polynomal.h--
#include
#include
#include"math.h"
struct Term
{
\x05float coef;
\x05int exp;
\x05Term *link;
\x05Term(float c,int e,Term *next=NULL)
\x05{
\x05\x05coef=c;
\x05\x05exp=e;
\x05\x05link=next;
\x05}
\x05Term *InsertAfter(float c,int e);
\x05friend ostream& operatorcoef,q->exp);
\x05q=q->link;
\x05p=p->link;
}
return *this;
}
istream& operator >> (istream& in,Polynomal& x)
{
\x05 Term *rear = x.first;\x05
float c;
int e,a;
\x05 int size=0;
\x05 couta) break;\x05
\x05 size++;
\x05\x05rear = rear->InsertAfter(c,e);\x05
\x05 }
\x05 return in;
};
ostream& operator link;
\x05 out coef;\x05\x05\x05 \x05
\x05\x05 \x05 if ( fabs(temp) > 0.001)
\x05\x05 pc = pc->InsertAfter(temp,pa->exp);
\x05\x05 pa = pa->link; pb = pb->link;
\x05\x05\x05 }
\x05 else
\x05\x05 \x05 if (pa->exp < pb->exp)
\x05\x05\x05 {
\x05\x05\x05 pc = pc->InsertAfter(pa->coef,pa->exp);
\x05\x05\x05 pa = pa->link;\x05\x05\x05\x05\x05
\x05\x05\x05 }
\x05\x05 else
\x05\x05\x05 {\x05 \x05
\x05\x05\x05 pc = pc->InsertAfter(pb->coef,pb->exp);
\x05\x05\x05 pb = pb->link;\x05\x05\x05\x05\x05
\x05\x05\x05 }\x05
\x05\x05\x05 }
p = (pa != NULL)?pa :pb;
while (p != NULL)
\x05\x05 {
\x05\x05 pc = pc->InsertAfter(p->coef,p->exp);
\x05\x05 p = p->link;
\x05\x05 }
\x05\x05\x05 return C;
};
Polynomal operator *(Polynomal& A,Polynomal& B)
{
Term *pa,*pb,*pc;
int AL,BL,k,maxExp;
Polynomal C;
pc=C.getHead();
AL=A.maxOrder();
BL=B.maxOrder();
if(AL!=-1||BL!=-1)
{
\x05 maxExp=AL+BL;
float *result=new float[maxExp+1];
for(int i=0;ilink ;
\x05 while(pa!=NULL)
\x05 {
\x05\x05 pb=B.getHead()->link ;
\x05\x05 while(pb!=NULL)
\x05\x05 {
\x05\x05\x05 k=pa->exp+pb->exp ;
\x05\x05\x05 result[k]=result[k]+(pa->coef)*(pb->coef) ;
\x05\x05\x05 pb=pb->link ;
\x05\x05 }
\x05\x05 pa=pa->link ;
\x05 }
\x05 for(i=0;i0.001)
\x05\x05\x05 pc=pc->InsertAfter (result[i],i);
\x05 }
\x05\x05 delete []result;
}
pc->link =NULL;
return C;
};
polynomal.cpp--
#include"polynomal.h"
#include
void main()
{
\x05Polynomal p1;
\x05cin>>p1;
\x05cout