📄 poly.c
字号:
#include"stdio.h"
#include"alloc.h"
typedef struct node
{ int c,e;/*定义系数,指数*/
struct node *next;
} polytype; /*定义结构体*/
polytype *create() /*建多项式表*/
{ polytype *p, *head=malloc(sizeof(polytype));/*定义两个指针*/
int c,e,n;
p=head;/*把头节点附给P*/
printf("please input the N:");
scanf("%d",&n);
printf("Warning the e's order must from min->max \n");
while(n)
{n--; /*n向下逐个运算*/
printf("please input c,e:");
scanf("%d%d",&c,&e);
p->next=malloc(sizeof(polytype)); /*申请一个空间付给p的下一个空间*/
p=p->next; /*把p的下一个项付给p*/
p->c=c;
p->e=e;
}
p->next=NULL;
return head; /*如果p的下一个项为空,则返回头指针*/
}
void printpoly(polytype *head) /*这个函数用来输出多项式*/
{ polytype *p=head->next; /*把头节点的下一个付给p*/
while(p)
{ printf("(%d,%d )",p->c,p->e);
p=p->next;
}
printf("\n");
}
void freepoly(polytype *head) /*此函数用来释放所占用的空间*/
{ polytype *p=head;
while(p)
{head=head->next; /*把头节点的下一个付给头节点*/
free(p);
p=head;
}
}
polytype *polyadd(polytype *ha,polytype *hb) /*多项式相加*/
{polytype *hc=malloc(sizeof(polytype)); /*为hc申请一个空间*/
polytype *pc=hc,*pa=ha->next,*pb=hb->next; /*定义多项式的三个数组*/
int e,c; /*定义系数,指数*/
while(pa||pb)
{if(pa&&(pb==NULL||pa->e<pb->e)) /*如果pb为空或是pa的系数小于pb的系数*/
{c=pa->c;
e=pa->e;
pa=pa->next;
}
else if(pb&&(pa==NULL||pb->e<pa->e))
{c=pb->c;
e=pb->e;
pb=pb->next;
}
else /*两链的当前结点指数相等*/
{c=pa->c+pb->c; /*把两个系数相加付给c*/
e=pa->e; /*把pa的指数付给e*/
pa=pa->next; /*把pa的下一项付给pa*/
pb=pb->next;/*把pb的下一项付给pb*/
}
if(c)
{pc->next=malloc(sizeof(polytype)); /*申请一个多项式的空间付给pc*/
pc=pc->next; /*pc下一项付给pc*/
pc->c=c; /*c付给pc的系数*/
pc->e=e;
}
}
pc->next=NULL;
return hc;
}
polytype *polysub(polytype *ha,polytype *hb) /*多项式相减*/
{polytype *hc=malloc(sizeof(polytype)); /*为hc申请一个空间*/
polytype *pc=hc,*pa=ha->next,*pb=hb->next; /*定义多项式的三个数组*/
int e,c; /*定义系数,指数*/
while(pa||pb)
{if(pa&&(pb==NULL||pa->e<pb->e)) /*如果pb为空或是pa的系数小于pb的系数*/
{c=pa->c;
e=pa->e;
pa=pa->next;
}
else if(pb&&(pa==NULL||pb->e<pa->e))
{c=-(pb->c);
e=pb->e;
pb=pb->next;
}
else /*两链的当前结点指数相等*/
{c=pa->c-pb->c; /*把两个系数相加付给c*/
e=pa->e; /*把pa的指数付给e*/
pa=pa->next; /*把pa的下一项付给pa*/
pb=pb->next;/*把pb的下一项付给pb*/
}
if(c)
{pc->next=malloc(sizeof(polytype)); /*申请一个多项式的空间付给pc*/
pc=pc->next; /*pc下一项付给pc*/
pc->c=c; /*c付给pc的系数*/
pc->e=e;
}
}
pc->next=NULL;
return hc;
}
polytype *onexmul(polytype *pa,polytype *pb) /*一个数与多项式相乘*/
{polytype *hc=malloc(sizeof(polytype)),*pc=hc;
pa=pa->next;
pb=pb->next;
while(pb)
{ pc->next=malloc(sizeof(polytype));
pc=pc->next;
pc->e=pa->e+pb->e;
pc->c=pa->c*pb->c;
pb=pb->next;
}
pc->next=NULL;
return hc;
}
polytype *mulxmul(polytype *ha,polytype *hb) /*多项式与多项式相乘*/
{polytype *temp,*hc=malloc(sizeof(polytype)),*pa=ha,*pb=hb;
hc->next=NULL;
while(pa)
{ temp=onexmul(pa,pb);
hc=polyadd(hc,temp);
freepoly(temp);
pa=pa->next; }
return hc;
}
void main ()
{polytype *ha,*hb,*add,*sub,*mul; /*定义ha,hb,add mul指针*/
ha=create(); /*建立多项式表*/
printpoly(ha); /*输出ha多项式项*/
hb=create(); /*建立多项式表*/
printpoly(hb);
add=polyadd(ha,hb); /*多项式相加*/
puts(" The add:");
printpoly(add);
sub=polysub(ha,hb); /*多项式相减*/
puts(" The sub:");
printpoly(sub);
puts("The multiple:");
mul=mulxmul(ha,hb); /*多项式相乘*/
printpoly(mul);
getch();
freepoly(ha);
freepoly(hb);
freepoly(add);
freepoly(mul); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -