kezhixingdechengxu.docx

上传人:b****7 文档编号:26304333 上传时间:2023-06-17 格式:DOCX 页数:25 大小:17.31KB
下载 相关 举报
kezhixingdechengxu.docx_第1页
第1页 / 共25页
kezhixingdechengxu.docx_第2页
第2页 / 共25页
kezhixingdechengxu.docx_第3页
第3页 / 共25页
kezhixingdechengxu.docx_第4页
第4页 / 共25页
kezhixingdechengxu.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

kezhixingdechengxu.docx

《kezhixingdechengxu.docx》由会员分享,可在线阅读,更多相关《kezhixingdechengxu.docx(25页珍藏版)》请在冰豆网上搜索。

kezhixingdechengxu.docx

kezhixingdechengxu

只有加乘

#include

#include

typedefstructpnode//定义指针//

{intcoef;//定义系数//

intexp;//定义指数//

structpnode*next;

}pnode;

pnode*creat()//creat函数用来存放多项式//

{intm,n;

pnode*head,*rear,*s;

head=(pnode*)malloc(sizeof(pnode));

rear=head;

printf("\n输入指数(按递增顺序输入):

");

scanf("%d",&m);

printf("输入一元式系数(0为退出):

");

scanf("%d",&n);

do

{

s=(pnode*)malloc(sizeof(pnode));

s->coef=n;//n为系数//

s->exp=m;//m为指数//

rear->next=s;

s->next=NULL;

rear=s;

printf("\n输入指数(按递增顺序输入):

");

scanf("%d",&m);

printf("输入一元式系数(0为退出):

");

scanf("%d",&n);

}while(n);

returnhead;

}

pnode*add(pnode*heada,pnode*headb)

{pnode*headc,*a,*b,*s,*rearc;

intsum;

a=heada->next;b=headb->next;

headc=(pnode*)malloc(sizeof(pnode));

rearc=headc;

//多项式的存放//都放到s中里//

while(a!

=NULL&&b!

=NULL)//指数相等,则系数相加。

//

{

if(a->exp==b->exp)

{sum=a->coef+b->coef;

if(sum)

{s=(pnode*)malloc(sizeof(pnode));

s->coef=sum;

s->exp=a->exp;

rearc->next=s;

rearc=s;

a=a->next;

b=b->next;}

else

{a=a->next;

b=b->next;

}

}

elseif(a->expexp)

//a指数如果小于b,则a放到s中//

{s=(pnode*)malloc(sizeof(pnode));

s->coef=a->coef;

s->exp=a->exp;

rearc->next=s;

//用下一个结点s取代下一个c//

rearc=s;

a=a->next;

}

else//如果a的指数大,则b放到s中//

{s=(pnode*)malloc(sizeof(pnode));

s->coef=b->coef;

s->exp=b->exp;

rearc->next=s;

rearc=s;

b=b->next;

}

}

if(a)

{while(a!

=NULL)//b空了放a中的项//

{s=(pnode*)malloc(sizeof(pnode));

s->coef=a->coef;

s->exp=a->exp;

rearc->next=s;

s->next=NULL;

rearc=s;

a=a->next;

}

}

elseif(b)

{while(b!

=NULL)//a空了放b中的项//

{s=(pnode*)malloc(sizeof(pnode));

s->coef=b->coef;

s->exp=b->exp;

rearc->next=s;

s->next=NULL;

rearc=s;

b=b->next;

}}

returnheadc;

}

voidmain()

{pnode*a,*b,*c;

printf("建立A:

");

a=creat();

printf("\n建立B:

");

b=creat();

c=add(a,b);

c=c->next;

printf("%dx^%d",c->coef,c->exp);

c=c->next;

while(c!

=NULL)

{printf("+%dx^%d",c->coef,c->exp);

c=c->next;

}

}

只做加法

#include

#include

#include

typedefstructpolynode

{

intcoef;//多项式的系数

intexp;//指数

structpolynode*next;

}node;

node*create()//用尾插法建立一元多项式的链表

{

node*h,*r,*s;

intc,e;

h=(node*)malloc(sizeof(node));

r=h;

printf("coef:

");

scanf("%d",&c);

printf("exp:

");

scanf("%d",&e);

while(c!

=0)//输入系数为0时,多项式的输入结束

{

s=(node*)malloc(sizeof(node));

s->coef=c;

s->exp=e;

r->next=s;

r=s;

printf("coef:

");

scanf("%d",&c);

printf("exp:

");

scanf("%d",&e);

}

r->next=NULL;

return(h);

}

voidprint(node*p)//输出函数,打印出一元多项式

{

while(p->next!

=NULL)

{

p=p->next;

printf("%d*x^%d",p->coef,p->exp);

}

}

voidpolyadd(node*ha,node*hb)//一元多项式相加函数,用于将两个多项式相加,然后将和多项式存放在多项式ha中,并将多项式hb删除

{

node*p,*q,*pre,*temp;

intsum;

p=ha->next;

q=hb->next;

pre=ha;

while(p!

=NULL&&q!

=NULL)

{

if(p->expexp)

{

pre->next=p;

pre=pre->next;

p=p->next;

}

elseif(p->exp==q->exp)

{

sum=p->coef+q->coef;

if(sum!

=0)

{

p->coef=sum;

pre->next=p;pre=pre->next;p=p->next;

temp=q;q=q->next;free(temp);

}

else//如果系数和为零,则删除结点p与q,并将指针指向下一个结点

{

temp=p->next;free(p);p=temp;

temp=q->next;free(q);q=temp;

}

}

else

{

pre->next=q;

pre=pre->next;

q=q->next;

}

}

if(p!

=NULL)//将多项式A中剩余的结点加入到和多项式中

pre->next=p;

else

pre->next=q;

}

voidmultipoly(node*ha,node*hb)

{node*p,*q,*n,*m;

p=ha->next;

n=(node*)malloc(sizeof(node));

n->next=NULL;

while(p!

=NULL)

{m=(node*)malloc(sizeof(node));

for(q=hb->next;q;q=q->next)

{m->coef=p->coef*q->coef;

m->exp=p->exp+q->exp;

m->next=NULL;

}

p=p->next;

polyadd(n,m);

}

printf("多项式的积是:

\n");

print(n);

}

voidmain()

{

node*ha,*hb;

printf("请输入多项式ha的系数与指数:

\n");

ha=create();

print(ha);

printf("\n");

printf("请输入多项式hb的系数与指数:

\n");

hb=create();

print(hb);

printf("\n");

printf("多项式的和是:

\n");

polyadd(ha,hb);

print(ha);

printf("\n");

printf("多项式的积是:

\n");

multipoly(ha,hb);

print(ha);

}

做加法

#defineNULL0

#include

#include

typedefstructterm

{

intcoef;

intexpn;

structterm*next;

}term,*polynomial;

//建一个带头结点的单链表

term*creat(intn)

{

term*head,*p,*q;

inti,s;

head=(polynomial)malloc(sizeof(term));//带头结点的单链表

head->next=NULL;

head->coef=0;

head->expn=-1;

q=head;

for(i=n;i>0;i--)

{

p=(polynomial)malloc(sizeof(term));//生成新结点

printf("输入系数和指数:

");

scanf("%d,%d",&s,&p->expn);

p->coef=s;

p->next=q->next;//插入表尾

q->next=p;

q=p;

}

return(head);

}

voidprint(polynomialhead)//输出多项式

{

term*p;

p=head->next;

printf("%d*x^%d",p->coef,p->expn);

p=p->next;

while(p)

{

if(p->coef>0)

printf("+%d*x^%d",p->coef,p->expn);

elseif(p->coef<0)

printf("%d*x^%d",p->coef,p->expn);

p=p->next;

}

}

voidaddpolyn(polynomialf,polynomialg)//两个多项式的和

{

polynomialp,q,pre,u;

intsum;

p=f->next;

q=g->next;

pre=f;

while(p&&q)

{

if(p->expnexpn)

{

pre=p;

p=p->next;

}

elseif(p->expn==q->expn)

{

sum=p->coef+q->coef;

if(sum!

=0){p->coef=sum;pre=p;}

else{pre->next=p->next;free(p);}

p=pre->next;

u=q;

q=q->next;

free(u);

}

else

{

u=q->next;

q->next=p;

pre->next=q;

pre=q;

q=u;

}

}

if(q)//指针为指到表尾

{

pre->next=q;

}

free(g);//q将其连到链表f的后面

}

main()

{

polynomialf,g;

intn,m;

printf("n=");

scanf("%d",&n);

f=creat(n);

print(f);

printf("\n");

printf("m=");

scanf("%d",&m);

g=creat(m);

print(g);

printf("\n");

addpolyn(f,g);

printf("多项式的和是:

\n");

print(f);

}

没有通过

#defineNULL0

#include

#include

typedefstructterm

{

intcoef;

intexpn;

structterm*next;

}term,*polynomial;

//建一个带头结点的单链表

term*creat(intn)

{

term*head,*p,*q;

inti,s;

head=(polynomial)malloc(sizeof(term));//带头结点的单链表

head->next=NULL;

head->coef=0;

head->expn=-1;

q=head;

for(i=n;i>0;i--)

{

p=(polynomial)malloc(sizeof(term));//生成新结点

printf("输入系数和指数:

");

scanf("%d,%d",&s,&p->expn);

p->coef=s;

p->next=q->next;//插入表尾

q->next=p;

q=p;

}

return(head);

}

voidprint(polynomialhead)//输出多项式

{

term*p;

p=head->next;

printf("%d*x^%d",p->coef,p->expn);

p=p->next;

while(p)

{

if(p->coef>0)

printf("+%d*x^%d",p->coef,p->expn);

elseif(p->coef<0)

printf("%d*x^%d",p->coef,p->expn);

p=p->next;

}

}

voidaddpolyn(polynomialf,polynomialg)//两个多项式的和

{

polynomialp,q,pre,u;

intsum;

p=f->next;

q=g->next;

pre=f;

while(p&&q)

{

if(p->expnexpn)

{

pre=p;

p=p->next;

}

elseif(p->expn==q->expn)

{

sum=p->coef+q->coef;

if(sum!

=0){p->coef=sum;pre=p;}

else{pre->next=p->next;free(p);}

p=pre->next;

u=q;

q=q->next;

free(u);

}

else

{

u=q->next;

q->next=p;

pre->next=q;

pre=q;

q=u;

}

}

if(q)//指针为指到表尾

{

pre->next=q;

}

free(g);//q将其连到链表f的后面

}

polynomial*multiply(polynomialf,polynomialg)

{

polynomialh,l,gp,hp;

h=(polynomial)malloc(sizeof(term));

l=h->next;

h->next=NULL;

while(l!

=0)

{

hp=(polynomial)malloc(sizeof(term));

for(gp=g->next;gp;gp=gp->next)

{

hp->coef=l->coef*gp->coef;

hp->expn=l->expn*gp->expn;

hp->next=NULL;

}

l=l->next;

addpolyn(h,hp);

}

return(h);

}

voidmain()

{

polynomialf,g;

intn,m;

printf("n=");

scanf("%d",&n);

f=creat(n);

print(f);

printf("\n");

printf("m=");

scanf("%d",&m);

g=creat(m);

print(g);

printf("\n");

addpolyn(f,g);

printf("多项式的和是:

\n");

print(f);

printf("\n");

multiply(f,g);

printf("多项式的积是:

\n");

print(f);

}

未通过

#defineNULL0

#include

#include

typedefstructterm

{

intcoef;

intexpn;

structterm*next;

}term,*polynomial;

//建一个带头结点的单链表

term*creat(intn)

{

term*head,*p,*q;

inti,s;

head=(polynomial)malloc(sizeof(term));//带头结点的单链表

head->next=NULL;

head->coef=0;

head->expn=-1;

q=head;

for(i=n;i>0;i--)

{

p=(polynomial)malloc(sizeof(term));//生成新结点

printf("输入系数和指数:

");

scanf("%d,%d",&s,&p->expn);

p->coef=s;

p->next=q->next;//插入表尾

q->next=p;

q=p;

}

return(head);

}

voidprint(polynomialhead)//输出多项式

{

term*p;

p=head->next;

printf("%d*x^%d",p->coef,p->expn);

p=p->next;

while(p)

{

if(p->coef>0)

printf("+%d*x^%d",p->coef,p->expn);

elseif(p->coef<0)

printf("%d*x^%d",p->coef,p->expn);

p=p->next;

}

}

voidaddpolyn(polynomialf,polynomialg)//两个多项式的和

{

polynomialp,q,pre,u;

intsum;

p=f->next;

q=g->next;

pre=f;

while(p&&q)

{

if(p->expnexpn)

{

pre=p;

p=p->next;

}

elseif(p->expn==q->expn)

{

sum=p->coef+q->coef;

if(sum!

=0){p->coef=sum;pre=p;}

else{pre->next=p->next;free(p);}

p=pre->next;

u=q;

q=q->next;

free(u);

}

else

{

u=q->next;

q->next=p;

pre->next=q;

pre=q;

q=u;

}

}

if(q)//指针为指到表尾

{

pre->next=q;

}

free(g);//q将其连到链表f的后面

}

polynomialreverse(polynomiall)

{

polynomialp,q;

p=l->next;

l->next=NULL;

while(p)

{

q=p->next;

p->next=l->next;

l->next=p;

p=q;

}

return(l);

}

polynomial*multiply(polynomialf,polynomialg)

{

polynomialh,l,fp,gp,hp,q;

intmax,k,r;

intc;

max=25;

/*max=f->expn+g->expn;*/

h=(polynomial)malloc(sizeof(term));

h->coef=0;

h->expn=-1;

h->next=NULL;

hp=h;

l=reverse(g);

for(r=max;r>=0;r--)

{

c=0;

fp=f->next;

gp=l->next;

while(fp!

=NULL&&gp!

=NULL)

{

k=fp->expn+gp->expn;

if(k>r)

fp=fp->next;

elseif(k

gp=gp->next;

else

{

c+=fp->coef*gp->coef;

fp=fp->next;

gp=gp->next;

}

}

if(c!

=0)

{

q=(polynomial)malloc(sizeof(term));

q->expn=r;

q->coef=c;

q->next=hp->next;

hp->next=q;

hp=q;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1