Polynomial Addition

/* Program to add 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==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)&&(t->pow==0))
        {
            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
            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);
                }
    }
    add(nd **h1,nd **h2,nd **h3)
    {
    nd *t1=*h1,*t2=*h2,*t3=*h3;
    while(t1!=NULL && t2!=NULL)
    {
        if(t1->pow > t2->pow)
        {
            t3->pow=t1->pow;
            t3->coef=t1->coef;
            t3->next=new;
            t3=t3->next;
            t1=t1->next;
        }
        else if(t1->pow < t2->pow)
        {
            t3->pow=t2->pow;
            t3->coef=t2->coef;
            t3->next=new;
            t3=t3->next;
            t2=t2->next;
        }
        else
        {
            t3->pow=t1->pow;
            t3->coef=t1->coef + t2->coef;
            t3->next=new;
            t3=t3->next;
            t1=t1->next;
            t2=t2->next;
        }
    }   
    while(t1!=NULL)
    {
        t3->pow=t1->pow;
        t3->coef=t1->coef;
        t3->next=new;
        t3=t3->next;
        t1=t1->next;       
    }
    while(t2!=NULL)
    {
        t3->pow=t2->pow;
        t3->coef=t2->coef;
        t3->next=new;
        t3=t3->next;
        t2=t2->next;       
    }
    }
    main()
        {
            printf("\nProgram to add 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");
            add(&h1,&h2,&h3);
            printf("\nSum of Polynomials :  ");
            display(&h3);
            printf("\b\b   ");
            printf("\n\n");
        } 

Top