Add to Favorites    Make Home Page 4961 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Data Structures Home » Add and subtract two polynomials(Using Linked List)

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Add and subtract two polynomials(Using Linked List)
Author Arun Vishnu
Author Email vyomworld [at] vyomworld.com
Description Add and subtract two polynomials, using linked list.
Category C++ » Data Structures
Hits 368715
Code Select and Copy the Code
#include<iostream.h> #include<conio.h> #include<process.h> // Creating a NODE Structure struct node { int coe,exp; // data struct node *next; // link to next node and previous node }; // Creating a class Polynomial class polynomial { struct node *start,*ptrn,*ptrp; public: void get_poly(); // to get a polynomial void show(); // show void add(polynomial p1,polynomial p2); // Add two polynomials void subtract(polynomial p1,polynomial p2); //Subtract2 polynomials }; void polynomial::get_poly() // Get Polynomial { char c='y'; ptrn=ptrp=start=NULL; while(c=='y' || c=='Y') { ptrn=new node; ptrp->next=ptrn; if(start==NULL) start=ptrn; ptrp=ptrn; cout<<" Enter the coefficient: "; cin>>ptrn->coe; cout<<"Enter the exponent: "; cin>>ptrn->exp; ptrn->next=NULL; cout<<"Enter y to add more nodes: "; cin>>c; } return; } void polynomial::show() // Show Polynomial { struct node *ptr; ptr=start; while(ptr!=NULL) { cout<<ptr->coe<<"X^"<<ptr->exp<<" + "; ptr=ptr->next; } cout<<" "; } void polynomial::add(polynomial p1,polynomial p2) // Add Polynomials { struct node *p1ptr,*p2ptr; int coe,exp; ptrn=ptrp=start=NULL; p1ptr=p1.start; p2ptr=p2.start; while(p1ptr!=NULL && p2ptr!=NULL) { if(p1ptr->exp==p2ptr->exp) // If coefficients are equal { coe=p1ptr->coe+p2ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; p2ptr=p2ptr->next; } else if(p1ptr->exp>p2ptr->exp) { coe=p1ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; } else if(p1ptr->exp<p2ptr->exp) { coe=p2ptr->coe; exp=p2ptr->exp; p2ptr=p2ptr->next; } ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } // End of While if(p1ptr==NULL) { while(p2ptr!=NULL) { coe=p2ptr->coe; exp=p2ptr->exp; p2ptr=p2ptr->next; ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } } else if(p2ptr==NULL) { while(p1ptr!=NULL) { coe=p1ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } } } // End of addition // Subtract two polynomials void polynomial::subtract(polynomial p1,polynomial p2) // Subtract { struct node *p1ptr,*p2ptr; int coe,exp; ptrn=ptrp=start=NULL; p1ptr=p1.start; p2ptr=p2.start; while(p1ptr!=NULL && p2ptr!=NULL) { if(p1ptr->exp==p2ptr->exp) // If coefficients are equal { coe=p1ptr->coe-p2ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; p2ptr=p2ptr->next; } else if(p1ptr->exp>p2ptr->exp) { coe=p1ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; } else if(p1ptr->exp<p2ptr->exp) { coe=0-p2ptr->coe; exp=p2ptr->exp; p2ptr=p2ptr->next; } ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } // End of While if(p1ptr==NULL) { while(p2ptr!=NULL) { coe=0-p2ptr->coe; exp=p2ptr->exp; p2ptr=p2ptr->next; ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } } else if(p2ptr==NULL) { while(p1ptr!=NULL) { coe=p1ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; ptrn=new node; if(start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } } } // End of subtraction int main() { clrscr(); polynomial p1,p2,sum,diff; cout<<"First Polynomial. "; p1.get_poly(); cout<<" Second polynomial. "; p2.get_poly(); clrscr(); cout<<" The First polynomial is: "; p1.show(); cout<<" The second polynomial is: "; p2.show(); cout<<" The sum of two polynomials is: "; sum.add(p1,p2); sum.show(); cout<<" The difference of two polynomials is: "; diff.subtract(p1,p2); diff.show(); getch(); return 0; }

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=835


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review