ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2-1 Add Two Polynomials (20 分)

2021-11-27 15:58:11  阅读:202  来源: 互联网

标签:Coefficient p3 p1 20 Exponent Two Next Add Polynomial


2-1 Add Two Polynomials (20 分)
Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node.

Format of functions:
Polynomial Add( Polynomial a, Polynomial b );
where Polynomial is defined as the following:

typedef struct Node PtrToNode;
struct Node {
int Coefficient;
int Exponent;
PtrToNode Next;
};
typedef PtrToNode Polynomial;
/
Nodes are sorted in decreasing order of exponents.*/
The function Add is supposed to return a polynomial which is the sum of a and b.

Sample program of judge:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node *PtrToNode;
struct Node {
int Coefficient;
int Exponent;
PtrToNode Next;
};
typedef PtrToNode Polynomial;

Polynomial Read(); /* details omitted /
void Print( Polynomial p ); /
details omitted */
Polynomial Add( Polynomial a, Polynomial b );

int main()
{
Polynomial a, b, s;
a = Read();
b = Read();
s = Add(a, b);
Print(s);
return 0;
}

/* Your function will be put here */
Sample Input:
4
3 4 -5 2 6 1 -2 0
3
5 20 -7 4 3 1
结尾无空行
Sample Output:
5 20 -4 4 -5 2 9 1 -2 0
这道题的意思其实就是给你一个多项式,这个多项式的输入一定要按照顺序输入,也就是按照题目的形式指数递减,先输系数,再输指数,最后两两合并

#include<stdio.h>
#include<stdlib.h>
typedef struct Node *PtrToNode;
struct Node{
int Coefficient;
int Exponent;
PtrToNode Next;
};
typedef PtrToNode Polynomial;
Polynomial Read();
Polynomial Read()
{
    Polynomial head;
    Polynomial s,r;
    int n,c,e;
    head=(Polynomial)malloc(sizeof(struct Node));
    head ->Next=NULL;
    scanf("%d",&n);
    r=head;
    while(n--)
    {
    scanf("%d %d",&c,&e);
    s=(Polynomial) malloc(sizeof(struct Node));
        s->Coefficient=c;
        s->Exponent=e;
        r->Next=s;
        r=s;
    }
    r->Next=NULL;
    return head;
}
void Print(Polynomial p);
void Print(Polynomial p)
{
    if(p==NULL)
    {
        printf("NULL");
        return ;
    }
    Polynomial t;
    t=p->Next;
    while(t!=NULL)
    {
        if(t->Coefficient>0)
            printf("%d %d ",t->Coefficient,t->Exponent);
        else
            printf("%d %d ",t->Coefficient,t->Exponent);
        t=t->Next;
    }
}
Polynomial Add(Polynomial a,Polynomial b);
Polynomial Add(Polynomial a,Polynomial b)
{
    Polynomial c=(Polynomial)malloc(sizeof(struct Node));
    c->Next=NULL;
    Polynomial p1=a->Next;
    Polynomial p2=b->Next;
    Polynomial p3;
    Polynomial rearC=c;
    while(p1!=NULL&&p2!=NULL)
    {
        if(p1->Exponent>p2->Exponent)
        {   p3=(Polynomial)malloc(sizeof(struct Node));
            //p3->Next=NULL;
            p3->Coefficient=p1->Coefficient;
            p3->Exponent=p1->Exponent;
            rearC->Next=p3;
            rearC=p3;
            p1=p1->Next;
        }
        else if(p1->Exponent<p2->Exponent)
        {
            p3=(Polynomial)malloc(sizeof(struct Node));
            //p3->Next=NULL;
            p3->Coefficient=p2->Coefficient;
            p3->Exponent=p2->Exponent;
            rearC->Next=p3;
            rearC=p3;
            p2=p2->Next;
        }
        else
        {
            if((p1->Coefficient+p2->Coefficient)!=0)
            {
                 p3=(Polynomial)malloc(sizeof(struct Node));
           // p3->Next=NULL;
            p3->Coefficient=p1->Coefficient+p2->Coefficient;
            p3->Exponent=p2->Exponent;
            rearC->Next=p3;
            rearC=p3;
            }
            p1=p1->Next;
            p2=p2->Next;
        }
    }
 while(p1!=NULL)
        {
            p3=(Polynomial)malloc(sizeof(struct Node));
            //p3->Next=NULL;
            p3->Coefficient=p1->Coefficient;
            p3->Exponent=p1->Exponent;
            rearC->Next=p3;
            rearC=p3;
            p1=p1->Next;
        }

        while(p2)
        {
            p3=(Polynomial)malloc(sizeof(struct Node));
            rearC->Next=p3;
           // p3->Next=NULL;
            p3->Coefficient=p2->Coefficient;
            p3->Exponent=p2->Exponent;
            rearC=p3;
            p2=p2->Next;
        }
        rearC->Next=NULL;
        return c;
}
int main()
{
    Polynomial a,b,s;
    a=Read();
    b=Read();
    s=Add(a,b);
    Print(s);
    return 0;
}

标签:Coefficient,p3,p1,20,Exponent,Two,Next,Add,Polynomial
来源: https://blog.csdn.net/muzir_/article/details/121578596

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有