#include<stdio.h>
#include<malloc.h>
typedef struct poly{ float coef;
int exp;
struct poly * next;}polytype;
polytype * createlist (polytype *llist,float n) /*创建链表*/
{ struct poly *p,*t;
int i;
p=llist;
if(p!=NULL) {p->coef=n;
p->exp=-1;}
p=(polytype *) malloc(sizeof(struct poly));/*申请第一个节点空间*/
for(i=0;i<n;i++) /*逐个申请后面节点的空间*/
{ t=(polytype *) malloc(sizeof(struct poly));
scanf("%f,%d\n",&p->coef,&p->exp);
p->next=t;
t->next=NULL;
p=p->next;
}
return(llist);
}
int printf_list(polytype *llist,float n) /*排序算法*/
{ int i,t,q,s; /*q,s用于交换变量值时充当临时变量*/
float r;
struct poly *p;
llist= createlist(llist, n);
p=llist;
t=p->exp;r=p->coef;
p=p->next;
while(p)
{ if( t<p->exp ) { q=t;t=p->exp;p->exp=q; /*把指数按从大到小排列*/
s=r;r=p->coef;p->coef=s; /*系数跟原指数匹配*/
p=p->next;
} }
p=llist; /*把指针l赋为头指针*/
printf("%f",*p->next);
p=p->next;
for(i=0;i<n;i++)
{ printf("%f%d",p->coef,p->exp); p=p->next;}
return(1);
}
polytype *add_list(polytype *pa, polytype *pb)
{ struct poly *p,*q,*pre,*pc,*u;
int x=0,sel,n;
p=pa->next;
q=pb->next;
pre=pa;
pc=pa;
while(p&&q)
{
if ((p->exp)<(q->exp))
sel=1;
else if ((p->exp)==(q->exp)) sel=2;
else sel=3;
switch(sel)
{case 1: {pre=p;p=p->next;break;}
case 2: {x=p->coef+q->coef;
if (x!=0) {p->coef=x;pre=p;}
{pre->next=p->next;
p=pre->next;
u=q;
q=q->next;
free(u);
}
break;
}
case 3: {u=q->next;pre->next=q;q->next=p;pre=q;q=u;break;}
}
}
if (q!=NULL) pre->next=q; free(pb);
p=pc->next;
printf("now these %drecords are:\n",n);
while (p!=NULL)
{printf("%d,%d\n",p->coef,p->exp);
p=p->next;}
return(pc);
}
main()
{ struct poly *pa,*pb ;
float n1,n2;
printf("请输入两多项式的长度(以浮点型输入):\n");
scanf("%f,%f",&n1,&n2);
pa=(polytype *) malloc(sizeof(struct poly));
pb=(polytype *) malloc(sizeof(struct poly));
printf("请逐项输入系数和指数:\n");
pa=createlist (pa,n1);
printf("继续输入另一个多项式的系数和指数:\n");
pb= createlist (pb,n2);
printf("输出第一个降序序列");
printf_list(pa,n1);
printf("输出第二个降序序列");
printf_list(pb, n2);
add_list(pa,pb);
}
有谁帮我看看这个程序,我拿它没辙了,能运行但实现不了目的!!
实验目的是建立并输出多项式,输出多项式,n是多项式的个数,ciei是第i项的系数和指数,序列按降序排列
再把两个多项式相加输出结果!!