© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu
Quick Links
Easy Studies


CPP Program To Add Two Polynomial Equations :

/**PROGRAM TO ADD TWO POLYNOMIALS**/ #include #include #include #include class PolyAdd { public: struct barbie { int coff; int pow; struct barbie *link; }*ptr,*start1,*node,*start2,*start3,*ptr1,*ptr2; typedef struct barbie bar; int temp1,temp2; void create(void); void display(void); void polyaddtion(void); void sorting(void); }; /*-----------------------------------------------------------------------------*/ void PolyAdd :: create() { char ch; while(1) { cout<<"\n\nEnter the coff and pow :"; //scanf("%d%d",&node->coff,&node->pow); cin>>node->coff; cin>>node->pow; if (node->pow==0 ) { ptr=node; node=(bar *)malloc(sizeof(bar)); node=NULL; ptr->link=node; break; } cout<<"\n\nDo u want enter more coff ?(y/n)"; fflush(stdin); //scanf("%c",&ch); cin>>ch; if (ch=='n' ) { ptr=node; node=(bar *)malloc(sizeof(bar)); node=NULL; ptr->link=node; break; } ptr=node; node=(bar *)malloc(sizeof(bar)); ptr->link=node; } } /*-------------------------------------------------------------------------*/ void PolyAdd :: display() { int i=1; while(ptr!=NULL ) { if(i!=1) cout<<"+ "; cout<<" "<coff<<"x^"<pow<<" "; ptr=ptr->link; i++; } //printf(" %d^%d",ptr->coff,ptr->pow); } /*---------------------------------------------------------------------------*/ void PolyAdd :: sorting() { for(;ptr->coff!=NULL;ptr=ptr->link) for(ptr2=ptr->link;ptr2->coff!=NULL;ptr2=ptr2->link) { if(ptr->pow>ptr2->pow) { temp1=ptr->coff; temp2=ptr->pow; ptr->coff=ptr2->coff; ptr->pow=ptr2->pow; ptr2->coff=temp1; ptr2->pow=temp2; } } } /*---------------------------------------------------------------------------*/ void PolyAdd :: polyaddtion() { node=(bar *)malloc (sizeof(bar)); start3=node; ptr1=start1; ptr2=start2; while(ptr1!=NULL && ptr2!=NULL) { ptr=node; if (ptr1->pow > ptr2->pow ) { node->coff=ptr2->coff; node->pow=ptr2->pow; ptr2=ptr2->link; //update ptr list B } else if ( ptr1->pow < ptr2->pow ) { node->coff=ptr1->coff; node->pow=ptr1->pow; ptr1=ptr1->link; //update ptr list A } else { node->coff=ptr2->coff+ptr1->coff; node->pow=ptr2->pow; ptr1=ptr1->link; //update ptr list A ptr2=ptr2->link; //update ptr list B } node=(bar *)malloc (sizeof(bar)); ptr->link=node; //update ptr list C }//end of while if (ptr1==NULL) //end of list A { while(ptr2!=NULL) { node->coff=ptr2->coff; node->pow=ptr2->pow; ptr2=ptr2->link; //update ptr list B ptr=node; node=(bar *)malloc (sizeof(bar)); ptr->link=node; //update ptr list C } } else if (ptr2==NULL) //end of list B { while(ptr1!=NULL) { node->coff=ptr1->coff; node->pow=ptr1->pow; ptr1=ptr1->link; //update ptr list B ptr=node; node=(bar *)malloc (sizeof(bar)); ptr->link=node; //update ptr list C } } node=NULL; ptr->link=node; } /*------------------------------------------------------------------------------------*/ void main() { PolyAdd obj; clrscr(); cout<<"\n\nEnrter the elements of the first poly"; obj.node = (bar *) malloc(sizeof (bar)); obj.start1=obj.node; if (obj.start1==NULL) { cout<<"\n\nUnable to create memory."; getch(); exit(0); } obj.create(); cout<<"\n\nEnrter the elements of the second poly :"; obj.node = (bar *) malloc(sizeof (bar)); obj.start2=obj.node; if (obj.start2==NULL) { cout<<"\n\nUnable to create memory."; getch(); exit(0); } obj.create(); clrscr(); //printing the elements of the lists cout<<"\n\nThe elements of the poly first are :"; obj.ptr=obj.start1; obj.display(); cout<<"\n\nThe elements of the poly second are :"; obj.ptr=obj.start2; obj.display(); cout<<"\n\nThe first sorted list is :"; obj.ptr=obj.start1; obj.sorting(); obj.ptr=obj.start1; obj.display(); cout<<"\n\nThe second sorted list is :"; obj.ptr=obj.start2; obj.sorting(); obj.ptr=obj.start2; obj.display(); cout<<"\n\nThe sum of the two lists are :"; obj.polyaddtion(); obj.ptr=obj.start3; obj.display(); getch(); }

SAMPLE INPUT AND OUTPUT:

 Enter the element of the first poly

Enter the coff and pow : 3  2

Do u want enter more coff ?(y/n)y

Enter the coff and pow :5  1

Do u want enter more coff ?(y/n)n

Enter the elements of the second poly:

Enter the coff and pow :4  2

Do u want enter more coff ?(y/n)y

Enter the coff and pow :4  2

The elements of the poly first are :  3x^2 + 5x^1
The elements of the poly first are :  4x^2 + 4x^1
The first sorted list is  : 5X^1 + 3X^2
The sacond sorted list is : 4X^1 + 4x^2
The sum of the two lists are : 9X^1 + 7X^2

 
SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑
bottom