c 编写Add Two Polynomials

题目:

 Add Two Polynomials   

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
#include
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
#include
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 */
void Print( Polynomial p )/*打印多项式*/
{
p=p->Next;
if(p==NULL){
printf("0");}
else{
while(p!=NULL){
printf("%d ",p->Coefficient);
printf("%d ",p->Exponent);
p=p->Next;}}
}
Polynomial Read()/*读入多项式创建链表*/
{
int digits;
Polynomial m,n,dummy;
n=(Polynomial)malloc(sizeof(struct Node));
dummy=n;
n->Next=NULL;
n->Coefficient=0;
n->Exponent=0;
scanf("%d",&digits);
for(;digits>0;digits--){
m=(Polynomial)malloc(sizeof(struct Node));
scanf("%d",&m->Coefficient);
scanf("%d",&m->Exponent);
m->Next=NULL;
n->Next=m;
n=m;}
return dummy;
}
Polynomial Add( Polynomial a, Polynomial b )/*实现多项式相加*/
{
Polynomial sum,e,d,f,w,g,suma,h,result,j;
int p;
sum=(Polynomial)malloc(sizeof(struct Node));
sum->Coefficient=0;
sum->Exponent=0;
sum->Next=NULL;
w=sum;
e=a;
d=a;
if(a->Next==NULL){
return b;}/*如果a是0,直接返回b*/
else if(b->Next==NULL){
return a;}/*如果b是0,直接返回a*/
else{
a=a->Next;
b=b->Next;
while(a->Next!=NULL)
a=a->Next;
a->Next=b;/*将b接到a末尾*/
for(d=d->Next;d!=NULL;d=d->Next){
for(f=d->Next;f!=NULL;f=f->Next)/*对多项式经行降幂排序*/
{
if(d->ExponentExponent){
p=d->Exponent;
d->Exponent=f->Exponent;
f->Exponent=p;
p=d->Coefficient;
d->Coefficient=f->Coefficient;
f->Coefficient=p;}}}
/*合并同类项*/
for(e=e->Next;e!=NULL;e=e->Next){
for(g=e->Next;g!=NULL;g=g->Next){
if(e->Exponent==g->Exponent){

e->Coefficient=e->Coefficient+g->Coefficient;

e->Next=g->Next;

}
}
suma=(Polynomial)malloc(sizeof(struct Node));

suma->Exponent=e->Exponent;
suma->Coefficient=e->Coefficient;
suma->Next=NULL;

sum->Next=suma;
sum=suma;}
}
h=(Polynomial)malloc(sizeof(struct Node));/*新建一个链表去除为0的项*/
h->Coefficient=0;
h->Exponent=0;
h->Next=NULL;
result=h;
for(w=w->Next;w!=NULL;w=w->Next){
if(w->Coefficient!=0){
j=(Polynomial)malloc(sizeof(struct Node));
j->Coefficient=w->Coefficient;
j->Exponent=w->Exponent;
j->Next=NULL;
h->Next=j;
h=j;}}
return result;
}





























你可能感兴趣的:(ds)