一元多项式的运算源代码[数据结构]
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
using namespace std;
typedef struct{
double coef;
int expn;
} ElemType;
typedef struct LNode{
ElemType data;
LNode *next;
} *Polynomial;
void Init_Poly(Polynomial &L){ //初始化多项式
L=new LNode;
L->data.expn=-1;
L->next=NULL;
}
void Order_Poly(Polynomial &L){
LNode *link=L;
while(link->next){
LNode *p=link->next,*q=p->next,*r=NULL;
while(q){
if(q->data.expn
p->next=q->next;
q->next=link->next;
link->next=q;
q=p->next;
}
else if(q->data.expn>link->next->data.expn) { p=q; q=p->next; }
else {
link->next->data.coef+=q->data.coef;
if(!link->next->data.coef){
if(link->next==p){ link->next=q->next; delete p; p=link->next; }
else { r=link->next; link->next=r->next; delete r; p->next=q->next; }
}
else { p->next=q->next; }
delete q;
if(!p) q=p;
else q=p->next;
}
} if(link->next) link=link->next;
}
}
void Invert_Poly(Polynomial &L){
LNode *link=L;
while(link->next){
LNode *p=link->next,*q=p->next,*r=NULL;
while(q){
if(q->data.expn>link->next->data.expn){
p->next=q->next;
q->next=link->next;
link->next=q;
q=p->next;
}
else if(q->data.expn
else {
link->next->data.coef+=q->data.coef;
if(!link->next->data.coef){
if(link->next==p){ link->next=q->next; delete p; p=link->next; }
else { r=link->next; link->next=r->next; delete r; p->next=q->next; }
}
else { p->next=q->next; }
delete q;
if(!p) q=p;
else q=p->next;
}
} if(link->next) link=link->next;
}
}
void Create_Poly(Polynomial &L){
double temp=0;
Init_Poly(L);
LNode *p=L,*q;
cout<<"输入多项式的系数->指数->系数->指数...以空格隔开,以系数为0结束:"<
while(temp){
q=new LNode;
p->next=q;
p=q;
p->data.coef=temp;
cin>>p->data.expn>>temp;
}
p->next=NULL;
Order_Poly(L);
cout<<"该多项式已创建!"<
void Print_Poly(Polynomial &L, char v){
LNode *p=L->next;
cout<
else
while(p){
if(p!=L->next&&p->data.coef>0) cout<<"+";
if((p!=L->next||(p==L->next&&p->data.coef<0))&&p->data.coef<0) cout<<"-";
if(!p->data.expn) cout<
else
if(fabs(p->data.coef)-1) cout<
else cout<<"x";
if(p->data.expn-1&&p->data.expn){
if(p->data.expn>0) cout<<"^"<
else cout<<"^("<
}
p=p->next;
}
}
void Destroy_Poly(Polynomial &L){
Polynomial p=L,q=p->next;
while(q){
delete p;
p=q;
q=p->next;
}
delete p;
L=NULL;
}
Polynomial BlankCopy_Poly(Polynomial &L){
Polynomial copy;
Init_Poly(copy);
Polynomial p=L->next,c1=copy,c2;
while(p){
c2=new LNode;
c1->next=c2;
c1=c2;
c1->data.coef=p->data.coef;
c1->data.expn=p->data.expn;
p=p->next;
}
c1->next=p;
return copy;
}
void ExistCopy_Poly(Polynomial &La, Polynomial &Lb){
LNode *pa=La->next,*pb=Lb,*p,*q;
while(pa&&pb->next){
pb=pb->next;
pb->data.coef=pa->data.coef;
pb->data.expn=pa->data.expn;
pa=pa->next;
}
if(!pb->next&&pa){
while(pa){
p=new LNode;
p->data.coef=pa->data.coef;
p->data.expn=pa->data.expn;
pb->next=p;
pb=pb->next;
pa=pa->next;
} pb->next=pa;
}
else if(!pa&&pb->next){
p=pb->next;
while(p){
q=p;
p=q->next;
delete q;
} pb->next=p;
}
}
void Copy_Poly(Polynomial &La, Polynomial &Lb, char v1, char v2){
int temp=0;
Polynomial Lc=NULL;
if(La){
if(Lb){
cout<<"已存在两个多项式,分别是: ";
Print_Poly(La,v1); cout<
if(temp==1){ ExistCopy_Poly(La,Lb); cout<<"复制后的多项式为: "; Print_Poly(Lb,v2); cout<
else{
cout<<"已存在一个多项式,为: "; Print_Poly(La,v1); cout<
if(temp==1){ Lb=BlankCopy_Poly(La); cout<<"复制后的多项式为: "; Print_Poly(Lb,v2); cout<
cout<<"请创建被复制的多项式!"<
}
else {
cout<<"不存在多项式,请创建!"<
if(Lc) Destroy_Poly(Lc);
}
void Add_Poly(Polynomial &La, Polynomial &Lb){
Polynomial ha=La,pa=ha->next,hb=Lb,pb=hb->next;
while(pa&&pb){
if(pb->data.expn
ha->next=pb;
ha=pb;
pb=pb->next;
ha->next=pa;
hb->next=pb;
}
else if(pb->data.expn==pa->data.expn){
pa->data.coef+=pb->data.coef;
if(!pa->data.coef){
ha->next=pa->next;
delete pa;
pa=ha->next; }
else{ ha=pa; pa=pa->next; }
hb=pb; pb=pb->next;
}
else{ ha=pa; pa=pa->next; }
}
if(!pa&&pb){ ha->next=pb; hb->next=NULL;}
Destroy_Poly(Lb);
}
void Subtract_Poly(Polynomial &La, Polynomial &Lb){
Polynomial p=Lb->next;
while(p){
p->data.coef=-p->data.coef;
p=p->next;
}
Add_Poly(La,Lb);
}
void Multiply_Poly(Poly
nomial &La, Polynomial &Lb){
Polynomial Lc,Ld,pa,pb,pc,pd;
Lc=BlankCopy_Poly(La); Ld=BlankCopy_Poly(La);
pa=La->next; pb=Lb->next; pc=Lc->next; pd=Ld->next;
while(pb){
while(pc){
pd->data.coef=pc->data.coef*pb->data.coef;
pd->data.expn=pc->data.expn+pb->data.expn;
pc=pc->next; pd=pd->next;
}
Add_Poly(La,Ld);
Ld=BlankCopy_Poly(Lc);
pd=Ld->next; pc=Lc->next; pb=pb->next;
}
Subtract_Poly(La,Lc);
Destroy_Poly(Lb);
}
void Index_Poly(Polynomial &La, int n){
Polynomial Lb,Lc;
Lc=BlankCopy_Poly(La);
for(int i=1;i
Multiply_Poly(La,Lb);
}
Destroy_Poly(Lc);
}
double Count_Poly(Polynomial &L, double x){
Polynomial p=L->next;
double temp,count=0;
while(p){
temp=p->data.coef;
if(p->data.expn<0){
for(int i=0;i<-p->data.expn;i++) temp/=x;
count+=temp; }
else{
for(int i=0;i
count+=temp; }
p=p->next;
}
return count;
}
Polynomial Divide_Poly(Polynomial &La, Polynomial &Lb){
Invert_Poly(La); Invert_Poly(Lb);
Polynomial Lc,Ld,pa=La->next,pb=Lb->next,pc,pd,p;
Lc=BlankCopy_Poly(Lb); pc=Lc->next;
Init_Poly(Ld); pd=Ld; p=new LNode; pd->next=p;
while(pa&&(pa->data.expn>=pb->data.expn)){
pd=p;
pd->data.coef=pa->data.coef/pb->data.coef;
pd->data.expn=pa->data.expn-pb->data.expn;
while(pc){
pc->data.coef*=(pa->data.coef/pb->data.coef);
pc->data.expn+=(pa->data.expn-pb->data.expn);
pc=pc->next;
}
Subtract_Poly(La,Lc); Invert_Poly(La);
Lc=BlankCopy_Poly(Lb); pc=Lc->next;
p=new LNode; pd->next=p;
pa=La->next; pb=Lb->next;
} pd->next=NULL; delete p;
Destroy_Poly(Lc);
return Ld;
}
Polynomial GCDivisor_Poly(Polynomial &La, Polynomial &Lb){
Invert_Poly(La); Invert_Poly(Lb);
Polynomial Lc=BlankCopy_Poly(Lb);
while(La->next&&Lb->next){
if(La->next->data.expn
ExistCopy_Poly(La,Lb); ExistCopy_Poly(Lc,La);
} ExistCopy_Poly(Lb,Lc);
Divide_Poly(La,Lb);
}
ExistCopy_Poly(Lc,La); Lb=NULL;
Destroy_Poly(Lc);
return La;
}
Polynomial LCMultiple_Poly(Polynomial &La, Polynomial &Lb){
Polynomial Lc=BlankCopy_Poly(La),Ld=BlankCopy_Poly(Lb),Le=GCDivisor_Poly(La,Lb),Lf;
Multiply_Poly(Lc,Ld);
Lf=Divide_Poly(Lc,Le); Destroy_Poly(Lc);
ExistCopy_Poly(Lf,La); Lb=NULL; Destroy_Poly(Lf);
return La;
}
void Derivative_Poly(Polynomial &La, int n){
for(int i=0;i
while(q){
q->data.coef*=q->data.expn;
--q->data.expn;
if(!q->data.coef){
p->next=q->next;
delete q;
q=p->next;
}
else{ p=q; q=p->next; }
}
}
}
void IndefiniteIntegral_Poly(Polynomial &La, char v){
LNode *p=La,*q=p->next;
double temp=0;
while(q){
++q->data.expn;
if(q->data.expn){ q->data.coef/=q->data.expn; p=q; }
else{ p->next=q->next; temp=q->data.coef; delete q;}
q=p->next;
}
Print_Poly(La,v);
if(!temp){ cout<<"+C"<
+C"<
void DefiniteIntegral_Poly(Polynomial &La, char v, double a, double b){
LNode *p=La,*q=p->next;
double temp=0;
if(q->data.expn<0&&a*b<=0){
cout<
while(q){
++q->data.expn;
if(q->data.expn){ q->data.coef/=q->data.expn; p=q; }
else{ p->next=q->next; temp=q->data.coef; delete q;}
q=p->next;
}
cout<
if(a*b==1){
if(temp==1/2){cout<<"+ln"<
if(temp==1){
if(a==1){ cout<<"+ln"<
else{
if(a==1){ cout<<"+"<
}
else{
if(a*b==1){
if(temp==-1/2){ cout<<"-ln"<
if(temp==-1){
if(a==1){ cout<<"-ln"<
else{
if(a==1){ cout<
}
}
void Menu_Poly(){
cout<<"==============================="<
void main(){
int n=1,i=1;
Polynomial La=NULL,Lb=NULL,Lc=NULL;
char v1='f',v2='g',flag='#';
double x,count,a,b;
cout<<" 欢迎使用一元多项式的运算!"<
while(true){
cout<<"请输入相应的序号:";
cin>>flag;
while(flag!='#'&&(flag>'Q'||fla
g<'A')){ cout<<"输入错误,请重新输入:"; cin>>flag; }
switch(flag){
case '#': Menu_Poly(); break;
case 'A':
if(La) Destroy_Poly(La); if(Lb) Destroy_Poly(Lb);
cout<<"==============================="<
if(!La){ cout<<"请输入函数名:"; cin>>v1; Create_Poly(La); }
else{
if(!Lb){ cout<<"已存在一个多项式,将会再创建一个!"<
else { cout<<"已存在两个多项式,将会删除第二个并创建!"<
case 'C':
if(La){ Print_Poly(La,v1); cout<
case 'D':
if(!La&&!Lb) cout<<"未创建多项式,没有可销毁的-_-!"<
case 'E':
Copy_Poly(La,Lb,v1,v2); break;
case 'F':
if(La&&Lb){ Print_Poly(La,v1); cout<
case 'G':
if(La&&Lb){ Print_Poly(La,v1); cout<
case 'H':
if(La&&Lb){ Print_Poly(La,v1); cout<
case 'I':
cout<<"请输入n次幂的n=:"; cin>>n;
if(!La){ cout<<"不存在
多项式,请创建!"<
if(!La){ cout<<"不存在多项式,请创建!"<
count=Count_Poly(La,x); cout<
if(La&&Lb){ Print_Poly(La,v1); cout<
if(La&&Lb){ Print_Poly(La,v1); cout<
if(La&&Lb){ Print_Poly(La,v1); cout<
if(La&&Lb){ Print_Poly(La,v1); cout<
cout<<"请输入n阶导数的n="; cin>>i;
if(!La){ cout<<"不存在多项式,请创建!"<
项式!"<
if(!La){ cout<<"不存在多项式,请创建!"<
case 'Q':
if(!La){ cout<<"不存在多项式,请创建!"<
DefiniteIntegral_Poly(La,v1,a,b); break;
}
}
}