2-1 Add Two Polynomials (20 分)

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;
}

上一篇:javascript获取当前时间


下一篇:皮尔森相关系数(Pearson correlation coefficient)