Professional Documents
Culture Documents
Index: S.No Date Experiments NO Signature Object Oriented Programming
Index: S.No Date Experiments NO Signature Object Oriented Programming
PAGE
S.NO DATE EXPERIMENTS SIGNATURE
NO
OBJECT ORIENTED PROGRAMMING
// CONTROL STRUCTURES
#include<iostream.h>
#include<conio.h>
Void main ()
{
Clrscr();
Int n,I,j,k,l,m,a,b,c,ch;
Cout<<”enter the choice “;
Cout<<”1.Factorial\n 2.sum 3.print 4.greater than 3 numbers “;
Cin>>ch;
Switch(ch)
{
Case 1:
Cout<<”enter the number to be factorized”;
Cin>>n;
For(i=0;i<n;i++)
{
Int f=1;
F=f*i;
}
Cout<<”the factorial is”<<fact; break;
Case 2:
Cout<
<”enter the upper limit of the numbers “;
Cin>>j;
While(int k<=j)
{
Int sum =0;
Sum=sum+k;
K++;
}
Cout<<”the sum of the number is “<<sum;
Break;
Case 3:
Cout<<”enter the upper limit of the number”;
Cin>>l;
Do
{
M=0;
Cout<<m;
M++;
}while(m<=l);
Break;
Case 4:
Cout<<”enter the three numbers to be compared “;
Cin>>a>>b>>c;
If((a>b)&&(a>c))
{
Cout<<”A IS THE GREATEST”;
}
Else if ((b>a)&&(b>c))
{
Cout<<”B IS THE GREATEST”;
}
Else
{
Cout<<”C IS THE GREATEST”;
}
Break;
Default;
Cout<<invalid entry “;
}
Getch();
}
OUTPUT
1
Enter the number to be factorized
3
The factorial is 6
# include <iostream.h>
class student
{
int regno;
char name[30];
int m1,m2,m3;
int total;
float average;
public:
void getdata(void);
void putdata(void);
void calculate(void);
};
void student::getdata(void)
{
cout<<"Enter Name:";
cin>>name;
cout<<"Enter Regno:";
cin>>regno;
cout<<"Enter English, Tamil & Maths Marks:";
cin>>m1>>m2>>m3;
}
void student::calculate(void)
{
total=m1+m2+m3;
average=total/3;
}
void student::putdata(void)
{
cout<<"Name:"<<name<<"\n";
cout<<"Regno:"<<regno<<"\n";
cout<<"English:"<<m1<<"\n";
cout<<"Tamil:"<<m2<<"\n";
cout<<"Maths:"<<m3<<"\n";
cout<<"Total:"<<total<<"\n";
cout<<"Average:"<<average<<"\n";
}
int main()
{
student s[2];
for(int i=0;i<2;i++)
{
cout<<"Enter details of Student"<<i+1<<"\n";
s[i].getdata();s[i].calculate();
}
cout<<"\n";
for(i=0;i<2;i++)
{
cout<<"\nStudent"<<i+1<<"\n";
s[i].putdata();
}
return 0;
}
OUTPUT:
Enter details of Student 1
Enter Name: ARAVIND
Enter Regno: 5001
Enter English, Tamil & Maths Marks:
90
95
85
STUDENT 1
Name : ARAVIND
Regno : 5001
English: 90
Tamil : 95
Maths : 85
Total : 270
Average: 90
STUDENT 2
Name : RAM
Regno : 5010
English: 80
Tamil : 85
Maths : 90
Total : 255
Average: 85
#include<iostream.h>
# include <conio.h>
Int rev (int);
Void main ()
{
Clrscr();
Int n;
Cout<<”enter the number”;
Cin>>n;
Cout<<”the reversed number is “<<rev(n);
Getch();
}
Int rev (int x )
{
Int r,sum=0;
While(x>=0)
{
r=x%10;
sum=sum*19+r;
r=r/10;
}
return (sum);
}
OUTPUT
Enter the number
52
The reversed number is 25
// FRIEND FUNCTION
# include <iostream.h>
# include <conio.h>
class sample
{
int a;
int b;
public:
void setvalue(int x,int y)
{
a=x;
b=y;
}
friend swap(sample s);
};
int swap(sample s)
{
s.a=s.a+s.b;
s.b=s.a-s.b;
s.a=s.a-s.b;
cout<<" A = "<<s.a;
cout<<" B = "<<s.b;
return 0;
}
int main()
{
sample x;
clrscr();
int A,B;
cout<<"Enter A and B Value:"<<"\n";
cin>>A>>B;
x.setvalue(A,B);
swap(x);
getch();
return 0;
}
OUTPUT:
Enter A and B Value: 10 20
Before Swapping
A = 10
B = 20
After Swapping
A = 20
B = 10
// INLINE FUNCTIONS
#include<iostream.h>
#include<conio.h>
inline float cube(int a)
{
return(a*a*a);
}
int main()
{
int a;
clrscr();
cout<<"enter a value....";
cin>>a;
cout<<"the cube value is"<<cube(a);
getch();
return(0);
}
OUTPUT:
Enter Number: 5
Cube value is 12
//CONSTRUCTOR OVERLOADING
#include<iostream.h>
#include<conio.h>
class student
{
int m1,m2,total;
public:
student()
{
m1=0;
m2=0;
]
student(int x,int y )
{
m1=x;
m2=y;
total=m1+m2;
cout<<” the total is “ << total;
}
};
void main ()
{
int l ,m ;
clrscr();
cout<<”enter the two marks of the student\n “;
student s;
cin >>l>>m;
student(l,m);
getch();
}
OUTPUT
90 90
//CALL BY REFERNCE
#include<iostream.h>
#include<conio.h>
void swap ( int *,int*)
void main ()
{
int a,b;
clrscr();
cout<<”enter the two numbers”;
cin >>a>>b;
cout<<”the swapped numbers are”<<swap(&a,&b);
cout<<”original numbers are “<<a<<b;
getch();
}
void swap(int*x , int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
OUTPUT
Enter the two numbers 10 18
the swapped numbers are 18 10
the original numbers 18 10
//COPY CONSTRUCTOR
#include<iostream.h>
#include<conio.h>
class std
{
int m1,m2,total;
public:
std()
{
m1=95;
m2=93;
}
std( int p.int q)
{
m1=p;
m2=q;
}
void display ()
{
cout<<m1<<”\n”<<<m2;
}
void output()
{
total=m1+m2;
cout<<”\n”<<total;
}
};
int main ()
{
std s;
std v(100.97);
std c(v);
cout<<”the marks using default constructor”;
s.display();
cout<<”the marks using parametrized constructor”;
v.display();
cout<<”the marks using copy constructor”;
c.output();
return(0);
}
OUTPUT
the marks using default constructor 95 93
the marks using parametrized constructor 100 97
the marks using copy constructor 197
SINGLE INHERITANCE
#include<iostream.h>
#include<conio.h>
class std
{
protected:
int a;
public:
void detail( int x)
{
a=x;
}
};
class student1 : public std
{
int b;
public:
void detail(int y)
{
b=y;
}
void disp()
{
cout<<” first is “<<a<<”\nsecond is “<<b;
}
};
void main();
{
clrscr();
student1 obj;
obj.detail(10);
obj.detail(20);
obj.disp();
getch();
}
OUTPUT
First is 10
second is 20
HYBRID INHERITANCE
#include<iostream.h>
#include<conio.h>
class a
{
protected:
int rollno;
public:
void note (int x)
{
rollno=x;
cout<<rollno;
}
};
class b: public a
{
protected:
int m1,m2;
public:
void note1(int y,int z)
{
m1=y;
m2=z;
}
};
class c : public a
{
protected:
int total,sub1,sub2;
public:
void note2( int k, int l)
{
sub1=k;
sub2=l;
total=sub1+sub2;
}
};
class d : public b, public c
{
int avg :
public:
void disp ()
{
avg = total/2;
cout<<” the marks are “ <<m1<<m2;
cout<<”total”<<total;
cout<<”average”<<avg;
}
};
void main()
{
clrscr();
d d1;
b d2;
d2.note(3658);
d1.note1(98,94);
d1.note 2(88,89);
d1.disp();
getch();
}
}
OUTPUT
3658
the marks are 98 94
total 192
average 96
#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 20 //maximum no of elements in the list
enum boolean { true, false };
typedef enum boolean boolean;
class list
{
public:
int element;
int pos;
int length;
int list[MAX];
int menu(void);
void create(void);
void insert(int, int);
void delet(int);
void find(int);
void display(void);
boolean islistfull(void);
boolean islistempty(void);
};
int list :: menu()
{
int ch;
clrscr();
cout<<"\n\t\t********************************************\n";
cout<<"\t\t******LIST Implementation Using Arrays******\n";
cout<<"\t\t********************************************\n\n";
cout<<"\t1. Create\n\t2. Insert\n\t3. Delete\n\t4. Count\n\t5. Find\n\t6. Display\n\t7.
Exit\n\n\tEnter your choice : ";
cin>>ch;
cout<<"\n\n";
return ch;
}
void list :: create(void)
{
int element;
int flag=1;
while(flag==1)
{
cout<<"Enter an element : ";
cin>>element;
list[length] = element;
length++;
cout<<"To insert another element press '1' : ";
cin>>flag;
}
}
void list :: display(void)
{
int i;
for (i=0; i<length; i++)
cout<<i+1<<" Element : "<<list[i]<<"\n";
cout<<"Press any key to continue...";
getch();
}
void list :: insert(int element, int pos)
{
int i;
if (pos == 0)
{
cout<<"\n\nCannot insert at zeroth position";
getch();
return;
}
if (pos-1 > length)
{
cout<<"\n\nOnly "<<length<<" elements exit. Cannot insert at "<<pos<<"
postion";
cout<<"\nPress any key to continue...";
getch();
}
else
{
for (i=length; i>=pos-1; i--)
{
list[i+1] = list[i];
}
list[pos-1] = element;
length++;
}
}
void list :: delet(int pos)
{
int i;
if(pos == 0)
{
cout<<"\n\nCannot delete at zeroth position";
getch();
return;
}
if (pos > length)
{
cout<<"\n\nOnly "<<length<<" elements exit. Cannot delete "<<pos;
cout<<"\nPress any key to continue...";
getch();
return;
}
for (i=pos-1; i<length; i++)
{
list[i] = list[i+1];
}
length--;
}
void list :: find(int element)
{
int i;
int flag = 1;
for (i=0; i<length; i++)
{
if(list[i] == element)
{
cout<<element<<" exists at "<<i+1<<" position";
flag = 0;
cout<<"\nPress any key to continue...";
getch();
break;
}
}
if(flag == 1)
{
cout<<"Element not found.\nPress any key to continue...";
getch();
}
}
boolean list :: islistfull(void)
{
if (length == MAX)
return true;
else
return false;
}
boolean list :: islistempty(void)
{
if (length == 0)
return true;
else
return false;
}
void main()
{
list l;
int ch;
int element;
int pos;
l.length = 0;
while(1)
{
ch = l.menu();
switch (ch)
{
case 1:
l.length = 0;
l.create();
break;
case 2:
if (l.islistfull() != true)
{
cout<<"\tEnter the New element :";
cin>>l.element;
cout<<"\tEnter the Position : ";
cin>>l.pos;
l.insert(l.element, l.pos);
}
else
{
cout<<"\tList if Full. Cannot insert";
cout<<"\nPress any key to continue...";
getch();
}
break;
case 3:
if (l.islistempty() != true)
{
cout<<"Enter the position of element to be deleted:";
cin>>l.pos;
l.delet(l.pos);
}
else
{
cout<<"List is Empty.";
cout<<"\nPress any key to continue...";
getch();
}
break;
case 4:
cout<<"No of elements in the list is "<<l.length;
cout<<"\nPress any key to continue...";
getch();
break;
case 5:
cout<<"Enter the element to be searched : ";
cin>>element;
l.find(element);
break;
case 6:
l.display();
break;
case 7:
exit(0);
break;
default:
cout<<"Invalid Choice";
cout<<"\nPress any key to continue...";
getch();
}
}
}
OUTPUT:
1.insert
2.delete
3.display
4.find
5.exit
Enter your choice:1
1.insert
2.delete
3.display
4.find
5.exit
Enter your choice:1
1.insert
2.delete
3.display
4.find
5.exit
Enter your choice: 3
23 45
1.insert
2.delete
3.display
4.find
5.exit
Enter your choice: 4
#include <iostream.h>
#include<conio.h>
struct node
{ int data;
node *nxt;// Pointer to next node
};
node *start_ptr = NULL; //HEAD pointer
void insertlast()
{ node *temp= new node; // Temporary pointers
cout << " Enter data value U want to Insert: ";
cin >> temp->data;
temp->nxt = NULL;
void display()
{ node *temp;
temp = start_ptr;
cout << endl;
if (temp == NULL) //List Empty
cout << "The list is empty!" << endl;
else
{
while (temp != NULL)
{
cout << "["<<temp->data << " ]";
cout << endl;
temp = temp->nxt;
}
cout << "End of list!" << endl;
}
}
void deletefirst()
{ node *temp;
temp = start_ptr;
start_ptr = start_ptr->nxt;
delete temp;
}
void deletelast()
{ node *temp1, *temp2;
if (start_ptr == NULL)
cout << "The list is empty!" << endl;
else
{ temp1 = start_ptr;
if (temp1->nxt == NULL)
{ delete temp1;
start_ptr = NULL;
}
else
{ while (temp1->nxt != NULL)
{ temp2 = temp1;
temp1 = temp1->nxt;
}
delete temp1;
temp2->nxt = NULL;
}
}
}
void search()
{
int x;
node *temp;
temp=start_ptr;
cout<<"Enter element U want to search";
cin>>x;
if(temp!=NULL) //Non empty list
{
if(temp->data==x)
cout<<"\n\nthe element found in the list";
else
temp=temp->nxt;
}
if(temp->data!=x)
cout<<"\n\nelement not in the list";
}
void main()
{
clrscr();
int option;
start_ptr = NULL;
do
{
cout<<"\n\n****************\n\n";
display();
cout<<"\n\n****************\n\n";
cout << endl;
cout << "Please select an option : " << endl;
cout << "0. Exit the program." << endl;
cout << "1.INSERT FIRST." << endl;
cout << "2. INSERT LAST." << endl;
cout << "3. DELETE FIRST." << endl;
cout << "4. DELETE LAST." << endl;
cout << "5. COUNT." << endl;
cout << "6.SEARCH."<<endl;
struct node;
typedef int ptrtonode;
typedef struct node nodetype;
typedef ptrtonode position;
typedef ptrtonode list;
typedef int elementtype;
struct node
{
elementtype element;
position next;
};
struct node cursorspace[space]; // array object
void initializecursorspace()
{
int i;
for(i=0;i<space;i++)
cursorspace[i].next=i+1;
cursorspace[space-1].next=0;
}
void main()
{
int ch,x,y;
position p;
list l;
clrscr();
initializecursorspace();
l=createlist(l);
do
{
cout<<"\n CURSOR IMPLEMENTATION OF LIST ADT";
cout<<"\n*************************************";
cout<<"\n1.insert\n2.delete\n3.find\n4.printlist\n5.exit"<<endl;
cout<<"\n enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
if(cursorspace[0].next==0)
cout<<"cursorspace is full";
else
{
cout<<"\n enter the element to insert:";
cin>>y;
p=find(y,l);
insert(y,p,l);
cout<<"\n inserted";
}
break;
case 2:
cout<<"\n enter the element to be deleted";
cin>>x;
del(x,l);
break;
case 3:
if(isempty(1))
cout<<"list is empty";
else
{
cout<<"\n enter the element to be found:";
cin>>x;
p=find(x,l);
if(p!=1)
cout<<"\n element is found";
else
cout<<"\n element is not found";
}
break;
case 4:
cout<<"\n";
printlist(l);
break;
case 5:
exit(0);
}}
while(ch<5);
getch();
}
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#define MAX_SIZE 100
class Stack
{
private:
int arr[MAX_SIZE];
int top;
public:
Stack() { top=-1; }
void push(int);
int pop();
void display();
};
void Stack::push(int data)
{
if(top>=MAX_SIZE-1) cout<<"Stack Overflow";
else
{
top++;
arr[top]=data;
}
}
int Stack::pop()
{
if(top==-1)
{
cout<<"Stack Underflow\n";
return 0;
}
else
{
int x=arr[top];
top--;
return x;
}
}
void Stack::display()
{
cout<<"The Stack is :\n";
for(int i=top;i>=0;i--)
{
cout<<arr[i]<<"\n";
}
}
void main()
{
clrscr();
int choice;
Stack stack;
cout<<"1: Push element\n";
cout<<"2: Pop element\n";
cout<<"3: Display Stack\n";
cout<<"0: Exit\n";
while(1)
{
cout<<"\nEnter your choice:";
cin>>choice;
switch(choice)
{
case 1:
int data;
cout<<"Enter the element to be pushed: ";
cin>>data;
stack.push(data);
break;
case 2:
cout<<"The Popped element is: " << stack.pop();
break;
case 3:
stack.display();
break;
case 0:
exit(0);
}
}
}
OUTPUT:
1: Push element
2: Pop element
3: Display Stack
0: Exit
#include<iostream.h>
#include<conio.h>
#include<process.h>
class stack
{
public:
float data;
stack *next;
stack(float &ele,stack *nextval)
{
data=ele;
next=nextval;
}
stack(stack *nextval)
{
next=nextval;
}
};
class list
{
private:
stack *top;
public:
list();
~list();
void push();
void pop();
void currval();
void display();
};
list::list()
{
top=new stack(NULL);
}
list::~list() {}
void list::push()
{
float data;
cout<<"\nEnter the data\n";
cin>>data;
top=new stack(data,top);
}
void list::pop()
{
if(top->next!=NULL)
{
stack *peer=top;
cout<<"\n"<<top->data;
top=top->next;
delete peer;
}
else
cout<<"\n Stack is Empty\n";
}
void list::display()
{
stack *top1=top;
if(top1->next!=NULL)
{
while(top1->next!=NULL)
{
cout<<top1->data<<"\t";
top1=top1->next;
}
}
else
cout<<"\nStack is Empty\n";
}
void list::currval()
{
if(top!=NULL)
cout<<top->data;
else
cout<<"\n Stack is empt\n";
}
main()
{
clrscr();
int ch;
cout<<"\nSTACK USING LIST\n";
list s;
while(1)
{
cout<<"\n";
for(int i=0;i<50;i++)
cout<<"-";
cout<<"\n1.push\n2.pop\n3.currval\n4.display\n5.exit\n";
for(i=0;i<50;i++)
cout<<"-";
cout<<"\nEnter your choice\n";
cin>>ch;
switch(ch)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.currval();
break;
case 4:
s.display();
break;
case 5:
exit(0);
}
}
}
OUTPUT:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
const int maxsize=100;
class queue
{
private:
int front;
int rear;
int count;
int qlist[maxsize];
public:
queue();
Enqueue(int x);
int Dequeue();
int display();
};
queue::queue()
{
front=0;
rear=0;
count=0;
}
queue::Enqueue(int x)
{
if(count==maxsize)
{
cout<<"QUEUE IS FULL"<<endl;
exit(1);
}
qlist[rear]=x;
count++;
rear=(rear+1)%maxsize;
return 0;
}
int queue::Dequeue()
{
if(count==0)
{
cout<<"DELETING AN EMPTY QUEUE"<<endl;
exit(1);
}
int temp;
temp=qlist[front];
count--;
front=(front+1)%maxsize;
return temp;
}
int queue::display()
{
int f = front;
int r = rear;
cout<<"The Elements in the Queue: "<<endl;
while(f <= r-1)
{
cout<<qlist[f]<<endl;
f++;
}
return 0;
}
queue q;
int main()
{
int choice;
clrscr();
cout<<"----------------------------------------------"<<endl;
cout<<"1.Insert"<<endl<<"2.Remove"<<endl<<"3.Display"<<endl<<"0.Exit"<<endl;
while(choice!=0)
{
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1: cout<<"enter data"<<endl;
int y;
cin>>y;
q.Enqueue(y);
break;
case 2: int z;
z=q.Dequeue();
cout<<z<<endl;
break;
case 3: q.display();
break;
case 0: break;
}
}
return 0;
}
OUTPUT:
1.Insert
2.Remove
3.Display
0.Exit
Enter your choice: 1
enter data
34
Enter your choice: 1
enter data
78
Enter your choice: 1
enter data
90
Enter your choice: 3
The Elements in the Queue:
34
78
90
Enter your choice: 2
34
Enter your choice: 3
The Elements in the Queue:
78
90
Enter your choice: 0
#include<iostream.h>
#include<process.h>
#include<conio.h>
class node
{
public:
float data;
node *next;
node (float data1,node *next1)
{
Data = data1;
Next =next1;
}
};
class queue
{
node *rear, *front;
public:
queue();
~queue();
void enqueue();
void display();
void dequeue();
};
queue::queue()
{
front=rear=NULL;
}
queue::~queue(){}
void queue::enqueue()
{
int element;
cout<<"Enter Element\n";
cin>>element;
if(front==NULL)
front = rear = new node(element , NULL);
else
rear = rear->next=new node(element , NULL);
}
void queue::display()
{
if(front!=NULL)
{
node *te=front;
while(te!=NULL)
{
cout<<"\t"<<te->data;
te = te->next;
}
}
else
cout<<"\mQueue is Empty\n";
}
void queue::dequeue()
{
if(front!=NULL)
{
display();
cout<<"\nDequeue data is:"<<front->data;
front=front->next;
}
else
cout<<"\nQueue is Empty\n";
}
void main()
{
clrscr();
cout<<"\nQUEUE USING LINKED LIST\n";
int ch;
queue q;
while(1)
{
cout<<"\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n";
cout<<"Enter ur choice:";
cin>>ch;
switch(ch)
{
case 1:
q.enqueue();
break;
case 2:
q.dequeue();
break;
case 3:
cout<<"\nElements in Queue\n";
q.display(); break;
case 4: exit(0);
}
}
}
OUTPUT:
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter ur choice:1
Enter Element
9
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter ur choice:1
Enter Element
4
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter ur choice:3
Elements in Queue
12 9 4
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter ur choice:2
12 9 4
Dequeue data is:12
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter ur choice:4
# include <stdio.h>
# include <conio.h>
# include <ctype.h>
# include <iostream.h>
class infixpos
{
char infix[20], stk[20], p[20], item;
int top, i, k;
public:
int pre(char ch);
void ConvertExp();
void getExp();
void display();
};
void infixpos::getExp()
{
top=k=0;
cout<<"EVALUATION OF EXPRESSION USING STACKS";
cout<<"\n\nENTER EXPRESSION IN INFIX FORM(Enter '.' at End:";
cout<<"\n\nEnter INFIX EXPRESSION:";
cin>>infix;
}
void infixpos::display()
{
cout<<"\n\nPOSTFIX EXPRESSION:";
for(i=0;p[i]!='.';i++)
cout<<p[i];
}
void infixpos::ConvertExp()
{
for(i=0;infix[i]!='.';i++)
{
item=infix[i];
if(isalpha(item))
{
p[k]=item;
k++;
}
else if(item=='(')
{
top++;
stk[top]=item;
}
else if(item==')')
{
while(stk[top]!='(')
{
p[k]=stk[top];
k++;
top--;
}
top--;
}
else
{
if(top==0)
{
top++;
stk[top]=item;
}
else
{
while((top!=0) && (pre(stk[top])>=pre(item)))
{
p[k]=stk[top];
k++;
top--;
}
if((top==0) || pre(stk[top])<pre(item))
{
top++;
stk[top]=item;
}
}
}
}
while(top>0)
{
p[k]=stk[top];
top--;
k++;
}
p[k]='.';
}
int infixpos::pre(char ch)
{
int j;
switch(ch)
{
case '*': j=4; break;
case '/': j=4; break;
case '+': j=3; break;
case '-': j=3; break;
case '(': j=2; break;
case ')': j=1; break;
}
return(j);
}
void main()
{
clrscr();
infixpos in;
in.getExp();
in.ConvertExp();
in.display();
getch();
}
OUTPUT:
# include <iostream.h>
# include <stdio.h>
# include <stdlib.h>
# include <conio.h>
# include <ctype.h>
# include <string.h>
int top=-1;
int stack[20];
char expr[20];
class Evaluation
{
public:
void push(int);
int pop();
void eval();
};
void Evaluation::push(int ele)
{
++top;
stack[top]=ele;
}
int Evaluation::pop()
{
int temp;
temp=stack[top];
top--;
return temp;
}
void Evaluation::eval()
{
int i,x,y,res;
char n_token[2];
for(i=0;expr[i]!='\0';i++)
{
n_token[0]=expr[i];
if(isdigit(n_token[0]))
{
strcat(n_token,"\0");
x=atoi(n_token);
push(x);
}
else if(n_token[0]=='+')
{
y=pop();
x=pop();
res=x+y;
push(res);
}
else if(n_token[0]=='-')
{
y=pop();
x=pop();
res=x-y;
push(res);
}
else if(n_token[0]=='*')
{
y=pop();
x=pop();
res=x*y;
push(res);
}
else if(n_token[0]=='/')
{
y=pop();
x=pop();
res=x/y;
push(res);
}
}
}
void main()
{
clrscr();
Evaluation E;
cout<<"Enter the postfix notation:";
gets(expr);
E.eval();
cout<<"RESULT : "<<stack[top];
getch();
}
OUTPUT:
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdlib.h>
enum bool{ false,true };
class TreeNode
{
friend class BSTree;
private:
TreeNode *lchild,*rchild;
int data;
};
class BSTree
{
int lm,rm,l;
TreeNode* tree;
void display(struct TreeNode*,int lm,int rm,int l);
void InOrder(TreeNode *t);
void PreOrder(TreeNode *t);
void PostOrder(TreeNode *t);
public:
BSTree()
{
tree=NULL;
lm=1,rm=80,l=1;
}
~BSTree()
{
delete tree;
}
TreeNode* Search(int x);
void Insert(int x);
void Traverse();
void printTree();
};
void BSTree::Insert(int x)
{
bool notfound=true;
TreeNode *p=tree,*q;
while((p) && (notfound))
{
q=p;
if(x==(p->data)) notfound=false;
else if (x<(p->data))p=(p->lchild);
else p=(p->rchild);
}
if(notfound)
{
p=new TreeNode;
p->lchild=NULL;p->rchild=NULL;p->data=x;
if(tree)
{
if(x<(q->data))
q->lchild=p;
else
q->rchild=p;
}
else
tree =p;
}
}
void BSTree::PreOrder(TreeNode* temp)
{
if(temp)
{
cout<<temp->data<<" ";
PreOrder(temp->lchild);
PreOrder(temp->rchild);
}
}
void BSTree::InOrder(TreeNode* temp)
{
if(temp)
{
InOrder(temp->lchild);
cout<<temp->data<<" ";
InOrder(temp->rchild);
}
}
void BSTree::PostOrder(TreeNode* temp)
{
if(temp)
{
PostOrder(temp->lchild);
PostOrder(temp->rchild);
cout<<temp->data<<" ";
}
}
void BSTree::display(struct TreeNode* t,int lm,int rm,int l)
{
int x=0;
if(t != NULL)
{
x=(lm+rm)/2;
gotoxy(x,4*l);
cout<<setw(2)<<t->data;
display(t->lchild,lm,x,l+1);
display(t->rchild,x,rm,l+1);
}
}
void BSTree::printTree() { this->display(tree,lm,rm,l); }
void BSTree::Traverse()
{
cout<<"\nPreOrder Traversal of Tree:";this->PreOrder(tree);
cout<<"\nInOrder Traversal of Tree:";this->InOrder(tree);
cout<<"\nPostOrder Traversal of Tree:";this->PostOrder(tree);
}
void main()
{
BSTree obj;
clrscr();
int opt,e;
do
{
cout<<"\n1.Insert Into BST\n2.Traverse\n3.Display\n4.Exit\n\tEnter the Option: ";
cin>>opt;
switch(opt)
{
case 1:
do
{ cout<<"\nEnter The Element to insert(0 to Terminate): ";
cin>>e;
if(!e)break;
obj.Insert(e);
}while(1);
break;
case 2:
obj.Traverse();
break;
case 3: clrscr();
obj.printTree(); break;
case 4: exit(0);
default:cout<<"\nEnter the Correct Option."<<endl;
}
}while(1);
}
OUTPUT:
12 67
9 56
#include<iostream.h>
#include<conio.h>
class sortq
{
int a[50];
public:
void quicksort(int a[],int n,int l,int u);
void partition(int a[],int beg,int end,int *loc);
};
void main()
{
int n;
int a[10];
sortq t;
cout<<"\n\n\t\tQUICK SORT";
cout<<"\n\t\t~~~~~~~~~~~~~\n";
cout<<"The Number of Elements:";
cin>>n;
cout<<"\n The individual Elements of the array is:\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
cout<<"\n"<<"\n";
}
t.quicksort(a,n,0,n-1);
cout<<"\nThe sorted Elements of the array is:";
for(i=0;i<n;i++)
{
cout<<"\n"<<a[i]<<"\n";
}
getch();
}
void sortq::quicksort(int a[],int n,int l,int u)
{
int loc;
if(l<u)
{
partition(a,l,u,&loc);
quicksort(a,n,l,loc-1);
quicksort(a,n,loc+1,u);
}
}
void sortq::partition(int a[],int beg,int end,int *loc)
{
int first;
int last,flag,temp;
*loc=first=beg;
last=end;
flag=0;
while(!flag)
{
while(a[last]>=a[*loc]&&(*loc!=last))
last--;
if(*loc==last)
flag=1;
else
{
if(a[*loc]>a[last])
{
temp=a[*loc];
a[*loc]=a[last];
a[last]=temp;
*loc=last;
}
}
if(!flag)
{
while((a[first]<=a[*loc])&&(*loc!=first))
first++;
if(*loc==first)
flag=1;
else
{
if(a[*loc]<a[first])
{
temp=a[*loc];
a[*loc]=a[first];
a[first]=temp;
*loc=first;
}
}
}
}
}
OUTPUT:
QUICK SORT
~~~~~~~~~~~~~
The Number of Elements:5
// HEAP SORT
# include <iostream.h>
# include <conio.h>
# include <stdlib.h>
class HeapSort
{
public:
void heap(int a[],int n);
void create_heap(int a[],int n);
};
void main()
{
int a[50],i,n;
HeapSort hs;
clrscr();
cout<<"Enter the Limit:";
cin>>n;
cout<<"\nEnter the elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
hs.heap(a,n);
cout<<"\nThe sorted list is:\n";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
getch();
}
void HeapSort::create_heap(int a[],int n)
{
int i,j,q,key;
for(q=1;q<n;q++)
{
i=q;
key=a[q];
j=(int)(i/2);
while((i>0)&&(key>a[j]))
{
a[i]=a[j];
i=j;
j=(int)(i/2);
if(j<0)
j=0;
}
a[i]=key;
}
}
void HeapSort::heap(int a[],int n)
{
int i,j,q,key,temp;
create_heap(a,n);
for(q=n-1;q>=1;q--)
{
temp=a[0];
a[0]=a[q];
a[q]=temp;
i=0;
key=a[0];
j=1;
if((j+1)<q)
if(a[j+1]>a[j])
a=a+1;
while((j<=q-1)&&(a[j]>key))
{
a[i]=a[j];
i=j;
j=2*i;
if(j+1<q)
if(a[j+1]>a[i])
j=j+1;
else
if(j>n-1)
j=n-1;
a[i]=key;
}
}
}
OUTPUT: