Polynomial Multiplication

/* Program to multiply two polynomials  */   

    #include
    #include
    #define new (nd *)malloc(sizeof(nd))
    struct poly
        {
            int coef;
            int pow;
            struct poly *next;
        };
    typedef struct poly nd;
    void swap(int *x,int *y)
    {
        int temp=*x;
        *x=*y;
        *y=temp;
    }
    create(nd **h1)
        {
            char ch;
            nd *t=*h1;
            do
            {
                printf("Enter coef : ");
                scanf("%d",&t->coef);
                printf("Enter power : ");
                scanf("%d",&t->pow);
                printf("Want to continue ? (y/n)  ");
                scanf("%s",&ch);
                printf("\n");
                if(ch=='y'||ch=='Y')
                    {
                        t->next=new;
                        t=t->next;
                    }
            }while(ch=='y'||ch=='Y');
            t->next=NULL;
        }
    display(nd **h1)
    {
    nd *t=*h1;
    while(t!=NULL)
    {   
        if(t->coef==0)
            printf("");
        else if((t->coef==1) && (t->pow==1))
            printf("x + ");
        else if((t->coef<0)&&(t->pow>0 && t->pow!=1))
        {
            printf("\b\b\b");
            printf(" %dx^%d + ",t->coef,t->pow);
        }
        else if(t->coef<0 && t->pow==0)
        {
            printf("\b\b\b");
            printf(" %d + ",t->coef);
        }
        else if((t->coef<0)&&(t->pow==1))
        {    printf("\b\b\b");
            printf(" %dx + ",t->coef);
        }
        else if((t->coef<0)&&(t->pow>1))
        {    printf("\b\b\b");
            printf(" -x^%d + ",t->pow);
        }
        else if((t->coef==-1)&&(t->pow==1))
        {
            printf("\b\b\b");
            printf("- x + ");
        }
        else if(t->coef==-1)
        {
            printf("\b\b\b");
            printf("-x^%d + ",t->pow);
        }
        else if((t->coef==1) && ((t->pow!=1)||(t->pow!=0)))
            printf("x^%d + ",t->pow);   
        else if(t->pow==0)
            printf("%d + ",t->coef);
        else if(t->pow==1)
            printf("%dx + ",t->coef);
        else if(t->coef==1 && t->coef==0)
            printf(" 1 + ");
        else
            printf("%dx^%d + ",t->coef,t->pow);
        t=t->next;
    }
    }
    void sort(nd **ptr)
    {
        nd *i,*j;
        for(i=*ptr;i->next!=NULL; i=i->next)
            for(j=i->next; j!=NULL; j=j->next)
                if(i->pow < j->pow)
                {
                    swap(&i->pow,&j->pow);
                    swap(&i->coef,&j->coef);
                }
    }
    void mul(nd **h1,nd **h2,nd **h3)
    {
        nd *t1=*h1,*t2=*h2,*t3=*h3;
        while(t1!=NULL)
        {   
            t2=*h2;
            while(t2!=NULL)
            {
                t3->coef=t1->coef * t2->coef;
                t3->pow=t1->pow + t2->pow;
                t2=t2->next;
                t3->next=new;
                t3=t3->next;
            }
            t1=t1->next;
        }   
        t3->next=NULL;
    }
    void search(nd **h3,nd **h4)
    {
        nd *i=*h3,*j,*t4=*h4;
        for(i=*h3;i!=NULL;i=i->next)
        {
            for(j=i->next;j!=NULL;j=j->next)
            {
                if(i->pow==j->pow)
                {
                    t4->coef=i->coef + j->coef;
                    t4->pow=i->pow;
                    j->coef=0;
                }
                else
                {
                    t4->coef=i->coef;
                    t4->pow=i->pow;
                    t4->next=new;
                    t4=t4->next;
                    break;
                }
               
            }
        }
    }   
                   
   
    main()
        {
            printf("\nProgram to multiply two polynomials : \n");
            nd *h1,*h2,*h3,*h4;
            h1=new;
            h2=new;
            h3=new;
            h4=new;
            printf("\nEnter the elements of 1st polynomial : \n");
            create(&h1);
            sort(&h1);
            printf("Polynomial 1 :  ");
            display(&h1);
            printf("\b\b  ");
            printf("\n");
            printf("\nEnter the elements of 2nd polynomial : \n");
            create(&h2);
            sort(&h2);
            printf("Polynomial 2 :  ");
            display(&h2);
            printf("\b\b  ");
            printf("\n");
            mul(&h1,&h2,&h3);
            sort(&h3);
            //printf("\nProduct of Polynomials :  ");
           //display(&h3);
            printf("\b\b   ");
            search(&h3,&h4);
            printf("\n\nProduct of polynomials :   ");
            display(&h4);
            printf("\b\b   ");
            printf("\n\n");
        }

Top