|
Anu.zip Size : 108.96 Kb Type : zip |
|
Mini Project.zip Size : 1313.446 Kb Type : zip |
|
Add and subtract two polynomials(Using Linked List).docx Size : 12.221 Kb Type : docx |
|
A Templated Stack Data Structure Example..docx Size : 11.867 Kb Type : docx |
Add and subtract two polynomials(Using Linked List)
#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;
}
A Templated Stack Data Structure Example.
#include <dos.h> // For sleep()
#include <iostream.h> // For I/0
#include <windows.h> // FOR MessageBox() API
#include <conio.h>
#define MAX 10 // MAXIMUM STACK CONTENT
template <class T> // Using Templates so that any type of data can be
// stored in Stack without multiple defination of class
class stack
{
protected:
T arr[MAX]; // Contains all the Data
public:
T item,r;
int top; //Contains location of Topmost Data pushed onto Stack
stack() //Constructor
{
for(int i=0;i<MAX;i++)
{
arr[i]=NULL; //Initialises all Stack Contents to NULL
}
top=-1; //Sets the Top Location to -1 indicating an empty stack
}
void push(T a) // Push ie. Add Value Function
{
top++; // increment to by 1
if(top<MAX)
{
arr[top]=a; //If Stack is Vacant store Value in Array
}
else // Bug the User
{
MessageBox(0,"STACK IS FULL","STACK WARNING!",MB_ICONSTOP);
top--;
}
}
T pop() // Delete Item. Returns the deleted item
{
if(top==-1)
{
MessageBox(0,"STACK IS EMPTY
","WARNING",MB_ICONSTOP);
return NULL;
}
else
{
T data=arr[top]; //Set Topmost Value in data
arr[top]=NULL; //Set Original Location to NULL
top--; // Decrement top by 1
return data; // Return deleted item
}
}
};
void main()
{
stack <int>a; // Create object of class a with int Template
int opt=1;
while (opt!=3)
{
clrscr();
cout<<" MAX STACK CAPACITY="<<((MAX-a.top)-1)<<"
";
cout<<"1) Push Item
";
cout<<"2) Pop Item
";
cout<<"3) Exit
";
cout<<"Option?";
cin>>opt;
switch(opt)
{
case 1:
cout<<"Which Number should be pushed?";
cin>>a.item;
a.push(a.item);
break;
case 2:
a.r=a.pop();
cout<<"Item popped from Stack is:"<<a.r<<endl;
sleep(2);
break;
}
}
}