数据结构多项式运算器源代码.docx
《数据结构多项式运算器源代码.docx》由会员分享,可在线阅读,更多相关《数据结构多项式运算器源代码.docx(16页珍藏版)》请在冰豆网上搜索。
![数据结构多项式运算器源代码.docx](https://file1.bdocx.com/fileroot1/2022-11/16/ef537a40-2f0a-4994-9b67-c7314e5b0d11/ef537a40-2f0a-4994-9b67-c7314e5b0d111.gif)
数据结构多项式运算器源代码
#include
#include
#include
#include
typedefstruct{
floatcoef;
intexpn;
}term,ElemType;
typedefstructLNode{
ElemTypedata;
structLNode*next;
}*Link;
typedefstruct{
Linkhead,tail;
intlen;
}LinkList;
/*====================链表-ADT====================*/
voidMakeNode(Link&p,ElemTypee){
p=(Link)malloc(sizeof(structLNode));
if(!
p)exit
(1);
p->data=e;
p->next=NULL;
}//MakeNode
voidInitList(LinkList&L){
L.head=L.tail=(Link)malloc(sizeof(LNode));
if(!
L.head)exit
(1);
L.head->next=NULL;
L.len=0;
}//InitList
voidDestroyList(LinkList&L){
Linkp,q;
p=L.head->next;
while(p){
q=p->next;
free(p);
p=q;
}
L.head->next=NULL;
L.tail=L.head;
L.len=0;
}//DestroyList
voidInsLast(LinkList&L,Links){
L.tail->next=s;
L.tail=s;
L.len++;
L.tail->next=NULL;
}//InsLast
voidAppend(LinkList&L,Links){
Linkq;
intm;
if(!
s)exit
(1);
L.tail->next=s;
q=s;m=1;
while(q->next){
m++;
q=q->next;
}
L.len+=m;
L.tail=q;
}//Append
/*====================多项式-ADT====================*/
voidCreatPolyn(LinkList&L){
terme;
Links;
scanf("%f,%d",&e.coef,&e.expn);
while(e.coef){
MakeNode(s,e);
InsLast(L,s);
scanf("%f,%d",&e.coef,&e.expn);
}
L.tail->next=NULL;
}//CreatPolyn
intJudge(intn){
if(n>19||n<0){
printf("存储位置选择错误!
请重新选择0-19.\n");
return1;
}
else
return0;
}//Judge
voidPrintPolyn(LinkListL){
Linkp;
p=L.head->next;
if(!
p)
printf("NULL\n");
else{
printf("y=");
if(p->data.coef>0){
if(p->data.expn==0)
printf("%f",p->data.coef);
else
printf("%fx^%d",p->data.coef,p->data.expn);
p=p->next;
}
else{
if(p->data.expn==0)
printf("%f",p->data.coef);
else
printf("%fx^%d",p->data.coef,p->data.expn);
p=p->next;
}
while(p){
if(p->data.coef>0){
if(p->data.expn==0)
printf("+%f",p->data.coef);
else
printf("+%fx^%d",p->data.coef,p->data.expn);
p=p->next;
}
else{
if(p->data.expn==0)
printf("%f",p->data.coef);
else
printf("%fx^%d",p->data.coef,p->data.expn);
if(p->next)
p=p->next;
}
}
printf("\n");
}
}//PrintPolyn
voidPrintAll(LinkListS[]){
inti;
for(i=0;i<20;i++){
printf("%d.",i);
PrintPolyn(S[i]);
}
}//PrintAll
voidCopyPolyn(LinkListL,LinkList&S){
terme;
Links,p;
p=L.head->next;
while(p){
e.coef=p->data.coef;
e.expn=p->data.expn;
MakeNode(s,e);
InsLast(S,s);
p=p->next;
}
S.tail->next=NULL;
}//CopyPolyn
intAddPolyn(LinkList&L,LinkListP,LinkListQ){
Linka,b,c;
terme;
a=P.head->next;
b=Q.head->next;
while((a!
=NULL)&&(b!
=NULL)){
if(a->data.expndata.expn){
e.coef=a->data.coef;
e.expn=a->data.expn;
MakeNode(c,e);
InsLast(L,c);
a=a->next;
}
elseif(a->data.expn>b->data.expn){
e.coef=b->data.coef;
e.expn=b->data.expn;
MakeNode(c,e);
InsLast(L,c);
b=b->next;
}
elseif((a->data.coef+b->data.coef)!
=0){
e.coef=a->data.coef+b->data.coef;
e.expn=a->data.expn;
MakeNode(c,e);
InsLast(L,c);
a=a->next;
b=b->next;
}
elseif((a->data.coef+b->data.coef)==0){
a=a->next;
b=b->next;
}
}
if(!
a&&!
b){
L.tail->next=NULL;
return0;
}
if(!
b){
while(a){
e.coef=a->data.coef;
e.expn=a->data.expn;
MakeNode(c,e);
InsLast(L,c);
a=a->next;
}
}
if(!
a){
while(b){
e.coef=b->data.coef;
e.expn=b->data.expn;
MakeNode(c,e);
InsLast(L,c);
b=b->next;
}
}
return0;
}//AddPolyn
voidSubtractPolyn(LinkList&L,LinkListP,LinkListQ){
Linkp;
p=Q.head->next;
while(p){
p->data.coef=0-p->data.coef;
p=p->next;
}
AddPolyn(L,P,Q);
p=Q.head->next;
while(p){
p->data.coef=0-p->data.coef;
p=p->next;
}
}//SubtractPolyn
doubleResultPolyn(LinkListL,floatx){
Linkp;
doubley=0;
p=L.head->next;
while(p){
y+=(pow(x,p->data.expn)*p->data.coef);
p=p->next;
}
returny;
}//ResultPolyn
voidMultiplyPolyn(LinkList&L,LinkListP,LinkListQ){
Linkl,p,q;
LinkListE,F;
InitList(E);
InitList(F);
inti,j;
terme;
p=P.head->next;
q=Q.head->next;
for(i=0;ifor(j=0;j
e.coef=p->data.coef*q->data.coef;
e.expn=p->data.expn+q->data.expn;
MakeNode(l,e);
InsLast(E,l);
p=p->next;
}
AddPolyn(L,E,F);
DestroyList(E);
DestroyList(F);
CopyPolyn(L,F);
if(i!
=Q.len-1)
DestroyList(L);
q=q->next;
p=P.head->next;
}
}//MultiplyPolyn
voidPolyn_NFang(LinkList&L,LinkListP,intn){
inti;
LinkListE;
InitList(E);
CopyPolyn(P,E);
for(i=0;iMultiplyPolyn(L,E,P);
DestroyList(E);
CopyPolyn(L,E);
if(i!
=n-1)
DestroyList(L);
}
}//Polyn_NFang
voidPolyn_NDao(LinkList&L,LinkListP,intn){
Linkp,q;
inti,j;
CopyPolyn(P,L);
for(i=0;ip=L.head->next;
q=L.head;
for(j=0;jif(p