You are on page 1of 55

/*1.

Given that an employee class contains the following members Data members:
Employee_no, Employee_name,Basic,DA,IT,Net_sal Member functions:to read the data, to calculate
Net_sal and to print the data members.Write c++ program to read the data of N employees and
computeNet_sal of each employee(DA=52% of basic and IT=30% of gross salarty*/

#include<iostream.h>
#include<conio.h>
class employee
{
public:
int emp_num;
float basic,net_sal,it,da,gross;
char emp_name[10];

void read()
{
cout<<"enter the name of the employee\n";
cin>>emp_name;
cout<<"enter the employee number"<<endl;
cin>>emp_num;
out<<"enter the basic salary of the emplyee"<<endl;
cin>>basic;
}

void cal_net()
{
da=0.52*basic;
gross=basic+da;
it=0.3*gross;
net_sal=basic+da-it;
}

void print()
{
cout<<"------------------------\nName of the employee is "
<<emp_name<<endl;
cout<<"employee number is "<<emp_num<<endl;
cout<<"the basic is " <<basic<<endl;
cout<<"the DA is " <<da<<endl;
cout<<"the GROSS is " <<gross<<endl;
cout<<"the IT is " <<it<<endl;
cout<<"the net sal is “ <<net_sal<<endl;
}

};
void main()
{
employee emp[100];
int n;
clrscr();
cout<<"enter number of employees"<<endl;
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<endl<<"enter the details of employee"<<i<<endl;
emp[i].read();
emp[i].cal_net();

}
for(i=1;i<=n;i++)
{
cout<<"\nDetails of employee "<<emp[i].emp_name<<endl;
emp[i].print();
}
getch();
}

OUTPUT

enter number of employees


2

enter the details of employee1


enter the name of the employee
abc
enter the employee number
123
enter the basic salary of the emplyee
8000

enter the details of employee2


enter the name of the employee
xyz
enter the employee number
234
enter the basic salary of the emplyee
8550
Details of employee abc
------------------------
Name of the employee is abc
employee number is 123
the basic is 8000
the DA is 4160
the GROSS is 12160
the IT is 3648
the net sal is 8512

Details of employee xyz


------------------------
Name of the employee is xyz
employee number is 234
the basic is 8550
the DA is 4446
the GROSS is 12996
the IT is 3898.8
the net sal is 9097.2
/*2. Define a STUDENT class with USN,name and marks in three tests of a subject.
Declare an 2.Define a STUDENT class
with USN,name and marks in three tests of a subject. Declare an array of 10
STUDENT objects.use appropriate functions; find the average of two better
marks for each student. Print the USN,name and the average marks of
all students.*/

#include<iostream.h>
#include<conio.h>
class STUDENT
{
public:
int usn;
char name[20];
float mark1,mark2,mark3,avg;

public:
void read();
void average();
void print();

};

void STUDENT::read()
{
cout<<"enter the name of the student\n";
cin>>name;
cout<<"enter the USN number of the student\n";
cin>>usn;
cout<<"enter mark1\n";
cin>>mark1;
cout<<"enter mark2\n";
cin>>mark2;
cout<<"enter mark3\n";
cin>>mark3;
}

void STUDENT::average()
{
int least;
least=mark1;
if(mark2<least)
least=mark2;
if(mark3<least)
least=mark3;
avg=(mark1+mark2+mark3-least)/2;
cout<<avg;
}
void STUDENT::print()
{
cout<<"the name is "<<name<<endl;
cout<<"USN numberis "<<usn<<endl;
cout<<"mark1= "<<mark1;
cout<<"\nmark2="<<mark2;
cout<<"\nmark3="<<mark3;
cout<<"\naverage of two better marks is ";
average();
}

void main()
{
STUDENT stud[10];
int n;
clrscr();

cout<<"enter number of students \n";


cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"\nenter the details of student"<<i<<endl;
stud[i].read();
}
for(i=1;i<=n;i++)
{
cout<<"\n\ndetails of "<<stud[i].name<<endl;
stud[i].print();
}
}

Output

enter number of students


2

enter the details of student1


enter the name of the student
abc
enter the USN number of the student
123
enter mark1
23
enter mark2
24
enter mark3
23

enter the details of student2


enter the name of the student
xyz
enter the USN number of the student
345
enter mark1
24
enter mark2
25
enter mark3
25

details of abc
the name is abc
USN numberis 123
mark1= 23
mark2=24
mark3=23
average of two better marks is 23.5

details of xyz
the name is xyz
USN numberis 345
mark1= 24
mark2=25
mark3=25
average of two better marks is 25
/*3 Write a C++ program to create a class called COMPLEX and implement the following overloading
functions ADD that return a COMPLEX number.
(1)ADD(a,s2)-where a is an integer(real part) and s2 is a complex number.
(2)ADD(s1,s2)-where s1 and s2 are complex numbers.*/

# include<conio.h>
# include<iostream.h>
# include<stdlib.h>

class complex
{
private: float real;
float img;
public:
void read()
{
cout<<"\nEnter the real part: ";
cin>>real;
cout<<"\nEnter the imaginary part: ";
cin>>img;
}
void display()
{
if(img>=0)
cout<<real<<" +"<<img<<"i"<<endl;
else
cout<<real<<img<<"i"<<endl;
}
complex add(float r, complex c1);
complex add(complex c1, complex c2);
};

complex complex::add(float r, complex c1)


{
complex t;
t.real = r + c1.real;
t.img = c1.img;
return t;
}

complex complex::add(complex c1, complex c2)


{
complex t;
t.real = c1.real + c2.real;
t.img = c1.img + c2.img;
return t;
}
void main()
{
complex c1,c2,c3,c4;
float n;
cout<<"enter first complex number"<<endl;
c1.read();
cout<<"\nenter second complex number"<<endl;
c2.read();
cout<<"\nfirst complex number is"<<endl;
c1.display();
cout<<"secoond complex number is"<<endl;
c2.display();
cout<<"addition of two complex number is\n";
c3=c3.add(c1,c2);
c3.display();
cout<<"\nenter a float/int to be added\n";
cin>>n;
c4=c4.add(n,c1);
cout<<"after adding "<<n<<" to first complex number value is"<<endl;
c4.display();
}

OUTPUT
enter first complex number

Enter the real part: 2

Enter the imaginary part: 3

enter second complex number

Enter the real part: 2

Enter the imaginary part: -4

first complex number is


2 +3i
secoond complex number is
2-4i
addition of two complex number is
4-1i

enter a float/int to be added


3
after adding 3 to first complex number value is
5 +3i
/*4.Write a C++ program to create a class called LIST(linked list) with member
functions to insert an
element at the front as well as to delete an element from the front of the
list.Demonstrate all the functions after creating a list object*/

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct NODE
{
int info;
NODE *link;
};

class LIST
{
public:

NODE *first;
LIST()
{
first=NULL;
}
void insert();
void del();
void display();
};

void LIST::insert()
{

NODE *temp=new NODE;

cout<<"enter the element to be inserted\n";


cin>>temp->info;
temp->link=first;
first=temp;
}

void LIST::del()
{

if(first==NULL)
{
cout<<"no elements to delete";
return;
}
NODE *temp=first;
first=first->link;
cout<<endl<<"element deleted is "<<temp->info;
delete temp;

void LIST::display()
{
NODE *temp;
if(first==NULL)
{
cout<<"no elements to display";
return;
}
cout<<"elements are\n";
for(temp=first;temp!=NULL;temp=temp->link)
cout<<temp->info<<endl;

void main()
{
LIST L;
int ch;
clrscr();

for(;;)
{
cout<<"\n1 - insert\n2 - delete\n3 - display\n4- EXIT\nenter your choice \n";
cin>>ch;
switch(ch)
{
case 1:L.insert(); break;
case 2:L.del(); break;
case 3:L.display(); break;
case 4: cout<<"program terminated"; exit(0);
}
}

getch();
}
OUTPUT

1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
1
enter the element to be inserted
23

1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
1
enter the element to be inserted
34

1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
3
elements are
34
23

1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
2

element deleted is 34
1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
2

element deleted is 23
1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
2
no elements to delete
1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
3
no elements to display
1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
4
program terminated
/*5.Write a C++ program to create a template function for Quicksort and demonstrate sorting of
integers and doubles*/

#include<iostream.h>
#include<stdlib.h>

template <class T>


void quicksort(T a[],int low, int high)
{
int pos;
if(low<high)
{
pos = partition(a,low,high);
quicksort(a,low,pos-1);
quicksort(a,pos+1,high);
}
}

template <class T>


int partition(T a[],int low, int high)
{
T key, temp;
int i = low+1;;
int j = high;
key = a[low];

while(i<=j)
{
while(key>=a[i])
{
i++;
}

while(key<a[j])
{
j--;
}

if(i<j) temp=a[i],a[i]=a[j],a[j]=temp;
}

temp=a[j],a[j]=a[low],a[low]=temp;
return j;

}
template <class T>
void sort(T a[])
{
int n;
cout<<"enter number of elements"<<endl;
cin>>n;

cout<<"enter "<<n<<" elements"<<endl;


for(int i=0;i<n;i++)
{

cin>>a[i];
}

quicksort(a,0,n-1);
cout<<"the sorted array is "<<endl;

for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}

void main()
{
int a[10];
cout<<"Integer array sorting"<<endl;
sort(a);

double b[10];
cout<<"double array sorting"<<endl;
sort(b);
}
OUTPUT

Integer array sorting


enter number of elements
5
enter 5 elements
2
1
5
6
7
the sorted array is
1
2
5
6
7
double array sorting
enter number of elements
4
enter 4 elements
2.1
4.1
3.2
1.1
the sorted array is
1.1
2.1
3.2
4.1
/*6.*Write a C++ program to create a class called STACK using an array of integers.Implement the
following operations by overloading the operators + and -
(1)s1=s1 + element;where s1 is an object of the class STACK and element is an
integer to be pushed on the top of the stack.
(2)s1=s1 -;where s1 is an object of the class STACK and the - operator pops the element.
Handle the STACK empty and STACK full condition.Also display the contents
of the stack after each operation,by overloading the operator <<.*/

# include<iostream.h>
# include<conio.h>
# include<stdlib.h>
# define SIZE 5

class stack
{
private:
int stk[SIZE];
int top;
public:
stack()
{
top=-1;
}
friend stack operator +(stack s,int x);
friend stack operator -(stack s);
friend ostream & operator<<(ostream &dout, stack &s);
};

stack operator +(stack s, int x)


{
if(s.top==SIZE-1)
{
cout<<"\nStack overflows on insertion\n";

}
else
{
s.stk[++s.top] = x;
}
return s;
}

stack operator -(stack s)


{
if(s.top == -1)
{
cout<<"\nStack underflows on deletion\n";
}
else
{
int temp = s.stk[s.top--];
cout<<"\nDeleted element is "<< temp<<endl;
}
return s;
}

ostream & operator<<(ostream &dout, stack &s)


{

if(s.top==-1)
{
cout<<"\nStack is empty\n";
}
else
{
cout<<"\ncontents of the stack \n";
for(int i=s.top;i>=0;i--)
cout<<s.stk[i]<<endl;
}
return (dout);
}

void main()
{
int opt;
stack s;

for(;;)
{
cout<<"\n1:-Insertion";
cout<<"\n2:-Deletion";
cout<<"\n3:-Display";
cout<<"\n4:-Exit";
cout<<"\nEnter your choice ";
cin>>opt;
switch(opt)
{
case 1: cout<<"\nEnter an element ";
int x;
cin>>x;
s = s + x;
break;
case 2: s = -s;
break;
case 3: cout<<s;
break;
case 4: cout<<"\nProgram Terminated";
exit(0);
}

}
getch();
}

OUTPUT

1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 1

Enter an element 23

1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 1

Enter an element 34

1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 3

contents of the stack


34
23

1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 2

Deleted element is 34

1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 3

contents of the stack


23

1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 2

Deleted element is 23

1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 2

Stack underflows on deletion

1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 4

Program Terminated
/*7. Write a C++ program to create a class called DATE.Accept two valid dates in the form
dd/mm/yy.Implement the following operations by overloading the operators + and - .After every
operation display the results by overloadingthe operator<<
i) no_of_days=d1-d2;where d1 and d2 are DATE objects,d1>=d2 and no_of_days
is an interger.
ii)d2=d1+no_of_days;where d1 is a DATE object and no_of_days is an integer.*/

#include<iostream.h>
#include<conio.h>
class date
{
private:
int no_days,mon,month[13],day,year;
public:
date();
void read();
int operator-(date);
date operator+(int);
friend ostream& operator<<(ostream &,date);
int operator>=(date);
};

date::date()
{
month[1]=month[3]=month[5]=month[7]=month[8]=month[10]=month[12]=31;
month[4]=month[6]=month[9]=month[11]=30;
month[2]=28;
no_days=0;
}

int date::operator>=(date d2)


{

if((year>d2.year)||(year==d2.year&&mon>d2.mon)||(year==d2.year&&mon==d2.mon&&day>d2.day))

return 1;
return 0;

void date::read()
{
cout<<"enter the date in (dd/mm/yyyy)format\n";
cin>>day;
cin>>mon;
cin>>year;
}
int date::operator-(date d2)
{

while(year!=d2.year||mon!=d2.mon||day!=d2.day)
{
no_days++;
d2.day++;

if((d2.year%4==0&&d2.mon==2)?d2.day>29:d2.day>month[d2.mon])
{
d2.day=1;
d2.mon++;
if(d2.mon>12)
{
d2.mon=1;
d2.year++;
}
}
}
return no_days;
}

date date::operator+(int d)
{
for(int i=0;i<d;i++)
{
day++;
if((year%4==0&&mon==2)?day>29:day>month[mon])
{
day=1;
mon++;
if(mon>12)
{
mon=1;
year++;
}
}
}
return *this;

}
ostream& operator<<(ostream &out,date p)
{
out<<p.day<<"/"<<p.mon<<"/"<<p.year;
return out;
}

void main()

date d1,d2;
int days;
clrscr();
cout<<"\n 1st date"<<endl;
d1.read();
cout<<"\n 2nd date"<<endl;
d2.read();

if(d1>=d2)
{
days=d1-d2;
cout<<"\ndays is "<<days;
}
else
{
cout<<"subtraction is not possible";
}
cout<<"\nenter the number of days to be added\n";
cin>>days;
d2=d1+days;
cout<<"d2=d1+"<<days<<endl;
cout<<d2;
getch();
}
OUTPUT

1st date
enter the date in (dd/mm/yyyy)format
23
02
2009

2nd date
enter the date in (dd/mm/yyyy)format
23
02
2008

days is 366
enter the number of days to be added
23
d2=d1+23
18/3/2009
/*8.Write a C++ program to create a class called MATRIX using a two dimensional
array of integers.Implement the following operations by overloading the
operator == which checks the compatibility of the two matrices to be added and
subtracted.Perform the addition and subtraction by overloading the operators +
and - respectively.Display the results by overloading the operator <<*/

# include<iostream.h>
# include<conio.h>
# include<stdlib.h>

class matrix
{
private:
int a[10][10];
int row,col;
public:
matrix(){}
matrix(int i,int j)
{
row=i;
col=j;
}
void getdata();
friend int operator ==(matrix m1,matrix m2);
matrix operator +(matrix m);
matrix operator -(matrix m);
friend ostream &operator<<(ostream &dout, matrix &m);
};

void matrix :: getdata()


{
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>a[i][j];
}

int operator ==(matrix m1, matrix m2)


{
if(m1.row==m2.row && m1.col==m2.col)
return(1);
return(0);
}

matrix matrix :: operator +(matrix m2)


{
matrix M3(row,col);
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
M3.a[i][j] = a[i][j] + m2.a[i][j];
return M3;
}

matrix matrix :: operator -(matrix m2)


{
matrix M4(row,col);

for(int i=0; i<row; i++)


for(int j=0; j<col; j++)
M4.a[i][j] = a[i][j] - m2.a[i][j];
return M4;
}

ostream & operator<<(ostream &dout, matrix &m1)


{
for(int i=0;i<m1.row;i++)
{
for(int j=0;j<m1.col;j++)
dout<<m1.a[i][j]<<" ";
dout<<endl;
}
return (dout);
}

void main()
{

int m,n,p,q;
clrscr();

cout<<"enter the order of 1st matrix\n";


cin>>m>>n;
cout<<"enter the elements of 1st matrix\n";
matrix m1(m,n);
m1.getdata();
cout<<"enter the order of 2nd matrix\n";
cin>>p>>q;
cout<<"enter the elements of second matrix\n";
matrix m2(p,q);
m2.getdata();
cout<<"the first matrix is\n"<<m1;
cout<<"the second matrix is \n"<<m2;
if(m1==m2)
{
matrix m3,m4;
m3=m1+m2;
m4=m1-m2;
cout<<"sum of the matrix is\n"<<m3;
cout<<"differnce of the matrix is \n"<<m4;
}
else
{
cout<<"addition and subtraction of matrices is not possible";
}
getch();
}

enter the order of 1st matrix


2
2
enter the elements of 1st matrix
2
2
2
2
enter the order of 2nd matrix
2
2
enter the elements of second matrix
3
3
3
3
the first matrix is
22
22
the second matrix is
33
33
sum of the matrix is
55
55
differnce of the matrix is
-1 -1
-1 -1
/*9Write a C++ program to create a class OCTAL,which has the characteristics of octal number and
overloaded operator +.
i.Octal h=x,x is an integer.
ii.int y=h+k where h is an octal object and k is an integer.*/

#include<iostream.h>
#include<conio.h>

class octal
{
int oct,dec;
public:
octal()
{
oct=0;
dec=0;
}
int operator+(int);
octal(int);
friend ostream & operator<<(ostream&,octal);
};

int octal::operator+(int x)
{
return(dec+x);

ostream &operator<<(ostream &dout,octal c)


{
dout<<c.oct;
return dout;
}

octal::octal(int n)
{
dec=n;

int sum=0,p=1,r,temp;
temp=dec;
while(temp>0)
{
r=temp%8;
sum=sum+(p*r);
p=p*10;
temp=temp/8;
}

oct=sum;

void main()
{

int k,x;
clrscr();
cout<<"Enter the decimal number \n";
cin>>x;
octal ol=x;
cout<<"the ocatl equivalent is\n"<<ol;
cout<<endl;

cout<<"\nEnter the number to be added\n";


cin>>k;

int y=ol+k;
ol=y;
cout<<"\nThe octal equivalent is \n"<<ol;
cout<<endl<<"The decimal equivalent is \n"<<y;
getch();
}

OUTPUT
Enter the decimal number
23
the ocatl equivalent is
27

Enter the number to be added


3

The octal equivalent is


32
The decimal equivalent is
26
/*10.Write a C++ program to create a class called QUEUE with member functions to
add an element and to delete an element from the queue.Using these member
functions,implement a queue of integer anddouble.Demonstrate the operations
by displaying the content of the queue after every operation*/

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define size 10

template <class Q>


class Queue
{
Q que[size];
int front, rear;
public:
Queue()
{
front = 0;
rear = -1;
}
void Insert(Q ele);
void Delete();
void Display();
};

template <class Q>


void Queue <Q> :: Insert(Q ele)
{
if(rear == size - 1)
cout<<"\nQueue overflow\n";
else
{
que[++rear] = ele;
}
}

template <class Q>


void Queue <Q> :: Delete()
{
if(rear == -1)
cout<<"\nQueue underflow\n";
else
{
if(front == rear)
{
cout<<"\n"<<que[front]<<" deleted\n";
front = 0;
rear = -1;
}
else
{
cout<<"\n"<<que[front]<<" deleted\n";
front++;
}
}
}

template <class Q>


void Queue <Q> :: Display()
{
if(rear == -1)
cout<<"\nQueue empty\n";
else
{
cout<<"\nThe elements of the queue are: \n";
for(int i = front; i <= rear; i++)
cout<<que[i]<<"\t";
cout<<endl;
}

void main()
{
Queue <int> q1;
Queue <double> q2;

int ch;

for(;;)
{

cout<<"\n 1 . insert integer\n";


cout<<"\n 2 . delete integer\n";
cout<<"\n 3 . display integer\n";
cout<<"\n 4 . insert double\n";
cout<<"\n 5 . delete double\n";
cout<<"\n 6 . display double\n";
cout<<"\n 7. exit\n";
cout<<"enter the choice\n";
cin>>ch;
switch(ch)
{
case 1: int x;
cout<<"\nEnter the integer to be inserted:\n ";
cin>>x;
q1.Insert(x); break;

case 2: q1.Delete(); break;

case 3: q1.Display(); break;

case 4: double y;
cout<<"\nEnter the double to be inserted:\n ";
cin>>y;
q2.Insert(y); break;

case 5: q2.Delete(); break;

case 6: q2.Display(); break;

case 7: cout<<"program terminated"; exit(0);


}

}
}

OUTPUT

1 . insert integer

2 . delete integer

3 . display integer

4 . insert double

5 . delete double

6 . display double

7. exit
enter the choice
1

Enter the integer to be inserted:


23

1 . insert integer

2 . delete integer
3 . display integer

4 . insert double

5 . delete double

6 . display double

7. exit
enter the choice
4

Enter the double to be inserted:


2.3

1 . insert integer

2 . delete integer

3 . display integer

4 . insert double

5 . delete double

6 . display double

7. exit
enter the choice
1

Enter the integer to be inserted:


24

1 . insert integer

2 . delete integer

3 . display integer

4 . insert double

5 . delete double

6 . display double

7. exit
enter the choice
4

Enter the double to be inserted:


4.5

1 . insert integer

2 . delete integer

3 . display integer

4 . insert double

5 . delete double

6 . display double

7. exit
enter the choice
3

The elements of the queue are:


23 24

1 . insert integer

2 . delete integer

3 . display integer

4 . insert double

5 . delete double

6 . display double

7. exit
enter the choice
6

The elements of the queue are:


2.3 4.5

1 . insert integer

2 . delete integer
3 . display integer

4 . insert double

5 . delete double

6 . display double

7. exit
enter the choice
5

2.3 deleted

1 . insert integer

2 . delete integer

3 . display integer

4 . insert double

5 . delete double

6 . display double

7. exit
enter the choice
2

23 deleted

1 . insert integer

2 . delete integer

3 . display integer

4 . insert double

5 . delete double

6 . display double

7. exit
enter the choice
7
program terminated
/*11. Write a C++ program to create a class called DLIST (Doubly Linked List) with
member functions to insert a node at a specified position an delete a node
from a specified position of the list .Demonstrate the operations by
displaying the content of the list after every operation.*/

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

struct NODE
{
int info;
NODE *llink;
NODE *rlink;
};

class Dlist
{
NODE *first;
public:
Dlist()
{
first = NULL;
}

void ins_pos();
void del_pos();
void show();
};

void Dlist::ins_pos()
{
NODE *cur,*prev;
NODE *temp=new NODE;
int count=1,pos;
cout<<"enter the item to be inserted\n";
cin>>temp->info;
if(first==NULL)
{
cout<<"the element inserted at the position 1\n";
pos=1;
temp->rlink=NULL;
first=temp;
return;
}
else
{
cout<<"enter the position\n";
cin>>pos;
}
if(pos==1)
{
temp->rlink=first;
temp->llink=NULL;
first->llink=temp;
first=temp;
}
else
{
cur=first;
prev=NULL;
while(count!=pos && cur!=NULL)
{
prev=cur;
cur=cur->rlink;
count++;
}
if(count!=pos)
{
cout<<"position not found\n";
return;
}
else
{
prev->rlink=temp;
temp->llink=prev;
temp->rlink=cur;
}
}

void Dlist::del_pos()
{
NODE *cur,*prev,*next;
int count=1,pos;
if(first==NULL)
{
cout<<"list is empty\n";
return;
}
cout<<"enter the position whose element to be deletd";
cin>>pos;
if(pos==1)
{
cur=first;
cout<<"the deleted item is "<<first->info;
first=first->rlink;
delete(cur);
return;
}
else
{
prev=NULL;
cur=first;
while(count!=pos && cur!=NULL)
{

prev=cur;
cur=cur->rlink;
count++;
}
if(count!=pos)
{
cout<<"position not found\n";
return;
}
else
{
prev->rlink=next;
cout<<"the deleted item is "<<cur->info;
delete(cur);
}
}

void Dlist ::show()


{
NODE *temp;
if(first==NULL)
{
cout<<"list is empty\n";
return;
}
cout<<"\nthe elements are\n";
for(temp=first;temp!=NULL;temp=temp->rlink)
cout<<temp->info<<"\t";
}
void main()
{

Dlist d;
int ch;

for(;;)
{
cout<<"\n1. Insert\n";
cout<<"2. Delete\n";
cout<<"3. Display\n";
cout<<"4. Exit\n";
cout<<"enter the choice\n";
cin>>ch;
switch(ch)
{
case 1: d.ins_pos();
break;
case 2: d.del_pos();
break;
case 3: d.show();
break;
case 4: cout<<"program terminated";
exit(0);
}
}
}
OUTPUT

1. Insert
2. Delete
3. Display
4. Exit
enter the choice
1
enter the item to be inserted
23
the element inserted at the position 1

1. Insert
2. Delete
3. Display
4. Exit
enter the choice
1
enter the item to be inserted
34
enter the position
2

1. Insert
2. Delete
3. Display
4. Exit
enter the choice
2
enter the position whose element to be dele
the deleted item is 34
1. Insert
2. Delete
3. Display
4. Exit
enter the choice
4
program terminated
/*12. Write a C++ program to create a class called STUDENT with data members USN,
nameand age.Using inheritance,create the classes UGSTUDENT and PGSTUDENT
having fields as Semester,Fees andStipend.Enter the data for atleast
5 students.Find the semesterwise average age for all theUG and PG students
separately.*/

#include<iostream.h>
#include<conio.h>
class student
{
public:
char name[20],usn[20];
int age;
void getdata();
void showdata();
};

void student::getdata()
{
cout<<"enter the name\n";
cin>>name;
cout<<"enter the usn\n";
cin>>usn;
cout<<"enter the age\n";
cin>>age;
}

void student::showdata()
{
cout<<"Name of the student "<<name<<endl;
cout<<"USN of the student "<<usn<<endl;
cout<<"age of the student "<<age<<endl;
}

class ugstudent:public student


{
public:
int sem,stipend;
long int fees;
void getdata();
void showdata();
};

void ugstudent::getdata()
{
student::getdata();
cout<<"enter the sem\n";
cin>>sem;
cout<<"enter the fees\n";
cin>>fees;
cout<<"enter the stipend\n";
cin>>stipend;
}

void ugstudent::showdata()
{
student::showdata();
cout<<"SEM "<<sem<<endl;
cout<<"Fees "<<fees<<endl;
cout<<"Stipend is "<<stipend<<endl;
}

class pgstudent:public student


{
public:
int sem,stipend;
long int fees;
void getdata();
void showdata();
};

void pgstudent::getdata()
{
student::getdata();
cout<<"enter the sem\n";
cin>>sem;
cout<<"enter the fees\n";
cin>>fees;
cout<<"enter the stipend\n";
cin>>stipend;
}

void pgstudent::showdata()
{
student::showdata();
cout<<"SEM "<<sem<<endl;
cout<<"Fees "<<fees<<endl;
cout<<"Stipend is "<<stipend<<endl;
}

void main()
{
ugstudent ug[10];
pgstudent pg[10];
int n;
clrscr();
cout<<"enter the number of students\n";
cin>>n;
cout<<"\nEnter the details of UG students\n";

for(int i=1;i<=n;i++)
{
cout<<"\nenter the details of student "<<i<<endl<<endl;
ug[i].getdata();
}

cout<<" UGSTUDENTS \n";

for(i=1;i<=n;i++)
{
cout<<"\n Details of student "<<ug[i].name<<endl;
ug[i].showdata();
}

for(int s=1;s<=8;s++)
{
float sum=0;
int count=0;
int flag=0;
for(i=1;i<=n;i++)
if(ug[i].sem==s)
{
sum+=ug[i].age;
count++;
flag=1;
}
if(flag==1)
{

cout<<endl<<s<<" semester "<<"\t"<<"average age is "<<sum/count<<"\n";


}
}

cout<<"\nEnter the details of PG students\n\n";

for(i=1;i<=n;i++)
{
cout<<"\nenter the details of student "<<i<<endl;
pg[i].getdata();
}

cout<<" PG STUDENTS \n";


for(i=1;i<=n;i++)
{
cout<<"\n Details of student "<<pg[i].name<<endl;
pg[i].showdata();
}

for(s=1;s<=4;s++)
{
float sum=0;
int count=0;
int flag=0;
for(i=1;i<=n;i++)
if(pg[i].sem==s)
{
sum+=pg[i].age;
count++;
flag=1;
}
if(flag==1)
{

cout<<endl<<s<<" semester "<<"\t"<<"average age is "<<sum/count<<"\n";


}
}
}

OUTPUT

enter the number of students


2

Enter the details of UG students

enter the details of student 1

enter the name


abc
enter the usn
123
enter the age
23
enter the sem
1
enter the fees
23456
enter the stipend
2345

enter the details of student 2

enter the name


xyz
enter the usn
234
enter the age
23
enter the sem
2
enter the fees
23456
enter the stipend
2345
UGSTUDENTS

Details of student abc


Name of the student abc
USN of the student 123
age of the student 23
SEM 1
Fees 23456
Stipend is 2345

Details of student xyz


Name of the student xyz
USN of the student 234
age of the student 23
SEM 2
Fees 23456
Stipend is 2345

1 semester average age is 23

2 semester average age is 23

Enter the details of PG students

enter the details of student 1


enter the name
ghi
enter the usn
456
enter the age
25
enter the sem
3
enter the fees
34567
enter the stipend
2345

enter the details of student 2


enter the name
venus
enter the usn
678
enter the age
25
enter the sem
2
enter the fees
344567
enter the stipend
2345
PG STUDENTS

Details of student ghi


Name of the student ghi
USN of the student 456
age of the student 25
SEM 3
Fees 34567
Stipend is 2345

Details of student venus


Name of the student venus
USN of the student 678
age of the student 25
SEM 2
Fees 344567
Stipend is 2345

2 semester average age is 25

3 semester average age is 25


/*13.Write a C++ program to create a class called STRING and implement the
following operations.Display the results after every operation by
overloading the operator <<
(1)STRING s1="VTU"
(2)STRING s2="BELGAUM"
(3)STRING s3=s1 +s2;(use copy constructor)*/

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

class String
{
char str[20];

public:
String() //zero argument constructor
{
strcpy(str, " ");
}

String(char *str1) // parameterized constructor


{
strcpy(str, str1);
}

String(const String &obj) //copy constructor


{
strcpy(str,obj.str);

String operator +(String s2);


friend ostream &operator <<(ostream &fout, String &s);
};

String String :: operator +(String s2)


{
String s3;
strcpy(s3.str,str);
strcat(s3.str," ");
strcat(s3.str, s2.str);
return s3;
}
ostream &operator <<(ostream &fout, String &s)
{

fout<<s.str;
return fout;
}

void main()
{
String s1("VTU ");
String s2("Belgaum");
clrscr();
cout<<"\nBefore concatenation\n";
cout<<"\nThe string1 is: \n";
cout<<s1;
cout<<"\n";
cout<<"\nThe string2 is: \n";
cout<<s2;
cout<<"\n\nAfter concatetion of string1 and string2 the string is\n";
String s3 = s1 + s2;
cout<<s3;
getch();
}

OUTPUT

Before concatenation

The string1 is:


VTU

The string2 is:


Belgaum

After concatetion of string1 and string2 the string is


VTU Belgaum
/*14 Write a C++ program to create a class BTREE with functions to perform inorder
preorder and postorder traversals.Create a BTREE object and demonstrate the
traversals*/

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
int info;
struct node *llink;
struct node *rlink;
};

class bin_tree
{
node *temp;
public:
node *root;
bin_tree()
{
root=NULL;
}
void create();
void pre(node *root);
void in(node *root);
void post(node *root);

};

void bin_tree::create()
{
node *prev,*cur;
temp=new node;
temp->llink=temp->rlink=NULL;
cout<<"enter the item\n";
cin>>temp->info;
if(root==NULL)
{
root =temp;
return;
}

prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
cur=(temp->info<cur->info)?cur->llink:cur->rlink;
}
if(temp->info<prev->info)
prev->llink=temp;
else
prev->rlink=temp;

}
void bin_tree::pre(node *root)
{
if(root!=NULL)
{
cout<<root->info<<"\t";
pre(root->llink);
pre(root->rlink);
}

void bin_tree::in(node *root)


{
if(root!=NULL)
{

in(root->llink);
cout<<root->info<<"\t";
in(root->rlink);
}

void bin_tree::post(node *root)


{
if(root!=NULL)
{

post(root->llink);
post(root->rlink);
cout<<root->info<<"\t";
}

}
void main()
{
bin_tree bt;
int ch;
bt.root=NULL;

clrscr();
for(;;)
{
cout<<"\n1. creation of tree\n";
cout<<"2. preorder traversal\n";
cout<<"3. inorder traversal\n";
cout<<"4. postorder traversal\n";
cout<<"5. exit \n";
cout<<"enter choice";
cin>>ch;
switch(ch)
{
case 1: bt.create();
cout<<"item added to tree\n\n";
break;
case 2:
if(bt.root==NULL)
{

cout<<"tree is empty";
break;
}
cout<<"\n pre order traversal\n";
bt.pre(bt.root);
cout<<endl;
break;

case 3: if(bt.root==NULL)
{
cout<<"tree is empty";
break;

}
cout<<"\n in order traversal\n";
bt.in(bt.root);
cout<<endl;
break;

case 4: if(bt.root==NULL)
{
cout<<"tree is empty";
break;
}
cout<<"\n post order traversal\n";
bt.post(bt.root);
cout<<endl;
break;

case 5: cout<<"program terminated";


exit(0);
}
}
}

OUTPUT

1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice1
enter the item
23
item added to tree

1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice1
enter the item
34
item added to tree

1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice1
enter the item
21
item added to tree

1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice1
enter the item
12
item added to tree

1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice2

pre order traversal


23 21 12 34

1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice3

in order traversal
12 21 23 34

1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice4

post order traversal


12 21 34 23

1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice5
program terminated
/*15 Write a C++ program to create a class called EXPRESSION.Using appropriate member functions
convert a given valid infix expression to a postfix form.Display the infix and postfix exp.*/

#include<iostream.h>
#include<conio.h>
#include<string.h>
class express
{
char infix[20],post[20],s[20];
int top;
public:
express(char in[20])
{
top=-1;
strcpy(infix,in);
}
void push(char c);
char pop();
int s_p(char c);
int i_p(char c);
void inpo();
void show();
};
void express::push(char c)
{
s[++(top)]=c;
}

char express::pop()
{
return(s[top--]);
}

int express::s_p(char c)
{
switch(c)
{
case '+':
case '-': return 2;
case '*':
case '/': return 4;
case '$':
case '^': return 5;
case '(' : return 0;
case '#' :return -1;
Default: return 8;
}
}

int express::i_p(char c)
{
switch(c)
{
case '+':
case '-': return 1;
case '*':
case '/': return 3;
case '$':
case '^': return 6;
case '(' : return 9;
case ')' :return 0;
Default: return 7;
}
}

void express::inpo()
{
char symbol;
int i,j;
j=0;
push('#');
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while((s_p(s[top]))>i_p(symbol))
post[j++]=pop();
if((s_p(s[top]))!=i_p(symbol))
push(symbol);
else
pop();
}
while(s[top]!='#')
post[j++]=pop();
post[j]='\0';
}

void express::show()
{
cout<<"the postfix expression is \n"<<post;
}
void main()
{
char infix[20];
clrscr();
cout<<"enter the infix expression\n";
cin>>infix;
express exp=infix;
exp.inpo();
exp.show();
getch();
}

OUTPUT

enter the infix expression


a+b*(c+d)
the postfix expression is
abcd+*+

You might also like