1. 程式人生 > >一元多項式的乘法和加法

一元多項式的乘法和加法

02-線性結構2 一元多項式的乘法與加法運算(20 分)

設計函式分別求兩個一元多項式的乘積與和。

輸入格式:

輸入分2行,每行分別先給出多項式非零項的個數,再以指數遞降方式輸入一個多項式非零項係數和指數(絕對值均為不超過1000的整數)。數字間以空格分隔。

輸出格式:

輸出分2行,分別以指數遞降方式輸出乘積多項式以及和多項式非零項的係數和指數。數字間以空格分隔,但結尾不能有多餘空格。零多項式應輸出0 0

輸入樣例:

4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1

輸出樣例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1

5 20 -4 4 -5 2 9 1 -2 0


    

#include<stdio.h>
#include<stdlib.h>

typedef struct PolyNode *Polynomial;
struct PolyNode{
   int coef;
   int expon;
   Polynomial Next;
};

Polynomial ReadPoly()
{
    int n,i;
    scanf("%d",&n);
    Polynomial P=(Polynomial)malloc(sizeof(struct PolyNode));
    Polynomial node,current;
    P->Next=NULL;
    current=P;
    for(i=0;i<n;i++)
    {
      node=(Polynomial)malloc(sizeof(struct PolyNode));
      node->Next=NULL;
      scanf("%d %d",&node->coef,&node->expon);
      current->Next=node;
      current=node;
    }
    return P;
}

void PrintPoly(Polynomial L)
{
    if(!L->Next) printf("0 0\n");
    else{
    Polynomial t=L->Next;
    printf("%d %d",t->coef,t->expon);
    t=t->Next;
    while(t)
    {
        printf(" %d %d",t->coef,t->expon);
        t=t->Next;
    }
    printf("\n");
    }
}

Polynomial Add(Polynomial L1,Polynomial L2)
{
    Polynomial t1,t2,t3,x;
    Polynomial L3=(Polynomial)malloc(sizeof(struct PolyNode));
    L3->Next=NULL;
    t3=L3;
    t1=L1->Next;
    t2=L2->Next;
    while(t1&&t2)
    {
       x=(Polynomial)malloc(sizeof(struct PolyNode));
       x->Next=NULL;
       if(t1->expon>t2->expon)
       {
           x->coef=t1->coef;
           x->expon=t1->expon;
           t3->Next=x;
           t3=x;
           t1=t1->Next;
       }
       else if(t1->expon<t2->expon)
       {
           x->coef=t2->coef;
           x->expon=t2->expon;
           t3->Next=x;
           t3=x;
           t2=t2->Next;
       }
       else
       {
           if(t1->coef+t2->coef==0)
           {
              t1=t1->Next;
              t2=t2->Next;
           }
           else
            {
               x->coef=t1->coef+t2->coef;
               x->expon=t1->expon;
               t3->Next=x;
               t3=x;
               t1=t1->Next;
               t2=t2->Next;
            }
       }
    }
    while(t1)
    {
        t3->Next=t1;
        t3=t1;
        t1=t1->Next;
    }
    while(t2)
    {
        t3->Next=t2;
        t3=t2;
        t2=t2->Next;
    }
    return L3;
}

Polynomial  Multi(Polynomial L1,Polynomial L2)
{
    Polynomial t1,t2,t3,x,middle;
    Polynomial L3=(Polynomial)malloc(sizeof(struct PolyNode));
    L3->Next=NULL;
    t1=L1->Next;
    t2=L2->Next;
    while(t2)
    {
       middle=(Polynomial)malloc(sizeof(struct PolyNode));
       middle->Next=NULL;
       t3=middle;
       while(t1)
      {
       x=(Polynomial)malloc(sizeof(struct PolyNode));
       x->expon=t1->expon+t2->expon;
       x->coef=t1->coef*t2->coef;
       t3->Next=x;
       t3=x;
       t1=t1->Next;
      }
      L3=Add(L3,middle);
      t1=L1->Next;
      t2=t2->Next;
    }
    return L3;
}

int main()
{
    Polynomial L1,L2,L3,L4;
    L1=ReadPoly();
    L2=ReadPoly();
    L3=Multi(L1,L2);
    L4=Add(L1,L2);
    PrintPoly(L3);
    PrintPoly(L4);
    return 0;
}