数据结构课程设计一元稀疏多项式的计算.docx
《数据结构课程设计一元稀疏多项式的计算.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计一元稀疏多项式的计算.docx(14页珍藏版)》请在冰豆网上搜索。
数据结构课程设计一元稀疏多项式的计算
//头文件
#include
//定义多项式的项
typedefstructPolynomial{
floatcoef;
intexpn;
structPolynomial*next;
}*Polyn,Polynomial;
voidInsert(Polynp,Polynh){
if(p->coef==0)free(p);//系数为0的话释放结点
else
{
Polynq1,q2;
q1=h;
q2=h->next;
while(q2&&p->expnexpn)
{//查找插入位置
q1=q2;
q2=q2->next;
}
if(q2&&p->expn==q2->expn)
{//将指数相同相合并
q2->coef+=p->coef;
free(p);
if(!
q2->coef)
{//系数为0的话释放结点
q1->next=q2->next;
free(q2);
{//指数为新时将结点插入
p->next=q2;
q1->next=p;
PolynCreatePolyn(Polynhead,intm){
//建立一个头指针为head、项数为m的一元多项式
inti;
Polynp;
p=head=(Polyn)malloc(sizeof(structPolynomial));
head->next=NULL;
for(i=0;i{p=(Polyn)malloc(sizeof(structPolynomial));//建立新结点以接收数据printf("请输入第%d项的系数与指数:",i+1);scanf("%f%d",&p->coef,&p->expn);Insert(p,head);//调用Insert函数插入结点}returnhead;}voidDestroyPolyn(Polynp){//销毁多项式pPolynq1,q2;q1=p->next;q2=q1->next;while(q1->next){free(q1);q1=q2;q2=q2->next;}}voidPrintPolyn(PolynP){Polynq=P->next;intflag=1;//项数计数器if(!q){//若多项式为空,输出0putchar('0');printf("\n");return;}while(q){if(q->coef>0&&flag!=1)putchar('+');//系数大于0且不是第一项if(q->coef!=1&&q->coef!=-1){//系数非1或-1的普通情况printf("%g",q->coef);if(q->expn==1)putchar('X');elseif(q->expn)printf("X^%d",q->expn);}else{if(q->coef==1){if(!q->expn)putchar('1');elseif(q->expn==1)putchar('X');elseprintf("X^%d",q->expn);}if(q->coef==-1){if(!q->expn)printf("-1");elseif(q->expn==1)printf("-X");elseprintf("-X^%d",q->expn);}}q=q->next;flag++;}printf("\n");}intcompare(Polyna,Polynb){if(a&&b){if(!b||a->expn>b->expn)return1;elseif(!a||a->expnexpn)return-1;elsereturn0;}elseif(!a&&b)return-1;//a多项式已空,但b多项式非空elsereturn1;//b多项式已空,但a多项式非空}PolynAddPolyn(Polynpa,Polynpb){//求解并建立多项式a+b,返回其头指针Polynqa=pa->next;Polynqb=pb->next;Polynheadc,hc,qc;hc=(Polyn)malloc(sizeof(structPolynomial));//建立头结点hc->next=NULL;headc=hc;while(qa||qb){qc=(Polyn)malloc(sizeof(structPolynomial));switch(compare(qa,qb)){case1:{qc->coef=qa->coef;qc->expn=qa->expn;qa=qa->next;break;}case0:{qc->coef=qa->coef+qb->coef;qc->expn=qa->expn;qa=qa->next;qb=qb->next;break;}case-1:{qc->coef=qb->coef;qc->expn=qb->expn;qb=qb->next;break;}}if(qc->coef!=0){qc->next=hc->next;hc->next=qc;hc=qc;}elsefree(qc);//当相加系数为0时,释放该结点}returnheadc;}PolynSubtractPolyn(Polynpa,Polynpb){//求解并建立多项式a-b,返回其头指针Polynh=pb;Polynp=pb->next;Polynpd;while(p){//将pb的系数取反p->coef*=-1;p=p->next;}pd=AddPolyn(pa,h);for(p=h->next;p;p=p->next)//恢复pb的系数p->coef*=-1;returnpd;}intValuePolyn(Polynhead,intx){//输入x值,计算并返回多项式的值Polynp;inti;intsum=0,t;for(p=head->next;p;p=p->next){t=1;for(i=p->expn;i!=0;){if(i<0){t/=x;i++;}//指数小于0,进行除法else{t*=x;i--;}//指数大于0,进行乘法}sum+=p->coef*t;}returnsum;}PolynDerivative(Polynhead){//求解并建立导函数多项式,并返回其头指针Polynq=head->next,p1,p2,hd;hd=p1=(Polyn)malloc(sizeof(structPolynomial));//建立头结点hd->next=NULL;while(q){if(q->expn!=0){//该项不是常数项时p2=(Polyn)malloc(sizeof(structPolynomial));p2->coef=q->coef*q->expn;p2->expn=q->expn-1;p2->next=p1->next;//连接结点p1->next=p2;p1=p2;}q=q->next;}returnhd;}PolynMultiplyPolyn(Polynpa,Polynpb){//求解并建立多项式a*b,返回其头指针Polynhf,pf;Polynqa=pa->next;Polynqb=pb->next;hf=(Polyn)malloc(sizeof(structPolynomial));//建立头结点hf->next=NULL;for(;qa;qa=qa->next){for(qb=pb->next;qb;qb=qb->next){pf=(Polyn)malloc(sizeof(structPolynomial));pf->coef=qa->coef*qb->coef;pf->expn=qa->expn+qb->expn;Insert(pf,hf);//调用Insert函数以合并指数相同的项}}returnhf;}voidmain(){intm,n,a,x;charflag;Polynpa=0,pb=0,pc;printf("欢迎使用多项式操作程序\n\n");printf("请输入a的项数:");scanf("%d",&m);pa=CreatePolyn(pa,m);//建立多项式aprintf("请输入b的项数:");scanf("%d",&n);pb=CreatePolyn(pb,n);//建立多项式b//输出菜单printf("*******************************************************\n");printf("*多项式操作程序*\n");printf("**\n");printf("*A:输出多项式B:输出多项式b*\n");printf("**\n");printf("*C:输出a的导数D:输出b的导数*\n");printf("**\n");printf("*E:代入x的值计算aF:代入x的值计算b*\n");printf("**\n");printf("*G:输出a+bH:输出a-b*\n");printf("**\n");printf("*I:输出a*bJ:退出程序*\n");printf("**\n");printf("*******************************************************\n");while(a){printf("\n请选择操作:");scanf("%c",&flag);//空格符号一定要注意switch(flag){case'A':case'a':{printf("\n多项式a=");PrintPolyn(pa);break;}case'B':case'b':{printf("\n多项式b=");PrintPolyn(pb);break;}case'C':case'c':{pc=Derivative(pa);printf("\n多项式a的导函数为:a'=");PrintPolyn(pc);break;}case'D':case'd':{pc=Derivative(pb);printf("\n多项式b的导函数为:b'=");PrintPolyn(pc);break;}case'E':case'e':{printf("输入x的值:x=");scanf("%d",&x);printf("\nx=%d时,a=%d\n",x,ValuePolyn(pa,x));break;}case'F':case'f':{printf("输入x的值:x=");scanf("%d",&x);printf("\nx=%d时,b=%d\n",x,ValuePolyn(pb,x));break;}case'G':case'g':{pc=AddPolyn(pa,pb);printf("\na+b=");PrintPolyn(pc);break;}case'H':case'h':{pc=SubtractPolyn(pa,pb);printf("\na-b=");PrintPolyn(pc);break;}case'I':case'i':{pc=MultiplyPolyn(pa,pb);printf("\na*b=");PrintPolyn(pc);break;}case'J':case'j':{printf("\n感谢使用此程序!\n");DestroyPolyn(pa);DestroyPolyn(pb);a=0;break;}default:printf("\n您的选择错误,请重新选择!\n");}}}
p=(Polyn)malloc(sizeof(structPolynomial));//建立新结点以接收数据
printf("请输入第%d项的系数与指数:
",i+1);
scanf("%f%d",&p->coef,&p->expn);
Insert(p,head);//调用Insert函数插入结点
returnhead;
voidDestroyPolyn(Polynp){
//销毁多项式p
q1=p->next;
q2=q1->next;
while(q1->next)
free(q1);
voidPrintPolyn(PolynP){
Polynq=P->next;
intflag=1;//项数计数器
q)
{//若多项式为空,输出0
putchar('0');
printf("\n");
return;
while(q)
if(q->coef>0&&flag!
=1)putchar('+');//系数大于0且不是第一项
if(q->coef!
=1&&q->coef!
=-1)
{//系数非1或-1的普通情况
printf("%g",q->coef);
if(q->expn==1)putchar('X');
elseif(q->expn)printf("X^%d",q->expn);
if(q->coef==1)
q->expn)putchar('1');
elseif(q->expn==1)putchar('X');
elseprintf("X^%d",q->expn);
if(q->coef==-1)
q->expn)printf("-1");
elseif(q->expn==1)printf("-X");
elseprintf("-X^%d",q->expn);
q=q->next;
flag++;
intcompare(Polyna,Polynb){
if(a&&b)
b||a->expn>b->expn)return1;
elseif(!
a||a->expnexpn)return-1;
elsereturn0;
a&&b)return-1;//a多项式已空,但b多项式非空
elsereturn1;//b多项式已空,但a多项式非空
PolynAddPolyn(Polynpa,Polynpb){//求解并建立多项式a+b,返回其头指针
Polynqa=pa->next;
Polynqb=pb->next;
Polynheadc,hc,qc;
hc=(Polyn)malloc(sizeof(structPolynomial));//建立头结点
hc->next=NULL;
headc=hc;
while(qa||qb)
qc=(Polyn)malloc(sizeof(structPolynomial));
switch(compare(qa,qb)){
case1:
qc->coef=qa->coef;
qc->expn=qa->expn;
qa=qa->next;
break;
case0:
qc->coef=qa->coef+qb->coef;
qb=qb->next;
case-1:
qc->coef=qb->coef;
qc->expn=qb->expn;
if(qc->coef!
=0)
qc->next=hc->next;
hc->next=qc;
hc=qc;
elsefree(qc);//当相加系数为0时,释放该结点
returnheadc;
PolynSubtractPolyn(Polynpa,Polynpb){//求解并建立多项式a-b,返回其头指针
Polynh=pb;
Polynp=pb->next;
Polynpd;
while(p)
{//将pb的系数取反
p->coef*=-1;
p=p->next;
pd=AddPolyn(pa,h);
for(p=h->next;p;p=p->next)//恢复pb的系数
returnpd;
intValuePolyn(Polynhead,intx){
//输入x值,计算并返回多项式的值
intsum=0,t;
for(p=head->next;p;p=p->next)
t=1;
for(i=p->expn;i!
=0;)
if(i<0){t/=x;i++;}//指数小于0,进行除法
else{t*=x;i--;}//指数大于0,进行乘法
sum+=p->coef*t;
returnsum;
PolynDerivative(Polynhead){
//求解并建立导函数多项式,并返回其头指针
Polynq=head->next,p1,p2,hd;
hd=p1=(Polyn)malloc(sizeof(structPolynomial));//建立头结点
hd->next=NULL;
if(q->expn!
{//该项不是常数项时
p2=(Polyn)malloc(sizeof(structPolynomial));
p2->coef=q->coef*q->expn;
p2->expn=q->expn-1;
p2->next=p1->next;//连接结点
p1->next=p2;
p1=p2;
returnhd;
PolynMultiplyPolyn(Polynpa,Polynpb){
//求解并建立多项式a*b,返回其头指针
Polynhf,pf;
hf=(Polyn)malloc(sizeof(structPolynomial));//建立头结点
hf->next=NULL;
for(;qa;qa=qa->next)
for(qb=pb->next;qb;qb=qb->next)
pf=(Polyn)malloc(sizeof(structPolynomial));
pf->coef=qa->coef*qb->coef;
pf->expn=qa->expn+qb->expn;
Insert(pf,hf);//调用Insert函数以合并指数相同的项
returnhf;
voidmain()
intm,n,a,x;
charflag;
Polynpa=0,pb=0,pc;
printf("欢迎使用多项式操作程序\n\n");
printf("请输入a的项数:
");
scanf("%d",&m);
pa=CreatePolyn(pa,m);//建立多项式a
printf("请输入b的项数:
scanf("%d",&n);
pb=CreatePolyn(pb,n);//建立多项式b
//输出菜单
printf("*******************************************************\n");
printf("*多项式操作程序*\n");
printf("**\n");
printf("*A:
输出多项式B:
输出多项式b*\n");
printf("*C:
输出a的导数D:
输出b的导数*\n");
printf("*E:
代入x的值计算aF:
代入x的值计算b*\n");
printf("*G:
输出a+bH:
输出a-b*\n");
printf("*I:
输出a*bJ:
退出程序*\n");
while(a)
printf("\n请选择操作:
scanf("%c",&flag);//空格符号一定要注意
switch(flag)
case'A':
case'a':
printf("\n多项式a=");
PrintPolyn(pa);
case'B':
case'b':
printf("\n多项式b=");
PrintPolyn(pb);
case'C':
case'c':
pc=Derivative(pa);
printf("\n多项式a的导函数为:
a'=");
PrintPolyn(pc);
case'D':
case'd':
pc=Derivative(pb);
printf("\n多项式b的导函数为:
b'=");
case'E':
case'e':
printf("输入x的值:
x=");
scanf("%d",&x);
printf("\nx=%d时,a=%d\n",x,ValuePolyn(pa,x));
case'F':
case'f':
printf("\nx=%d时,b=%d\n",x,ValuePolyn(pb,x));
case'G':
case'g':
pc=AddPolyn(pa,pb);
printf("\na+b=");
case'H':
case'h':
pc=SubtractPolyn(pa,pb);
printf("\na-b=");
case'I':
case'i':
pc=MultiplyPolyn(pa,pb);
printf("\na*b=");
case'J':
case'j':
printf("\n感谢使用此程序!
\n");
DestroyPolyn(pa);
DestroyPolyn(pb);
a=0;
default:
printf("\n您的选择错误,请重新选择!
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1