一元多项式的运算源代码[数据结构]

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 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.expnnext->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.expnnext->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 Create_Poly(Polynomial &L){
double temp=0;
Init_Poly(L);
LNode *p=L,*q;
cout<<"输入多项式的系数->指数->系数->指数...以空格隔开,以系数为0结束:"<cin>>temp;
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<if(!p) cout<<0;
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<data.coef);
else
if(fabs(p->data.coef)-1) cout<data.coef)<<"x";
else cout<<"x";
if(p->data.expn-1&&p->data.expn){
if(p->data.expn>0) cout<<"^"<data.expn;
else cout<<"^("<data.expn<<")";
}
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<cout<<"把"<cout<<"把"<cin>>temp;
if(temp==1){ ExistCopy_Poly(La,Lb); cout<<"复制后的多项式为: "; Print_Poly(Lb,v2); cout<if(temp==2){ ExistCopy_Poly(Lb,La); cout<<"复制后的多项式为: "; Print_Poly(La,v1); cout<if(temp==3){ Lc=BlankCopy_Poly(La); cout<<"复制后的多项式为: "; Print_Poly(Lc,'h'); cout<if(temp==4){ Lc=BlankCopy_Poly(Lb); cout<<"复制后的多项式为: "; Print_Poly(Lc,'h'); cout<}
else{
cout<<"已存在一个多项式,为: "; Print_Poly(La,v1); cout<cout<<"把"<>temp;
if(temp==1){ Lb=BlankCopy_Poly(La); cout<<"复制后的多项式为: "; Print_Poly(Lb,v2); cout<else {
cout<<"请创建被复制的多项式!"<ExistCopy_Poly(La,Lb); cout<<"复制后的多项式为: "; Print_Poly(Lb,v2); cout<}
}
else {
cout<<"不存在多项式,请创建!"<Lb=BlankCopy_Poly(La); cout<<"复制后的多项式为: "; Print_Poly(Lb,v2); 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.expndata.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;iLb=BlankCopy_Poly(Lc);
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;idata.expn;i++) temp*=x;
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.expnnext->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;iLNode *p=La,*q=p->next;
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"<if(temp==1){ cout<<"+ln|x|

+C"<if(temp==-1){ cout<<"-ln|x|+C"<if(temp>0) cout<<"+"<else cout<}
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<return; }
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(!temp||!(a+b)){ cout<if(temp>0){
if(a*b==1){
if(temp==1/2){cout<<"+ln"<else{ cout<<"+"<<2*temp<<"ln"<}
if(temp==1){
if(a==1){ cout<<"+ln"<if(b==1){ cout<<"-ln"<cout<<"+ln"<}
else{
if(a==1){ cout<<"+"<if(b==1){ cout<<"-"<cout<<"+"<}
}
else{
if(a*b==1){
if(temp==-1/2){ cout<<"-ln"<else{ cout<<2*temp<<"ln"<}
if(temp==-1){
if(a==1){ cout<<"-ln"<if(b==1){ cout<<"+ln"<cout<<"-ln"<}
else{
if(a==1){ cout<if(b==1){ cout<<"+"<cout<}
}
}
void Menu_Poly(){
cout<<"==============================="<cout<<" #-> 打开选择菜单!"<cout<<" A-> 退出程序!"<cout<<" B-> 创建多项式!"<cout<<" C-> 打印多项式!"<cout<<" D-> 销毁多项式!"<cout<<" E-> 复制多项式!"<cout<<" F-> 多项式的和!"<cout<<" G-> 多项式的差!"<cout<<" H-> 多项式的积!"<cout<<" I-> 多项式的n次幂!"<cout<<" J-> 多项式在x=a处的值!"<cout<<" K-> 多项式的商!"<cout<<" L-> 多项式的余式!"<cout<<" M-> 多项式的最大公约式!"<cout<<" N-> 多项式的最小公倍式!"<cout<<" O-> 多项式的n阶导数!"<cout<<" P-> 多项式的不定积分!"<cout<<" Q-> 多项式在[a,b]上的定积分!"<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<<" 欢迎使用一元多项式的运算!"<Menu_Poly();
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<<"==============================="<cout<<"感谢使用本程序,下次再见 *^_^* !"<cout<<"==============================="<case 'B':
if(!La){ cout<<"请输入函数名:"; cin>>v1; Create_Poly(La); }
else{
if(!Lb){ cout<<"已存在一个多项式,将会再创建一个!"<>v2; Create_Poly(Lb); }
else { cout<<"已存在两个多项式,将会删除第二个并创建!"<} break;
case 'C':
if(La){ Print_Poly(La,v1); cout<if(Lb){ Print_Poly(Lb,v2); cout<if(!La&&!Lb) { cout<<"未创建多项式,先创建好不好-_-!"<break;
case 'D':
if(!La&&!Lb) cout<<"未创建多项式,没有可销毁的-_-!"<if(La){ Print_Poly(La,v1); cout<if(Lb){ Print_Poly(Lb,v2); cout<break;
case 'E':
Copy_Poly(La,Lb,v1,v2); break;
case 'F':
if(La&&Lb){ Print_Poly(La,v1); cout<if(La&&!Lb){ cout<<"已存在一个多项式,再创建一个!"<if(!La&&!Lb){ cout<<"创建第一个多项式!"<cout<<"请输入多项式!"<cout<<"创建第二个多项式!"<Add_Poly(La,Lb); Print_Poly(La,v1); cout<break;
case 'G':
if(La&&Lb){ Print_Poly(La,v1); cout<if(La&&!Lb){ cout<<"已存在一个多项式,再创建一个!"<if(!La&&!Lb){ cout<<"创建第一个多项式!"<cout<<"请输入多项式!"<cout<<"创建第二个多项式!"<Subtract_Poly(La,Lb); Print_Poly(La,v1); cout<break;
case 'H':
if(La&&Lb){ Print_Poly(La,v1); cout<if(La&&!Lb){ cout<<"已存在一个多项式,再创建一个!"<if(!La&&!Lb){ cout<<"创建第一个多项式!"<cout<<"请输入多项式!"<cout<<"创建第二个多项式!"<Multiply_Poly(La,Lb); Print_Poly(La,v1); cout<break;
case 'I':
cout<<"请输入n次幂的n=:"; cin>>n;
if(!La){ cout<<"不存在

多项式,请创建!"<Print_Poly(La,v1); cout<cout<<"求n次幂后的多项式为: "; Print_Poly(La,v1); cout<case 'J':
if(!La){ cout<<"不存在多项式,请创建!"<cout<<"请输入x="; cin>>x;
count=Count_Poly(La,x); cout<case 'K':
if(La&&Lb){ Print_Poly(La,v1); cout<if(La&&!Lb){ cout<<"已存在一个多项式,再创建一个!"<if(!La&&!Lb){ cout<<"创建第一个多项式!"<cout<<"请输入多项式!"<cout<<"创建第二个多项式!"<Lc=Divide_Poly(La,Lb); cout<case 'L':
if(La&&Lb){ Print_Poly(La,v1); cout<if(La&&!Lb){ cout<<"已存在一个多项式,再创建一个!"<if(!La&&!Lb){ cout<<"创建第一个多项式!"<cout<<"请输入多项式!"<cout<<"创建第二个多项式!"<Divide_Poly(La,Lb); cout<case 'M':
if(La&&Lb){ Print_Poly(La,v1); cout<if(La&&!Lb){ cout<<"已存在一个多项式,再创建一个!"<if(!La&&!Lb){ cout<<"创建第一个多项式!"<cout<<"请输入多项式!"<cout<<"创建第二个多项式!"<GCDivisor_Poly(La,Lb); cout<<"它们的最大公约式为: "; Print_Poly(La,v1); cout<case 'N':
if(La&&Lb){ Print_Poly(La,v1); cout<if(La&&!Lb){ cout<<"已存在一个多项式,再创建一个!"<cout<<"请输入多项式!"<if(!La&&!Lb){ cout<<"创建第一个多项式!"<cout<<"请输入多项式!"<cout<<"创建第二个多项式!"<LCMultiple_Poly(La,Lb); cout<<"它们的最小公倍式为: "; Print_Poly(La,v1); cout<case 'O':
cout<<"请输入n阶导数的n="; cin>>i;
if(!La){ cout<<"不存在多项式,请创建!"<

项式!"<Print_Poly(La,v1); cout<cout<<"求"<case 'P':
if(!La){ cout<<"不存在多项式,请创建!"<Print_Poly(La,v1); cout<IndefiniteIntegral_Poly(La,v1); break;
case 'Q':
if(!La){ cout<<"不存在多项式,请创建!"<Print_Poly(La,v1); cout<

相关文档
最新文档