多项式的加法与乘法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LoverJuan/article/details/76854700

#include <stdio.h>

#include <stdlib.h>

typedef struct PolyNode *Polynomial;

struct PolyNode

{

    int coef;

    int expon;

    Polynomial link;

};


Polynomial ReadPoly();

void Attach(int c, int e, Polynomial *pRear);

void PrintPoly(Polynomial P);

Polynomial Add(Polynomial P1, Polynomial P2);

Polynomial Mult(Polynomial P1, Polynomial P2);


int main()

{

    Polynomial P1,P2,PP,PS;

    P1 = ReadPoly();

    P2 = ReadPoly();

    PS = Mult(P1,P2);

    PP = Add(P1, P2);

    PrintPoly(PS);

    PrintPoly(PP);

    return 0;

}

void Attach(int c, int e, Polynomial *pRear)

{

    Polynomial P;

    P = (Polynomial)malloc(sizeof(struct PolyNode));

    P->coef = c;

    P->expon = e;

    P->link = NULL;

    (*pRear)->link = P;

    *pRear = P;

}

Polynomial ReadPoly()

{

    int N;

    scanf("%d",&N);

    int c, e;

    Polynomial Rear, P, t;

    P = (Polynomial)malloc(sizeof(struct PolyNode));

    P->link = NULL;

    Rear = P;

    while (N--) {

        scanf("%d %d",&c,&e);

        Attach(c, e, &Rear);

    }

    t = P; P = P->link; free(t);

    return P;

}


Polynomial Add(Polynomial P1, Polynomial P2)

{

    Polynomial t1,t2,P,Rear;

    t1 = P1;

    t2 = P2;

    P = (Polynomial)malloc(sizeof(struct PolyNode));

    P->link = NULL;

    Rear = P;

    while (t1&&t2) {

        if (t1->expon == t2->expon) {

            if (t1->coef + t2->coef == 0) {

            }else

            {

                Attach(t1->coef+t2->coef, t1->expon, &Rear);

            }

            t1 = t1->link;

            t2 = t2->link;

        }else if (t1->expon > t2->expon)

        {

            Attach(t1->coef, t1->expon, &Rear);

            t1 = t1->link;

            

        }else if(t1->expon < t2->expon)

        {

            Attach(t2->coef, t2->expon, &Rear);

            t2 = t2->link;

        }

    }

    while (t1) {

        Attach(t1->coef, t1->expon, &Rear);

        t1 = t1->link;

    }

    while (t2) {

        Attach(t2->coef, t2->expon, &Rear);

        t2 = t2->link;

    }

    Polynomial t = P;

    P = P->link; free(t);

    return P;

}

void PrintPoly(Polynomial P)

{

    if (!P) {

        printf("0 0\n");

        return;

    }

    while (P->link) {

        printf("%d %d ",P->coef,P->expon);

        P = P->link;

    }

    printf("%d %d\n",P->coef,P->expon);

}

Polynomial Mult(Polynomial P1, Polynomial P2)

{

    int flag = 1;

    Polynomial t1, t2;

    t1 = P1; t2 = P2;

    Polynomial Result = NULL;

    Polynomial t = NULL;

    while (t1) {

        Polynomial temp = (Polynomial)malloc(sizeof(struct PolyNode));

        temp->link = NULL;

        Polynomial nowRear = temp;

        t2 = P2;

        while (t2) {

            Attach(t1->coef*t2->coef, t1->expon+t2->expon, &nowRear);

            t2 = t2->link;

        }

        if (flag) {

            flag = 0;

            Result = temp->link;

        }else

        {

            Result = Add(Result, temp->link);

            while (temp) {

                t = temp;

                free(t);

                temp = temp->link;

            }

        }

        temp = nowRear = NULL;

        t1 = t1->link;

    }

    return Result;

}

猜你喜欢

转载自blog.csdn.net/LoverJuan/article/details/76854700