Sunday, 16 October 2011

POLYNOMIAL ADDITION


/*POLYNOMIAL ADDITION*/
#include<stdio.h>
#include<malloc.h>
struct link
{
int coeff;
int power;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
            char ch;
            do
            {
                        printf("\n Enter the coefficient");
                        scanf("%d",&node->coeff);
                        printf("\n Enter the power");
                        scanf("%d",&node->power);
                        node->next=malloc(sizeof(struct link));
                        node=node->next;
                        node->next=NULL;
                        printf("\n continue(y/n):");
                        scanf("%s",&ch);
            }
            while(ch=='y'||ch=='Y');
           
}
void display(struct link *node)
{
            while(node->next!=NULL)
            {
                        printf("%dx^%d",node->coeff,node->power);
                        node=node->next;
                        if(node->next!=NULL)
                        printf("+");
            }
}
void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
{
            while(poly1->next&&poly2->next)
            {
                        if(poly1->power>poly2->power)
                        {
                                    poly->power=poly1->power;
                                    poly->coeff=poly1->coeff;
                                    poly1=poly1->next;
                        }
                        else if(poly1->power<poly2->power)
                        {
                                    poly->power=poly2->power;
                        poly->coeff=poly2->coeff;
                        poly2=poly2->next;
                        }
                        else
                        {
                                    poly->power=poly1->power;
                                    poly->coeff=poly1->coeff+poly2->coeff;
                                    poly1=poly1->next;
                                    poly2=poly2->next;
                        }
                        poly->next=malloc(sizeof(struct link));
                        poly=poly->next;
                        poly->next=NULL;
            }
            while(poly1->next||poly2->next)
            {
                        if(poly1->next)
                        {
                                    poly->power=poly1->power;
                                    poly->coeff=poly1->coeff;
                                    poly1=poly1->next;
                        }
                        if(poly2->next)
                        {
                                    poly->power=poly2->power;
                        poly->coeff=poly2->coeff;
                        poly2=poly2->next;
                        }
                        poly->next=malloc(sizeof(struct link));
                        poly=poly->next;
                        poly->next=NULL;
            }
}
int main()
{
            poly1=malloc(sizeof(struct link));
            poly2=malloc(sizeof(struct link));
            poly=malloc(sizeof(struct link));
            printf("\n Enter the first polynomial");
            create(poly1);
            printf("\n First polynomial");
            display(poly1);
            printf("\n Enter the second polynomial");
        create(poly2);
        printf("\n Second polynomial");
        display(poly2);
            polyadd(poly1,poly2,poly);
            printf("\n Addition of two polynomial");
            display(poly);
}

OUTPUT:

Enter the first polynomial
Enter the first coefficient10

Enter the power3

Continue<y/n>:y
Enter the first coefficient20

Enter the power2
Continue<y/n>:y
Enter the first coefficient30

Enter the power1
Continue<y/n>:n
First polynomial 10x^3+20x^2+30x^1
Enter thr second polynomial
Enter the first coefficient1

Enter the power3

Continue<y/n>:y
Enter the first coefficient2

Enter the power2
Continue<y/n>:y
Enter the first coefficient3

Enter the power1
Continue<y/n>:n
second polynomial 1x^3+2x^2+3x^1

addition of two polynomial 11x^3+22x^2+33x^1

No comments:

Post a Comment