多项式的乘法和除法

02-线性结构2 一元多项式的乘法与加法运算   (20分)

设计函数分别求两个一元多项式的乘积与和。

乘法主要思想:

1.将乘法运算转换为加法运算 将P1当前项(ci,ei)乘P2多项式,再加到结果多项式里 

t1 = P1; t2 = P2;

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

P->link = NULL; 

Rear = P; while (t2) {Attach(t1->coef*t2->coef, t1->expon+t2->expon, &Rear);t2 = t2->link; }

2.逐项插入 将P1当前项(c1i,e1i)乘P2当前项(c2i,e2i),并插入到结果多项式中。关键是要找到插入位置 初始结果多项式可由P1第一项乘P2获得(如上)

输入格式:

输入分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		

#include 
     
     
      
      
#include 
      
      
       
       


typedef int ElementType ;
typedef struct Node * PNode;

struct Node
{

    ElementType exponent;
    ElementType factor;
    PNode next;
};
typedef PNode List;

List read();

void Print(List a);

List add(List a,List b);

List mult(List a,List b);
void Attach(ElementType a,ElementType b,PNode *c);
int main()
{
    List a;
    List b;
    List result;
   // freopen("11.txt","r",stdin);
    a = read();
    b = read();
    result = mult(a,b);
    Print(result);

    result = add(a,b);
    Print(result);


    return 0;
}

List read(List a)
{
    PNode Ahead;
    PNode Rear;
    PNode f;
    Ahead = (PNode)malloc(sizeof(struct Node));
    Ahead ->next = NULL;
    Rear = Ahead;
    int n;
    int exp,fac;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d %d",&fac,&exp);
        Attach(fac,exp,&Rear);
        Rear->next = NULL;
    }
    f = Ahead;
    Ahead = Ahead->next;
    free(f);
    return Ahead;
}

void Print(List a)
{
    PNode PrintNode;

    PrintNode = a;
    if(PrintNode == NULL)
    {
        printf("0 0\n");
        return;
    }
    int tag = 0;

    while(PrintNode)
    {
        if(!tag)
            tag=1;
            else
                printf(" ");

        printf("%d %d",PrintNode->factor,PrintNode->exponent);

        PrintNode = PrintNode->next;

    }
    printf("\n");
}
void Attach(int a,int b,PNode *p)
{
    PNode c;

    c = (PNode)malloc(sizeof(struct Node));
    c->factor = a;
    c->exponent = b;
    c->next = NULL;
    (*p)->next = c;
    *p = c;

}
List add(List a,List b)
{
    PNode front,rear,f;
    PNode t1,t2;
    t1=a;
    t2=b;
    rear = (PNode)malloc(sizeof(struct Node));
    rear->next=NULL;
    front = rear;
    int sum;
    while(t1&&t2)
    {
        if(t1->exponent==t2->exponent)
        {
            sum = t1->factor+t2->factor;
            if(sum)
                Attach(sum,t1->exponent,&rear);
            t1 = t1->next;
            t2 = t2->next;

        }
        else if(t1->exponent > t2->exponent)
        {
            Attach(t1->factor,t1->exponent,&rear);
            t1=t1->next;
        }
        else if(t1->exponent < t2->exponent)
        {
            Attach(t2->factor,t2->exponent,&rear);
            t2=t2->next;
        }

    }

    for(; t1; t1=t1->next)
    {
        Attach(t1->factor,t1->exponent,&rear);
    }

    for(; t2; t2=t2->next)
    {
        Attach(t2->factor,t2->exponent,&rear);
    }

    rear->next = NULL;
    f = front;
    front = front->next;
    free(f);

    return front;

}

List mult(List a,List b)
{
    PNode t1,t2,rear,front,t;
    int fac,exp;
    if(!a||!b)
    {
        return NULL;
    }
    t1 = a;
    t2 = b;

    front = (PNode)malloc(sizeof(struct Node));
    front ->next=NULL;
    rear = front;

    while(t2)
    {
        Attach(t1->factor*t2->factor,t1->exponent+t2->exponent,&rear);
        t2 = t2->next;
    }
    t1=t1->next;

    while(t1)
    {
        t2 = b;
        rear = front;
        while(t2)
        {
            fac = t1->factor*t2->factor;
            exp = t1->exponent+t2->exponent;
            while(rear->next&&rear->next->exponent > exp)
                rear=rear->next;
            if(rear->next&&rear->next->exponent == exp)
            {
                if(rear->next->factor+fac)
                    rear->next->factor+=fac;
                else
                {
                    t = rear->next;
                    rear->next = t->next;
                    free(t);
                }
            }
            else
            {
                t = (PNode)malloc(sizeof(struct Node));
                t->exponent = exp;
                t->factor = fac;
                t->next = rear->next;
                rear->next = t;
                rear=rear->next;

            }
            t2=t2->next;
        }

        t1=t1->next;

    }

    t2 = front;
    front = front->next;
    free(t2);
   return front;
}


      
      
     
     

														
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值