c0201时遇到的问题

多项式合并同类项,实现乘法加法

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

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


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


int main()
{
    Polynomial P1,P2,PP,PS;

    P1 = ReadPoly();
    P2 = ReadPoly();
    PP = Mult(P1,P2);
    PrintPoly( PP );
    PS = Add(P1,P2);
    printf("\n");
    PrintPoly( PS );
    while(1);
    return 0;
}
Polynomial ReadPoly()
{
    Polynomial P,Rear,t;
    int c,e,N;

    scanf("%d",&N);
    P = (Polynomial)malloc(sizeof(struct PolyNode)); /*链表头空结点*/
    P->link = NULL;
    Rear = P;
    while(N--){
        scanf("%d %d", &c, &e);
        Attach(c,e,&Rear);   /*c函数的传值属性,由于Attach后Rear的值需要改变,所以一定是传入Rear的地址&*/
    }

    t = P; P = P->link; free(t);  /*删除临时生成的头结点*/

    return P;
}
void Attach(int c, int e, Polynomial *pRear) /*pRear是指针的指针*/
{
    Polynomial P;

    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->coef = c;
    P->expon = e;
    P->link = NULL;
    (*pRear)->link = P;
    *pRear = P;   
}
Polynomial Add(Polynomial P1,Polynomial P2)
{
    int sum ;                      /*一定要先全定义后,再赋值*/
    Polynomial Rear,P,temp;        /*头结点,当前位置,返回的指针,插入时用来申请空间的结点,最后释放空结点的指针*/
    Polynomial t1,t2;     /*先初始化!!!!!*/


    t1 = P1; t2 = P2;
    P = (Polynomial)malloc(sizeof(struct PolyNode));P->link=NULL; 
    Rear = P;   /*Rear指向尾巴*/

    while(t1 && t2){
        if(t1->expon == t2->expon){
            sum = t1->coef + t2->coef;
            if( sum ) Attach(sum, 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{
            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;
    }
    Rear->link = NULL;
    temp = P;
    P = P->link;
    free(temp);
    return P;         
}

Polynomial Mult(Polynomial P1,Polynomial P2)
{
    Polynomial P,Rear,t1,t2,t;
    int c,e;

    if(!P1||!P2) return NULL; /*都为空*/

    t1 = P1;t2 = P2;
    P = (Polynomial)malloc(sizeof(struct PolyNode)); P->link = NULL;
    Rear = P;
    while(t2){        /*先用P1的第一项乘以P2,得到P,构造一个初始多项式*/
        Attach(t1->coef*t2->coef,t1->expon+t2->expon,&Rear);
        t2 = t2->link;
    }
    t1 = t1->link;
    while(t1){ /*这个两重循环用t1的每一项乘t2的每一项*/
        t2 = P2; Rear = P;/*重新赋值此时Rear和P指向的是头空结点*/
        while(t2){
            e = t1->expon + t2->expon;
            c = t1->coef * t2->coef;    /*构造后如何插入*/
            while( Rear->link && Rear->link->expon > e)
                Rear = Rear->link;      /*找到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{   /*Rear->link->expon小于插入的项,那么直接插入到Rear的后面*/
                t = (Polynomial)malloc(sizeof(struct PolyNode));
                t->coef = c; t->expon = e;
                t->link = Rear->link;
                Rear->link = t; Rear = Rear->link; /*此为Insert不是Attach*/
            }
            t2 = t2->link;
        }
        t1 = t1->link;
    }
    /*对于最后的结果处理 合并同类项*/
    t2 = P; P = P->link; free(t2); /*删除空头结点*/
    return P;
}

void PrintPoly(Polynomial P)
{
    int flag = 0;    /*辅助调整输出格式*/

    if(!P){printf("0 0\n");return;}  /*P为空*/

    while( P ){
        if(!flag)
            flag  = 1;
        else
            printf(" ");
        printf("%d %d", P->coef, P->expon);
        P = P->link;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值