Returns the product and (c) of two unidimensional polynomials. The product of polynomials is C.

Source: Internet
Author: User

Returns the product and (c) of two unidimensional polynomials. The product of polynomials is C.
The design function is used to calculate the product and sum of two unary polynomials. Input Format:

The input is divided into two rows. Each row first gives the number of non-zero polynomial items, and then enters a non-zero polynomial coefficient and exponent (the absolute value is an integer not greater than 1000) in the exponential descent mode ). Numbers are separated by spaces.

Output Format:

The output is divided into two rows. The product polynomials and non-zero coefficients and exponent of polynomials are output in the exponential descent mode respectively. Numbers are separated by spaces, but no extra space is allowed at the end. Zero polynomial output0 0.

Input example:
4 3 4 -5 2  6 1  -2 03 5 20  -7 4  3 1

Output example:


15 24-25 22 30 21-10 20-21 8 35 6-33 5 14 4-15 3 18 2-6 15 20-4 4-5 2 9 1-2 0


The Code is as follows:

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

Typedef struct PolyNode *Polynomial;
Struct PolyNode {
    Int coef;
    Int expon;
    Polynomial link;
};

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; The new node pointed to by /*P is inserted after the current tail item*/
    *pRear=P; /*pRear pointer moves to P node*/
}

Int Compare(int a,int b){
    Int c;
    If (a>b) c=1;
    Else if (a<b) c=-1;
    Else c=0;
    Return c;
}

Polynomial ReadPoly(){
    Polynomial P,Rear,t;
    Int c,e,N;
    
    Scanf("%d",&N);
    P=(Polynomial)malloc(sizeof(struct PolyNode));/*chain header empty node*/
    P->link=NULL;
    Rear=P;
    While(N--){
        Scanf("%d %d",&c,&e);
        Attach(c,e,&Rear); /*Inserts the current item into the polynomial tail*/
    }
    t=P;P=P->link;free(t); /*Remove the temporarily generated head node*/
    Return P;
}

Polynomial Add (Polynomial P1, Polynomial P2){
    Polynomial front,rear,temp;
    Int sum;
    Rear=(Polynomial)malloc(sizeof(struct PolyNode));
    Front=rear;//front is used to record the link header node
    While(P1&&P2){

        Switch(Compare(P1->expon,P2->expon)){
            Case 1:
                Attach(P1->coef,P1->expon,&rear);
                P1=P1->link;
                Break;
            Case -1:
                Attach(P2->coef,P2->expon,&rear);
                P2=P2->link;
                Break;
            Case 0:
                Sum=P1->coef+P2->coef;
                If (sum){
                    Attach(sum,P1->expon,&rear);
                }
                P1=P1->link;
                P2=P2->link;
                Break;
            
        }
    }
    For(;P1;P1=P1->link) Attach(P1->coef,P1->expon,&rear);
    For(;P2;P2=P2->link) Attach(P2->coef,P2->expon,&rear);
    Rear->link=NULL;
    Temp=front;
    Front=front->link;
    Free(temp);
    Return front;
}

Polynomial Mult (Polynomial P1, Polynomial P2){
    Polynomial P, Rear, t1, t2, t;
    Int c,e;
    If (!P1||!P2) return NULL; / / determine if one of P1 or P2 is NULL, the result is NULL;
    
    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;
    }
    T1=t1->link;//t1 has been multiplied, moved backward
    While (t1){
        T2=P2; Rear=P;
        While(t2){
            e=t1->expon+t2->expon;
            c=t1->coef*t2->coef;
            While(Rear->link && Rear->link->expon>e)
            Rear=Rear->link;//When the multiplied e is smaller than the polynomial coefficient, the Rear pointer moves backward
            If(Rear->link && Rear->link->expon==e){
                If(Rear->link->coef+c)
                    Rear->link->coef+=c;//If the sum of the polynomial coefficients is not 0, the new coefficient is assigned.
                Else{
                    t=Rear->link;
                    Rear->link=t->link;
                    Free(t);//If the coefficient of the polynomial is 0, then the Rear pointer points to the next item, which releases the node
                }
            }
            Else{
                t=(Polynomial)malloc(sizeof(struct PolyNode));
                T->coef=c;t->expon=e;
                T->link=Rear->link;
                Rear->link=t;Rear=Rear->link;//If the polynomial does not reach the end and does not currently have this item, allocate memory space, create a new node, and insert the node into the linked list, and the Rear pointer points to the new node. Point t
            }
        T2=t2->link;
        }
    T1=t1->link;
    }
T2=P;P=P->link;free(t2);
Return P;
}

Void PrintPoly(Polynomial P){
    Int flag=0; //Used to assist in adjusting the output format
    
    If(!P) {printf("0 0\n");return;}
    
    While (P){
        If(!flag) flag=1;
        Else printf(" ");
        Printf("%d %d",P->coef,P->expon);
        P=P->link;
    }
    Printf("\n");
}

Int main()
{
    Polynomial P1, P2, PP, PS;
    P1=ReadPoly();
    P2=ReadPoly();
    PP=Mult(P1,P2);
    PrintPoly(PP);
    PS=Add(P1,P2);
    PrintPoly(PS);
    
    Return 0;
} 

 

 
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.