两个多项式加减(用数组进行降幂排序)

#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;
node
ah=NEW;
nodepa=ah;
node
qa=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;
node
bh=NEW;
nodepb=bh;
node
qb=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;
node
pa=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;
node
pb=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;
node
ch=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;
node
ch=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)
{
node
p=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;
node
headb=NULL,headbb;
node
p,*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;
}

两多项式的加减

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZengqiangChen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值