다항식 의 덧셈 과 곱셈

데이터 구조의 선형 표, 체인 표 는 다항식 의 저장 과 두 다항식 의 덧셈 과 곱셈 조작 을 실현 한다.
A=a0+a1x+a2x2+...+anxn
입력:
   A      n1                  
   A      n2                  

출력:
                
                
#include 
#include 
#include 

typedef struct PolyNode *Polynomial;
struct PolyNode{
    int coef;           //  
    int expon;          //  
    Polynomial link;    //  
};

Polynomial ReadPoly();
Polynomial Mult(Polynomial P1, Polynomial P2);
Polynomial Add(Polynomial P1, Polynomial P2);
void PrintPoly(Polynomial P);
void Attach(int c, int e, Polynomial *Rear);

int main()
{
    Polynomial P1, P2, PP, PS;
    //     1
    P1 = ReadPoly();
    //     2
    P2 = ReadPoly();
    //         
    PP = Mult(P1, P2);
    printf("Mult Result: ");
    PrintPoly(PP);
    //         
    PS = Add(P1, P2);
    printf("Add Result: ");
    PrintPoly(PS);
    return 0;
}

/*
       :
1、          ReadPoly()
2、           Mult()
3、           Add()
4、            PrintPoly()
*/

Polynomial ReadPoly()
{
    int n;
    int c, e;
    Polynomial P, Rear, tmp;
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->link = NULL;
    Rear = P;
    scanf("%d", &n);
    while(n--)
    {
        scanf("%d %d", &c, &e);
        Attach(c, e, &Rear);
    }
    //       
    tmp = P;
    P = P->link;
    free(tmp);
    return P;
}

void Attach(int c, int e, Polynomial *Rear)
{
    //       ,      Rear  ,  Rear     
    Polynomial P;
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->coef = c;
    P->expon = e;
    P->link = NULL;
    (*Rear)->link = P;
    *Rear = P;
}

Polynomial Mult(Polynomial P1, Polynomial P2)
{
    Polynomial t1, t2;
    Polynomial P, Rear, t;
    if(!P1 || !P2) return NULL;
    t1 = P1;
    t2 = P2;
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->link = NULL;
    Rear = P;
    // t1      t2,          
    while(t2)
    {
        Attach(t1->coef*t2->coef, t1->expon+t2->expon, &Rear);
        t2 = t2->link;
    }

    t1 = t1->link;
    int e, c;
    //   t1      t2,                   
    while(t1)
    {
        t2 = P2;
        Rear = P;
        while(t2)
        {
            c = t1->coef * t2->coef;
            e = t1->expon + t2->expon;
            //          
            while(Rear->link && Rear->link->expon < e)
                Rear = Rear->link;
            //    ,            
            if(Rear->link && Rear->link->expon == e)
            {
                if(Rear->link->coef + c)
                    Rear->link->coef += c;
                else
                {
                    t = Rear->link;
                    Rear->link = t->link;
                    free(t);
                }
            }
            //         ,             
            else
            {
                t = (Polynomial)malloc(sizeof(struct PolyNode));
                t->coef = c;
                t->expon = e;
                t->link = Rear->link;
                Rear->link = t;
                Rear = Rear->link;
            }
            t2 = t2->link;
        }
        t1 = t1->link;
    }
    t = P;
    P = P->link;
    free(t);
    return P;
}

Polynomial Add(Polynomial P1, Polynomial P2)
{
    Polynomial t1, t2;
    Polynomial P, Rear, t;
    int tmp;
    t1 = P1; t2 = P2;
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->link = NULL;
    Rear = P;
    while(t1 && t2)
    {
        if(t1->expon == t2->expon)
        {
            tmp = t1->coef + t2->coef;
            if(tmp != 0)
                Attach(tmp, t1->expon, &Rear);
            t1 = t1->link;
            t2 = t2->link;
        }
        else if(t1->expon > t2->expon)
        {
            Attach(t2->coef, t2->expon, &Rear);
            t2 = t2->link;
        }
        else
        {
            Attach(t1->coef, t1->expon, &Rear);
            t1 = t1->link;
        }
    }
    while(t1)
    {
        Attach(t1->coef, t1->expon, &Rear);
        t1 = t1->link;
    }
    while(t2)
    {
        Attach(t2->coef, t2->expon, &Rear);
        t2 = t2->link;
    }
    t = P;
    P = P->link;
    free(t);
    return P;
}
void PrintPoly(Polynomial P)
{
    int flag = 0;
    while(P)
    {
        if(!flag)
            flag = 1;
        else
            printf(" ");
        printf(" %d %d", P->coef, P->expon);
        P = P->link;
    }
    printf("
"
); } /* 4 5 0 3 2 7 3 2 5 3 2 1 4 3 9 4 */

좋은 웹페이지 즐겨찾기