数据结构实验——一元多项式相加减

本文介绍了如何使用链表数据结构实现多项式的加减运算,并详细阐述了创建、合并和排序链表的过程。通过实例代码学习链表操作和多项式处理技巧,适合巩固基础算法知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于多项式的加减肯定会用到链表,所以本题运用链表来实现,其中会用到链表的插入,删除以及一些细节的地方,同时为了处理好一些其他的输入问题,或者是不是按照顺序的输入,加入了一些自己的理解,关于链表的知识点,可以通过此题多多学习,加上此代码非原创,但通过这个代码学到了许多,注意复习*的用法以及含义加上链表初级知识的复习。

#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>   //考虑全局静态变量 

#define LEN sizeof(struct poly)
static struct poly *head1,*head2; 
struct poly
{
    int index;
    float ratio;
    struct poly *next;
};//建立链表,index储存指数,ratio储存系数
struct poly *creat()
{
    struct poly *temp,*head,*rear,*t;
    int index;
    float ratio;
    int flag=0;
    head=rear=(struct poly*)malloc(sizeof(LEN));
    rear->next=NULL;
    printf("请输入多项式的各项的系数与指数,以空格隔开,输入0 0代表输入结束\n");
    while(1)
    {

        scanf("%f %d",&ratio,&index);
        if(ratio==0&&index==0)
            return head;
        if(head->next==NULL)
        {
            temp=(struct poly*)malloc(sizeof(LEN));
            rear->next=temp;
            rear=temp;
            rear->next=NULL;
            rear->index=index;
            rear->ratio=ratio;
            continue;
        }
		for(t=head->next; t!=NULL; t=t->next)
        {
            if(t->index==index)
            {
                flag=1;
                t->ratio=t->ratio+ratio;
                break;
            }
        }
        if(flag==0)
        {
            temp=(struct poly*)malloc(sizeof(LEN));
            rear->next=temp;
            rear=temp;
            rear->next=NULL;
            rear->index=index;
            rear->ratio=ratio;

        }

    }
    return head;
};//输入数据,在输入时,可能存在输入两次相同系数的数据,此时将两组数据合并
struct poly sort(struct poly *head)
{
    struct poly *temp1,*temp2;
    temp1=head->next;
    temp2=NULL;
    while(temp1!=temp2)
    {
        while(temp1->next!=temp2)
        {
            if(temp1->index>temp1->next->index)
            {
                temp1->index=temp1->index+temp1->next->index;
                temp1->next->index=temp1->index-temp1->next->index;
                temp1->index=temp1->index-temp1->next->index;
                temp1->ratio=temp1->ratio+temp1->next->ratio;
                temp1->next->ratio=temp1->ratio-temp1->next->ratio;
                temp1->ratio=temp1->ratio-temp1->next->ratio;

            }
            temp1=temp1->next;
        }
        temp2=temp1;
        temp1=head->next;
    }
};//将多项式进行排序,以符合我们通常状态下升幂的次序
struct poly add(struct poly *head1,struct poly *head2)
{   
    struct poly *head,*rear,*temp1,*p,*q;
    head=rear=(struct poly*)malloc(sizeof(LEN));
    rear->next=NULL;
    p=head1->next;
    q=head2->next;
    while(p!=NULL||q!=NULL)
    {


        if(p==NULL||q==NULL)
        {
            if(p==NULL)
            {
                while(q!=NULL)
                {
                    temp1=(struct poly*)malloc(sizeof(LEN));
                    rear->next=temp1;
                    temp1->next=NULL;
                    temp1->index=q->index;
                    temp1->ratio=q->ratio;
                    rear=temp1;
                    q=q->next;
                }
            }//求和时,若head1为空,则直接将head2复制到新结果中去
            else if(q==NULL)
            {
                while(p!=NULL)
                {
                    temp1=(struct poly*)malloc(sizeof(LEN));
                    rear->next=temp1;
                    temp1->next=NULL;
                    temp1->index=p->index;
                    temp1->ratio=p->ratio;
                    rear=temp1;
                    p=p->next;
                }
            }//若head2为空,则将head1复制到结果中去
        }
        else
        {
            if(p->index<q->index)
            {
                temp1=(struct poly*)malloc(sizeof(LEN));
                temp1->index=p->index;
                temp1->ratio=p->ratio;
                rear->next=temp1;
                rear=temp1;
                rear->next=NULL;
                p=p->next;
            }
            else if(p->index>q->index)
            {
                temp1=(struct poly*)malloc(sizeof(LEN));
                temp1->index=q->index;
                temp1->ratio=q->ratio;
                rear->next=temp1;
                rear=temp1;
                rear->next=NULL;
                q=q->next;
            }
            else
            {
                temp1=(struct poly*)malloc(sizeof(LEN));
                temp1->index=p->index;
                temp1->ratio=q->ratio+p->ratio;
                if(temp1->ratio!=0)
                {
                    rear->next=temp1;
                    rear=temp1;
                    rear->next=NULL;
                    p=p->next;
                    q=q->next;
                }
                else
                {
                    p=p->next;
                    q=q->next;
                }
            }

        }//若head1和head2都不为空,则按照升幂的次序将其排序相加,系数为0的项不需要,注意,一定要按照次序来,少使用排序函数
    } 
   void print(struct poly *head);
   print(head);
  

   
};//对两多项式进行求和,求和结果储存在另一个结构体中
struct poly sub(struct poly *head1,struct poly *head2)
{
    struct poly *head,*rear,*temp1,*temp2,*p,*q;
    head=rear=(struct poly*)malloc(sizeof(LEN));
    head->next=rear->next=NULL;
    p=head1->next;
    q=head2->next;

    while(p!=NULL||q!=NULL)
    {
        if(p==NULL||q==NULL)
        {
            if(p==NULL)
            {
                while(q!=NULL)
                {
                    temp1=(struct poly*)malloc(sizeof(LEN));
                    rear->next=temp1;
                    temp1->next=NULL;
                    temp1->index=q->index;
                    temp1->ratio=-q->ratio;
                    rear=temp1;
                    q=q->next;
                }
            }
            else if(q==NULL)
            {
                while(p!=NULL)
                {
                    temp1=(struct poly*)malloc(sizeof(LEN));
                    rear->next=temp1;
                    temp1->next=NULL;
                    temp1->index=p->index;
                    temp1->ratio=p->ratio;
                    rear=temp1;
                    p=p->next;
                }
            }
        }
        else
        {
            if(p->index<q->index)
            {
                temp1=(struct poly*)malloc(sizeof(LEN));
                temp1->index=p->index;
                temp1->ratio=p->ratio;
                rear->next=temp1;
                rear=temp1;
                rear->next=NULL;
                p=p->next;
            }
            else if(p->index>q->index)
            {
                temp1=(struct poly*)malloc(sizeof(LEN));
                temp1->index=q->index;
                temp1->ratio=-q->ratio;
                rear->next=temp1;
                rear=temp1;
                rear->next=NULL;
                q=q->next;
            }
            else
            {
                temp1=(struct poly*)malloc(sizeof(LEN));
                temp1->index=p->index;
                temp1->ratio=p->ratio-q->ratio;
                if(temp1->ratio!=0)
                {
                    rear->next=temp1;
                    rear=temp1;
                    rear->next=NULL;
                    p=p->next;
                    q=q->next;
                }
                else
                {
                    p=p->next;
                    q=q->next;
                }
            }

        }
        
        
    }
  
   void print(struct poly *head);
   print(head);
   
   
};//多项式求差,与求和同理


void print(struct poly *head)
{   FILE * fp;
	fp=fopen("test18.txt","a+");
    struct poly *temp;
    temp=head->next;
    if(temp==NULL)
    {
        fprintf(fp,"0\n");
        return;
    }//多项式的输出,若多项式为NULL则直接输出0
    while(temp!=NULL)
    {
        if(temp==head->next)
        {
            fprintf(fp," %.2f",temp->ratio);
            if(temp->index!=0)
            {
                if(temp->index==1)
                    fprintf(fp,"x");
                else
                    fprintf(fp,"x^%d",temp->index);
            }
            else
                fprintf(fp," ");
        }//在输出第一项时,系数为正,直接输出,系数为负,也直接输出
        else
        {
            if(temp->ratio>0)
            {
                fprintf(fp,"+%.2f",temp->ratio);
                if(temp->index!=0)
                {
                    if(temp->index==1)
                        fprintf(fp,"x");
                    else
                        fprintf(fp,"x^%d",temp->index);
                }

                else
                    fprintf(fp," ");
            }
            else
            {
                fprintf(fp,"%.2f",temp->ratio);
                if(temp->index!=0)
                {
                    if(temp->index==1)
                        fprintf(fp,"x");
                    else
                        fprintf(fp,"x^%d",temp->index);
                }

                else
                    fprintf(fp," ");
            }//之后的每一项中,系数为正,则加上正号,即加号,系数为负,可直接输出
        }
        temp=temp->next;
    }
    fprintf(fp,"\n");
fclose(fp);
};

int main()
{
	int a,b;
    head1=creat();
    head2=creat();
    sort(head1);
    sort(head2);
    printf("\n多项式a(x)为:\n");
    print(head1);
    printf("\n多项式b(x)为:\n");
    print(head2);
    printf("\n两多项式之和为:\n");
    add(head1,head2);
    printf("\n两多项式之差为:\n");
    sub(head1,head2);
   
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值