#include <stdio.h>
#include <stdlib.h>
#define NEW (node*)malloc(sizeof(node))
typedef struct Node
{
float conf;
int exp;
struct Node*next;
} node;
node a[20],b[20],temp;
nodecreatah()//创建a链表
{
float n;
int m;
nodeah=NEW;
nodepa=ah;
nodeqa=NEW;
pa->next=qa;
printf(“Input first conf\n”);
scanf("%f",&qa->conf);
while(qa->conf!=0)
{
printf(“Input first exp\n”);
scanf("%d",&qa->exp);
pa=qa;
qa=NEW;
pa->next=qa;
printf(“Input first conf\n”);
scanf("%f",&qa->conf);
}
pa->next=NULL;
return ah;
}
nodecreatbh()//创建b链表
{
float n;
int m;
nodebh=NEW;
nodepb=bh;
nodeqb=NEW;
pb->next=qb;
printf(“Input second conf\n”);
scanf("%f",&qb->conf);
while(qb->conf!=0)
{
printf(“Input second exp\n”);
scanf("%d",&qb->exp);
pb=qb;
qb=NEW;
pb->next=qb;
printf(“Input second conf\n”);
scanf("%f",&qb->conf);
}
pb->next=NULL;
return bh;
}
nodesortah(nodeah)
{
nodepaa;
int i=0,j,count=0;
nodepa=ah->next;
while(pa!=NULL)
{
a[i].conf=pa->conf;
a[i].exp=pa->exp;
i++;
count++;
pa=pa->next;
}
for(i=1; i<count; i++)
{
for(j=0; j<count-i; j++)
{
if(a[j].exp<a[j+1].exp)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
pa=ah;
paa=NEW;
pa->next=paa;
for(i=0; i<count; i++)
{
paa->conf=a[i].conf;
paa->exp=a[i].exp;
pa=paa;;
paa=NEW;
pa->next=paa;
}
pa->next=NULL;
return ah;
}
nodesortbh(nodebh)
{
nodepbb;
int i=0,j,count=0;
nodepb=bh->next;
while(pb!=NULL)
{
b[i].conf=pb->conf;
b[i].exp=pb->exp;
i++;
count++;
pb=pb->next;
}
for(i=1; i<count; i++)
{
for(j=0; j<count-i; j++)
{
if(b[j].exp<b[j+1].exp)
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
pb=bh;
pbb=NEW;
pb->next=pbb;
for(i=0; i<count; i++)
{
pbb->conf=b[i].conf;
pbb->exp=b[i].exp;
pb=pbb;
pbb=NEW;
pb->next=pbb;
}
pb->next=NULL;
return bh;
}
nodeadd(nodeah,nodebh)
{
float c;
nodech=NEW;
node*pa,*pb,*pc,*s;
pa=ah->next;
pb=bh->next;
pc=ch;
while(pa!=NULL&&pb!=NULL)
{
if(pa->exp>pb->exp)
{
s=NEW;
s->conf=pa->conf;
s->exp=pa->exp;
pc->next=s;
pc=s;
pa=pa->next;
}
else if(pa->exp<pb->exp)
{
s=NEW;
s->conf=pb->conf;
s->exp=pb->exp;
pc->next=s;
pc=s;
pb=pb->next;
}
else
{
c=pa->conf+pb->conf;
if(c!=0)
{
s=NEW;
s->conf=c;
s->exp=pa->exp;
pc->next=s;
pc=s;
}
pa=pa->next;
pb=pb->next;
}
}
if(pb!=NULL)
pa=pb;
while(pa!=NULL)
{
s=NEW;
s->conf=pa->conf;
s->exp=pa->exp;
pc->next=s;
pc=s;
pa=pa->next;
}
pc->next=NULL;
return ch;
}
nodecut(nodeah,nodebh)
{
float c;
nodech=NEW;
node*pa,*pb,*pc,*s;
pa=ah->next;
pb=bh->next;
pc=ch;
while(pa!=NULL&&pb!=NULL)
{
if(pa->exp>pb->exp)
{
s=NEW;
s->conf=pa->conf;
s->exp=pa->exp;
pc->next=s;
pc=s;
pa=pa->next;
}
else if(pa->exp<pb->exp)
{
s=NEW;
s->conf=pb->conf;
s->exp=pb->exp;
pc->next=s;
pc=s;
pb=pb->next;
}
else
{
c=pa->conf-pb->conf;
if(c!=0)
{
s=NEW;
s->conf=c;
s->exp=pa->exp;
pc->next=s;
pc=s;
}
pa=pa->next;
pb=pb->next;
}
}
if(pb!=NULL)
pa=pb;
while(pa!=NULL)
{
s=NEW;
s->conf=pa->conf;
s->exp=pa->exp;
pc->next=s;
pc=s;
pa=pa->next;
}
pc->next=NULL;
return ch;
}
void output(nodehead)
{
nodep=head->next;
while(p!=NULL)
{
printf("+%.1lfx^%d",p->conf,p->exp);
p=p->next;
}
printf("\n");
}
int main()
{
int m;
node*heada=NULL,*headaa,head;
nodeheadb=NULL,headbb;
nodep,*q;
printf(“请对第一个多项式进行赋值,当系数为零时结束赋值\n”);
heada=creatah();
printf(“请对第二个多项式进行赋值,当系数为零时结束赋值\n”);
headb=creatbh();
printf(“对第一个多项式进行降幂排序\n”);
headaa=sortah(heada);
printf(“对第二个多项式进行降幂排序\n”);
headbb=sortbh(headb);
printf(“请输入您的选择1.相加2.相减\n”);
scanf("%d",&m);
if(m==1)
head=add(headaa,headbb);
else
head=cut(headaa,headbb);
printf(“输出两多项式之和\n”);
output(head);
return 0;
}
两多项式的加减