多项式的加法和乘法

数据结构线性表,链表实现多项式的存储以及两个多项式的加法和乘法操作。

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("\n");
}

/*
4 5 0 3 2 7 3 2 5
3 2 1 4 3 9 4
*/

你可能感兴趣的:(数据结构,数据结构,链表,c语言)