affiliate marketing

Monday 12 December 2011

POLYNOMIAL MANUPULATION


POLYNOMIAL MANUPULATION
Aim
To implement polynomial manipulation using doubly linked lists.
Algorithm
POLYADD(POLY1: POLY2:POLY)
HEAD:POLY
Step 1: Assign HEAD+=NULL
Step2: While (POLY !=null)
Step3: HEAD=INSERTNODE(HEAD,COPYNODE,(POLY1,1))
Step4: POLY1=POLY1àNEXT
Step5: [End of Step2 while structure]
Step6: While(POLY2 1=NULL)
Step7: HEAD =INSERTNODE(HEAD,COPYNODE(POLY2,1))
Step8: POLY2=POLY2àNEXT
Step9: [End of Step 6 while Structure]
Step10: Return HEAD
END POLYADD()

Algorithm for polynomial subtraction

POLYSUB(POLY1:POLY, POLY2:POLY)
HEAD:POLY
Step1: Assign HEAD=NULL
Step2: While(POLY1!=NULL)
Step3: HEAD=INSERTNODE(HEAD,COPYNODE(POLY1,1))
Step4: POLY1=POLY1à NEXT
Step5: [End of Step2 while Structure]
Step6:While(POLY2!=NULL)
Step7: HEAD=INSERTNODE(HEAD,COPYNODE(POLY2,-1))
Step8: POLY2=POLY2àNEXT
Step9: [End of Step 6 While Structure]
Step10: Return HEAD
END POLYSUB()

Coding:

#include<malloc.h>
#include<conio.h>
struct link
{
            int coeff;
            int pow;
            struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
            char ch;
            do
            {
                         printf("\nEnter the coefficient :");
                         scanf("%d",&node->coeff);
                         printf("\nEnter the power :");
                         scanf("%d",&node->pow);
                         node->next=(struct link *)malloc(sizeof(struct link));
                         node=node->next;
                         node->next=NULL;
                         printf("\nContinue??? (Y/N) :");
                         ch=getch();
            }while(ch=='y' || ch=='Y');
}
void display(struct link *node)
{
            while(node->next!=NULL)
            {
                        printf("%dx^%d",node->coeff,node->pow);
                        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(poly->pow > poly2->pow)
                        {
                                    poly->pow=poly1->pow;
                                    poly->coeff=poly1->coeff;
                                    poly1=poly1->next;
                        }
                        else if(poly1->pow < poly2->pow)
                        {
                                    poly->pow=poly2->pow;
                                    poly->coeff=poly2->coeff;
                                    poly2=poly2->next;
                        }
                        else
                        {
                                    poly->pow=poly1->pow;
                                    poly->coeff=poly1->coeff+poly2->coeff;
                                    poly1=poly1->next;
                                    poly2=poly2->next;
                        }
                        poly->next=(struct link *)malloc(sizeof(struct link));
                        poly=poly->next;
                        poly->next=NULL;
            }
            while(poly1->next||poly2->next)
            {
                        if(poly1->next)
                        {
                                    poly->pow=poly1->pow;
                                    poly->coeff=poly1->coeff;
                                    poly1=poly1->next;
                        }
                        if(poly2->next)
                        {
                                    poly->pow=poly2->pow;
                                    poly->coeff=poly2->coeff;
                                    poly2=poly2->next;
                        }
                        poly->next=(struct link *)malloc(sizeof(struct link));
                        poly=poly->next;
                        poly->next=NULL;
            }
}
void main()
{
            poly1=(struct link *)malloc(sizeof(struct link));
            poly2=(struct link *)malloc(sizeof(struct link));
            poly=(struct link *)malloc(sizeof(struct link));
            clrscr();
            printf("\nEnter the first polynomial::");
            create(poly1);
            printf("\nFirst polynomial is :: \n");
            display(poly1);
            printf("\nEnter the second polynomial::");
            create(poly2);
            printf("\nSecond polynomial is :: \n");
            display(poly2);
            polyadd(poly1,poly2,poly);
            printf("\nAddition of the two polynomials::");
            display(poly);
            getch();
}



Output

Enter the first polynomial::

Enter the coefficient :5

Enter the power :3

Continue??? (Y/N) :Y
Enter the coefficient :3

Enter the power :2

Continue??? (Y/N) :
First polynomial is ::
5x^3 + 3x^2
Enter the second polynomial::
Enter the coefficient :7

Enter the power :3

Continue??? (Y/N) :
Second polynomial is ::
7x^3
Addition of the two polynomials::12x^3 + 3x^2

No comments:

Post a Comment