0% found this document useful (0 votes)
2K views

2c Lab Manual C++-Final

The document is a laboratory manual for a C++ programming lab course. It provides 16 questions/problems for students to solve related to object-oriented programming concepts in C++, including creating classes, overloading operators, inheritance, templates, and other common OOP tasks. Students will pick one question from the list to solve during their examination. The questions cover topics like defining classes for employees, students, complex numbers, linked lists, stacks, dates, matrices, binary trees, and more.

Uploaded by

Praveen Patil
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

2c Lab Manual C++-Final

The document is a laboratory manual for a C++ programming lab course. It provides 16 questions/problems for students to solve related to object-oriented programming concepts in C++, including creating classes, overloading operators, inheritance, templates, and other common OOP tasks. Students will pick one question from the list to solve during their examination. The questions cover topics like defining classes for employees, students, complex numbers, linked lists, stacks, dates, matrices, binary trees, and more.

Uploaded by

Praveen Patil
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 48

C++ Lab Manual

Children’s Education Society (Regd.)


THE OXFORD COLLEGE OF ENGINEERING
Hosur Road, Bommanahalli, Bangalore

Department of
Master of Computer Applications

A Laboratory Manual

For

Object Oriented Programming with C++ Laboratory- 10MCA27

2nd Semester, MCA

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 1 of 48


C++ Lab Manual

Object Oriented Programming with C++ Laboratory

Subject Code: 10MCA27 I.A Marks: 50


Hours/Week: 3 Exam Marks: 50
Total Hours : 42 Exam Hours: 3

1. Given that an EMPLOYEE class contains the following members:


a. Data Members: Employee_Number, Employee_Name, Basic, DA, IT, Net_Sal
b. Member Functions: to read data, to calculate Net_Sal and to print data members

2. Write a C++ program to read data on N employees and compute the Net_Sal of each employee
(DA = 52% of Basic and Income Tax = 30% of the gross salary)

3. Define a STUDENT class with USN, Name, and Marks in 3 tests of a subject. Declare an array of 10
STUDENT objects. Using appropriate functions, find the average of the two better marks for each
student. Print the USN, Name and the average marks of all the students.

4. Write a C++ program to create a class called COMPLEX and implement the following overloading
functions ADD that return a complex number:
a. ADD(a, s2) – where ‘a’ is an integer (real part) and s2 is a complex number
b. ADD(s1, s2) – where s1 and s2 are complex numbers

5. 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.

6. Write a C++ program to create a template function for Quicksort and demonstrate sorting of
integers and doubles.

7. Write a C++ program to create a class called STACK using an array of integers. Implement the
following operations by overloading the operators ‘+’ and ‘-‘:
a. 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
b. s1 = s1- ; where s1 is an object of the class STACK. ‘-‘ operator pops the element.
Handle the STACK empty and full conditions. Also display the contents of the stack after each
operation, by overloading the << operator.

8. 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 overloading the operator <<.
a. no_of_days = d1 – d2; where d1 and d2 are DATE objects, and no_of_days is an integer
b. d2 = d1 + no_of_days; where d1 is a DATE object and no_of_days is an integer

9. Create a class called MATRIX using two-dimensional array of integers. Implement the following
operations by overloading the operator ++ which checks the compatibility of two matrices to be
added and subtracted. Perform the addition and subtraction by overloading the + and – operators
respectively. Display the results by overloading the operator <<.

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 2 of 48


C++ Lab Manual

If (m1==m2) then m3 = m1+m2 and m4 = m1-m2 else display error.

10. Write a C++ program to create a class called OCTAL which has the characteristics of an octal number.
Implement the following operations by writing an appropriate constructor and an overloaded
operator +.
a. OCTAL h = x; where x is an integer.
b. int y = h + k; where h is an OCTAL object and k is an integer
Display the OCTAL result by overloading the operator << . Also display the values of h and y.

11. 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 the member functions, implement a queue of integers
and double. Demonstrate the operations by displaying the contents of the queue after every
operation.

12. Write a C++ program to create a class called DLIST (doubly Linked List) with member functions to
insert a node at a specified position and delete a node from a specified position of the list.
Demonstrate the operations by displaying the content of the list after every operation.

13. Write a C++ program to create a class called STUDENT with data members USN, Name and Age.
Using inheritance, create the classes UGSTUDENT and PGSTUDENT having fields as Semester, Fees
and Stipend. Enter the data for at least 5 students. Find the semester-wise average age for all UG
and PG students separately.

14. 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 <<.
a. STRING s1 = “VTU”
b. STRING s2 = “BELGAUM”
c. STRING s3 = s1 + s2 (Use copy constructor)

15. Write a C++ program to create a class called BIN_TREE (Binary Tree) with member functions to
perform in-order, preorder and post-order traversals. Create a BIN_TREE object and demonstrate
the traversals.

16. Write a C++ program to create a class called EXPRESSION. Using appropriate member functions
convert a given valid Infix expression into postfix form. Display the infix and postfix expressions.

Note: In the examination each student picks one question from a lot of all the 16 questions.

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 3 of 48


C++ Lab Manual

1. Given that an EMPLOYEE class contains the following members:


a. Data Members: Employee_Number, Employee_Name, Basic, DA, IT, Net_Sal
b. Member Functions: to read data, to calculate Net_Sal and to print data members

#include<iostream.h>
#include<conio.h>
class employee
{
int emp_id;
char emp_name[20];
float basic,DA,gross,IT,net_sal;
public:
void read_data();
void net_cal();
void write_data()
{
cout<<"Name : "<<emp_name<<endl;
cout<<"ID : "<<emp_id<<endl;
cout<<"Basic : "<<basic<<endl;
cout<<"DA : "<<DA<<endl;
cout<<"Gross : "<<gross<<endl;
cout<<"IT : "<<IT<<endl;
cout<<"Net_salary : "<<net_sal<<endl;
}
};
void employee::read_data()
{
cout<<"enter name : ";
cin>>emp_name;
cout<<"enter the ID :";
cin>>emp_id;
cout<<"enter Basic : ";
cin>>basic;
}
void employee::net_cal()
{
DA=0.52*basic;
gross=basic+DA;
IT=0.30*gross;
net_sal=gross-IT;
}
void main()
{
clrscr();
employee e1;
e1.read_data();
e1.net_cal();

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 4 of 48


C++ Lab Manual

e1.write_data();
getch();
}

OUTPUT
Enter name : asha
enter the ID :111
enter Basic : 40000
Name : asha
ID : 111
Basic : 40000
DA : 20800
Gross : 60800
IT : 18240
Net_salary : 42560

2. Write a C++ program to read data on N employees and compute the Net_Sal of each employee
(DA = 52% of Basic and Income Tax = 30% of the gross salary)

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class EMPLOYEE
{
char name[10];
int id;
int basic;
float it,net,gross,da;
public:
void readdata();
void cal_net();
void write();
};
void EMPLOYEE::readdata()
{
cout<<"enter the name\n";
cin>>name;
cout<<"enter the id\n";
cin>>id;
cout<<"enter the basic\n";
cin>>basic;
}
void EMPLOYEE::cal_net()
{
da=0.52*basic;
gross=basic+da;

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 5 of 48


C++ Lab Manual

it=0.30*gross;
net=gross-it;
}
void EMPLOYEE ::write()
{
printf("\n%s\t %d\t %d\t %1.2f %1.2f %1.2f %1.2f",name,id,basic,da,it,gross,net);
}
void main()
{
EMPLOYEE e[10];
int n,i;
clrscr();
cout<<"how many records u want";
cin>>n;
cout<<"enter the records\n";
for(i=0;i<n;i++)
{
e[i].readdata();
e[i].cal_net();
}
cout<<"name\t id\t basic\t da\t it\t gross\t net\n";
for(i=0;i<n;i++)
{
e[i].write();
}
getch();
}

Output
how many records u want 3
enter the records
enter the name
asha
enter the id
111
enter the basic
30000
enter the name
laxmi
enter the id
222
enter the basic
10000
enter the name
fiza
enter the id
333
enter the basic

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 6 of 48


C++ Lab Manual

25000

name id basic da it gross net

asha 111 30000 15600.00 13680.00 45600.00 31920.00


beena 222 10000 5200.00 4560.00 15200.00 10640.00
laxmi 333 25000 13000.00 11400.00 38000.00 26600.00

3. Define a STUDENT class with USN, Name, and Marks in 3 tests of a subject. Declare an array of 10
STUDENT objects. Using appropriate functions, find the average of the two better marks for each
student. Print the USN, Name and the average marks of all the students.

#include<iostream.h>
#include<conio.h>
class student
{
private:
int usn,m1,m2,m3;
char name[20];
public:
void getdata();
void putdata();
};
void student::getdata()
{
int i;
cout<<"enter the usn number:";
cin>>usn;
cout<<"enter the name:";
cin>>name;
cout<<"\n enter the 3 marks:\n";
cin>>m1>>m2>>m3;
}
void student::putdata()
{
float avg,min;
min=m1;
if(m2<min)
min=m2;
if(m3<min)
min=m3;
avg=(m1+m2+m3-min)/2;
cout<<endl<<"usn:"<<usn;
cout<<endl<<"name:"<<name;
cout<<endl<<"average marks:"<<avg;
}
main()
{

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 7 of 48


C++ Lab Manual

student s[10];
int i,n;
clrscr();
cout<<endl<<"enter number of students n:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter"<<i+1<<"students data:"<<endl;
s[i].getdata();
}
cout<<endl<<"students information:"<<endl;
for(i=0;i<n;i++)
s[i].putdata();
getch();
return 0;
}

OUTPUT

Enter the no.of students : 2

Enter the 1st student data

Enter the usn number : 101


Enter the name : RAJESH
Enter the 3 marks :

50 60 50

Enter the 2nd student data

Enter the usn number : 102


Enter the name : RAMKUMAR
Enter the 3 marks :

60 50 60

Calculated students average marks :

Usn : 101
Name : RAJESH
Average : 55

Usn : 102
Name : RAMKUMAR
Average : 60

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 8 of 48


C++ Lab Manual

4. 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<iostream.h>
#include<conio.h>
class complex
{
int real, imag;
public:
void getdata();
void display();
friend complex add(int a, complex s2);
friend complex add(complex s1, complex s2);
};
//function to accept real and imaginary parts of a
//complex number
void complex::getdata()
{
cin>>real>>imag;
}
//overloaded ADD function taking one integer and
//one complex number
complex add(int a, complex s2)
{
complex temp;
temp.real=a+s2.real;
temp.imag=s2.imag;
return temp;
}
//overloading ADD function taking both complex parameters
complex add(complex s1, complex s2)
{
complex temp;
temp.real=s1.real+s2.real;
temp.imag=s1.imag+s2.imag;
return temp;
}
//Function to display the complex number
void complex::display()
{
cout<<real<<"+i"<<imag<<endl;
}
void main()
{
clrscr();

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 9 of 48


C++ Lab Manual

complex s1, s2, s3, s4;


int a;
cout<<"Enter the first complex value for real, imaginary"<<endl;
s1.getdata();
cout<<"Enter the second complex value for real, imaginary"<<endl;
s2.getdata();
cout<<"1st complex no:";
s1.display();
cout<<"2nd complex no:";
s2.display();
cout<<"enter a integer value(real part)"<<endl;
cin>>a;
s3=add(a, s2);
cout<<"S3 complex no(a+s2.real)+imaginary:";
s3.display();
s4=add(s1, s2);
cout<<"s4 complex no[s1+s2]:";
s4.display();
getch();
}

OUTPUT :
Enter the first complex value for real, imaginary
5
4
Enter the second complex value for real, imaginary
4
3
1st complex no:5+i4
2nd complex no:4+i3
enter a integer value(real part)
2
S3 complex no(a+s2.real)+imaginary:6+i3
s4 complex no[s1+s2]:9+i7

5. 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>
class LIST
{
private:
struct node
{

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 10 of 48


C++ Lab Manual

int data;
struct node *link;
}*p;
public:
LIST()
{
p=NULL;
}
void insert(int);
void delete1();
int countList();
void display();
};
//Function to insert a node at front
void LIST::insert(int n)
{

node *q;
q = new node;
q->data = n;
q->link = p;
p=q;
}
//Function to delete a node from front
void LIST::delete1()
{
node *q;
if(p == NULL)
{
//cout<<"\nList is empty";
return;
} else {
cout<<"\nThe deleted Node is : "<<p->data<<endl;
q=p;
p=p->link;
delete q;
}
}
//Function to display the contents of the list
void LIST::display()
{
node *q;
if(p==NULL)
cout<<"The list is empty";
else {

for(q=p;q!=NULL;q=q->link)
{

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 11 of 48


C++ Lab Manual

cout<<q->data<<"\t";
}
}
cout<<"\n";
}
void main()
{
clrscr();
LIST l;
int ch, item;
while(1)
{
cout<<"\nSINGLY LINKED LIST"<<endl;
cout<<"*************************"<<endl;
cout<<"\n1.Insert\n2.Delete\n3.Exit"<<endl;
cout<<"enter your choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the node value"<<endl;
cin>>item;
l.insert(item);
l.display();
break;
case 2:
l.delete1();
l.display();
break;
case 3:
exit(0);
default:
cout<<"enter correct choice"<<endl;
getch();
}
}
}

OUTPUT:
SINGLY LINKED LIST
1. Insert
2. Delete
3. Exit

Enter Your choice : 1

Enter the node value : 3

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 12 of 48


C++ Lab Manual

SINGLY LINKED LIST

1. Insert
2. Delete
3. Exit

Enter Your choice : 1

Enter the node value : 2


2 3

SINGLY LINKED LIST


1.Insert
2. Delete
3. Exit

Enter Your choice : 1

Enter the node value : 1


1 2 3

SINGLY LINKED LIST

1.Insert
2. Delete
3. Exit

Enter Your choice : 1

Enter the node value : 4

List is Full.

SINGLY LINKED LIST

1.Insert
2. Delete
3. Exit

Enter Your choice : 2

2 3

SINGLY LINKED LIST

1.Insert

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 13 of 48


C++ Lab Manual

2. Delete
3. Exit

Enter Your choice : 2

SINGLY LINKED LIST

1.Insert
2. Delete
3. Exit

Enter Your choice : 2

List is empty.

6. Write a C++ program to create a template function for Quicksort and demonstrate sorting of
integers and doubles.

#include<iostream.h>
#include<conio.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 left, right;
key=a[low];
left=low+1;
right=high;
while(1)
{
while((left<high) &&(key>=a[left]))
left++;
while(key<a[right])
right--;

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 14 of 48


C++ Lab Manual

if(left<right)
{
temp=a[left];
a[left]=a[right];
a[right]=temp;
}
else
{
temp=a[low];
a[low]=a[right];
a[right]=temp;
return (right);
}
}
}

void main()
{
clrscr();
int a[10],n,i,low, high;
double b[10];
cout<<"QUICK SORT USING FUNCTION TEMPLATE(To perform int,double array
operatons"<<endl;

cout<<"*************************************************************************"<<end
l;
cout<<"enter array size\n";
cin>>n;
cout<<"Enter the elements of integer array\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the elements of double array\n";
for(i=0;i<n;i++)
cin>>b[i];
low=0;
high=n-1;
quicksort(a, low, high);
quicksort(b, low, high);
cout<<"The sorted list of integer:\n";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
cout<<"\nThe sorted list of double:\n";
for(i=0;i<n;i++)
cout<<"\t"<<b[i];
getch();
}

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 15 of 48


C++ Lab Manual

OUTPUT:
QUICK SORT USING FUNCTION TEMPLATE(T0 perform int, double array )
Enter array size : 3

Enter the elements of integer array


2
1
3

Enter the elements of doubly array


2.1
1.1
3.1
The sorted list of integer array : 1 2 3

The sorted list of double array : 1.1 2.1 3.1

7. Write a C++ program to create a class called STACK using an array of integers. Implement the
following operations by overloading the operators ‘+’ and ‘-‘:
c. 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
d. s1 = s1- ; where s1 is an object of the class STACK. ‘-‘ operator pops the element.
Handle the STACK empty and full conditions. Also display the contents of the stack after each
operation, by overloading the << operator.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//#include<PROCESS.H>
#define size 5
class stack
{
private: int s[5];
int top;
public:
stack();
~stack();
friend stack operator+(stack s1,int e);
friend stack operator-(stack s1);
friend ostream &operator <<(ostream & print,stack s);
};

stack::stack()
{
top = -1;

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 16 of 48


C++ Lab Manual

}
stack::~stack()
{
}
stack operator+(stack s1,int e)
{
if(s1.top == 4)
cout<<"stack full";
else
{
s1.top ++;
s1.s[s1.top]= e;
}
return s1;
}
stack operator-(stack s1)
{
if(s1.top == -1)
{
cout<<"stack empty";
}
else
{
cout<<"poped element is"<<s1.s[s1.top];
s1.top =s1.top - 1;
}
return s1;
}

ostream & operator <<(ostream & print,stack s1)


{
if (s1.top == -1)
cout<<"\nstack empty";
else
{
cout<<"\nelements of the stack\n";
for(int i=s1.top;i>=0;i--)
{
cout<<"\t";
print<<s1.s[i];
cout<<"\n";
}
}
return print;
}

main()

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 17 of 48


C++ Lab Manual

{
stack s1;
int element,ch= 1;
clrscr();

while(1)
{
cout<<"\n 1:push\n 2:pop \n 3:quit";
cout<<"\nenter choice ";
cin >>ch;
switch(ch)
{
case 1: cout <<"enter element ";
cin>>element;
s1 = s1 +element;
cout<<s1;
break;
case 2: s1 = -s1 ;
cout<<s1;
break;
default: exit(0);
}
}
// return(0);
}

OUTPUT:

STACK USING OPERATOR OVERLOADING

1. PUSH
2. POP
3. QUIT

Enter your choice: 1


Enter element :3

3
1. PUSH
2. POP
3. QUIT

Enter your choice: 1


Enter element :2
2 3
1. PUSH

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 18 of 48


C++ Lab Manual

2. POP
3 QUIT

Enter your choice: 1


Enter element :1
1 2 3

1. PUSH
2. POP
3. QUIT

Enter your choice: 1


Enter element: 1
1. PUSH
2. POP
3. QUIT

Stack full.

1. PUSH
2. POP
3. QUIT

Enter your choice: 2


2 3

1. PUSH
2. POP
4. QUIT

Enter your choice: 2


3

1. PUSH
2. POP
3. QUIT

Enter your choice: 2

Stack empty.

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 19 of 48


C++ Lab Manual

8. 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 overloading the operator <<.
c. no_of_days = d1 – d2; where d1 and d2 are DATE objects, and no_of_days is an integer
d. d2 = d1 + no_of_days; where d1 is a DATE object and no_of_days is an integer
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class date
{
public:
int dd,mm,yyyy;
// int month[13];
long days;
date(int d,int n,int y);
date getdata(int ,int);
date operator +(long n);
long operator-(date);
friend ostream &operator<<(ostream& ,date);
};
date :: date(int d,int m,int y)
{
dd=d;
mm=m;
yyyy=y;
int feb=((yyyy%4==0&&yyyy%100!=0)||(yyyy%400==0)) ? 29:28;
int month[]={0,31,feb,31,30,31,30,31,31,30,31,30,31};
days=0;
if(d>month[m])
{
cout<<"\nINVALID DATE";
exit(0);
}

for(int i=1;i<mm;i++)
days+=month[i];
days+=dd;
}
long date ::operator -(date d2)
{
long days1=days;
long days2=d2.days;
long d=days1-days2;
for(int i=d2.yyyy;i<yyyy;i++)
d+=((i%4==0&&i%100!=0)||(i%400==0))?366:365;
return d;
}

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 20 of 48


C++ Lab Manual

void valid(int d,int m,int y)


{
if(d>0&&d<=31&&m>0&&m<=12&&y>0)
cout<<"VALID DATE\n";
else
{
cout<<"INVALID DATE\n";
exit(0);
}
}

ostream &operator<<(ostream &ot, date d)


{
ot<<d.dd<<"/"<<d.mm<<"/"<<d.yyyy;
return ot;
}

/*void isless(date d1,date d2)


{
int dy=d1.yyyy-d2.yyyy;
long dd=d1.days-d2.days;
if(dy<0)
{
cout<<"Date2 is greater \n";
getch();
exit(0);
}
if(dy<=0&&dd<0)
{
cout<<"Date2 is greater\n";
getch();
exit(0);
}
} */

date date :: getdata(int d,int year)


{
int feb=((year%4==0&&year%100!=0)||(year%400==0))?29:28;
int month[]={0,31,feb,31,30,31,30,31,31,30,31,30,31};
date dt(0,1,year);
for(int i=1;i<=d;i++)
{
dt.dd++;
if(dt.dd>month[dt.mm])
{
dt.mm++;

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 21 of 48


C++ Lab Manual

dt.dd=1;
}
}
return dt;
}

date date ::operator + (long d)


{
long dys=this->days;
int yr=yyyy;
for(int i=1;i<=d;i++)
{
dys++;
int t=((yr%4==0&&yr%100!=0)||(yr%400==0))?366:365;
if(dys>t)
{
dys=1;
yr++;
}
}
return getdata(dys,yr);
}

void main()
{
int dd ,mm,yyyy;
clrscr();
cout<<"First Date :\n";
cout<<"Day: ";cin>>dd;
cout<<"Month: ";cin>>mm;
cout<<"Year: ";cin>>yyyy;

date d1(dd,mm,yyyy);
valid(dd,mm,yyyy);
cout<<"enter the second date:\n";
cout<<"Day: ";cin>>dd;
cout<<"month: ";cin>>mm;
cout<<"year: ";cin>>yyyy;

date d2(dd,mm,yyyy);
valid(dd,mm,yyyy);
// isless(d1,d2);
clrscr();
cout<<"\n Date1: "<<d1;
cout<<"\n Date2: "<<d2;
long no_of_days=d1-d2;
cout<<"\nno of days :"<<no_of_days;
cout<<"\nEnter the days to be added to date1:";cin>>no_of_days;

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 22 of 48


C++ Lab Manual

d2=d1+no_of_days;
cout<<"Result date is : "<<d2;
getch();
}

9. Create a class called MATRIX using two-dimensional array of integers. Implement the following
operations by overloading the operator ++ which checks the compatibility of two matrices to be
added and subtracted. Perform the addition and subtraction by overloading the + and – operators
respectively. Display the results by overloading the operator <<.
If (m1==m2) then m3 = m1+m2 and m4 = m1-m2 else display error.

#include<iostream.h>
#include<conio.h>
class matrix
{
private:
int a[5][5],row,col;
public :
//initializing the matrix size
void initialize(int r, int c)
{
row = r;
col = c;
}
void getmatrix();
int operator == (matrix m2);
matrix operator + (matrix m2);
matrix operator - (matrix m2);
friend ostream &operator << (ostream &print,matrix m2);
};
//getting the array elements
void matrix :: getmatrix()
{
for(int i= 0;i<row;i++)
for(int j= 0;j<col;j++)
cin>>a[i][j];
}
//comparing M1(row,col) and M2 matrix(row,col)
int matrix :: operator == (matrix m2)
{
return(m2.row == row&& m2.col == col);
}
//matrix addition
matrix matrix :: operator + (matrix m2)
{
matrix temp;
for(int i= 0;i<row;i++)

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 23 of 48


C++ Lab Manual

for(int j= 0;j<col;j++)
temp.a[i][j]= a[i][j] + m2.a[i][j];
temp.row = m2.row;
temp.col = m2.col;
return temp;
}
//matrix subtraction
matrix matrix :: operator - (matrix m2)
{
matrix temp;
for(int i= 0;i<row;i++)
for(int j= 0;j<col;j++)
temp.a[i][j]= a[i][j] - m2.a[i][j] ;
temp.row = m2.row;
temp.col = m2.col;
return temp;
}
//Displaying the m1,m2,m3,m4 matrix
ostream &operator <<(ostream &print,matrix m)
{
print<<"\n\n";
for(int i= 0;i<m.row;i++)
{
for(int j= 0;j<m.col;j++)
print<<"\t"<<m.a[i][j];
print<<"\n\n";

}
return print;
}
void main()
{
matrix m1,m2,m3,m4;
int r,c,ch;
clrscr();
cout<<"MATRIX ADDITION AND SUBTRACTION"<<endl;
cout<<"**********************************"<<endl;
cout<<"ENTER THE ORDER OF THE MATRIX1:";
cin>>r>>c;
m1.initialize(r,c);
cout<<"ENTER THE ORDER OF THE MATRIX2:";
cin>>r>>c;
m2.initialize(r,c);
if(m1 == m2)
{
cout<<"Enter M1 matrix elements:";
m1.getmatrix();
cout<<"\nENTER M2 matrix elements:";

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 24 of 48


C++ Lab Manual

m2.getmatrix();
cout<<"\nM1 MATRIX:"<<endl;
cout<<m1;

cout<<"\nM2 MATRIX:"<<endl;
cout<<m2;
m3 = m1+m2;
m4 = m1-m2;
cout<<"\nMatrix Addition:"<<m3<<endl;
cout<<"\nMatrix Subtraction:"<<m4<<endl;
}
else
cout<<"Matrix addition and subtraction not possible"<<endl;

getch();
}

OUTPUT

MATRIX ADDITION AND SUBTRACTION


ENTER THE ORDER OF THE MATRIX1 :2 2

ENTER THE ORDER OF THE MATRIX2 :2 1

Matrix addition and subtraction not possible

ENTER THE ORDER OF THE MATRIX1 :2 2

ENTER THE ORDER OF THE MATRIX2 :2 2

Enter M1 matrix elements :1


2
3
4
Enter M2 matrix elements : 1
2
3
4
Matrix Addition

2 4
6 8

Matrix Subtraction
0 0
1 0

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 25 of 48


C++ Lab Manual

10. Write a C++ program to create a class called OCTAL which has the characteristics of an octal number.
Implement the following operations by writing an appropriate constructor and an overloaded
operator +.
c. OCTAL h = x; where x is an integer.
d. int y = h + k; where h is an OCTAL object and k is an integer
Display the OCTAL result by overloading the operator << . Also display the values of h and y.

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

class octal
{
int num;
public:
octal()
{
num=0;
}
octal(int n)
{
num=n;
}
int operator+(octal n1)
{
return (num+n1.num);
}
friend ostream&operator<<(ostream &print,octal o);
};

ostream &operator<<(ostream &print,octal o)


{
print<<setbase(8)<<o.num;
return print;
}

void main()
{
int x,k,y,oct;
//cout<<"enter the octal number"<<endl;
//cin>>oct;
octal h(8);
clrscr();
cout<<"number in base 8 is"<<h<<endl;
cout<<"\n ENTER THE DECIMAL NUMBER";
cin>>k;
cout<<"\n THE equivalent in octal is:"<<k<<endl;

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 26 of 48


C++ Lab Manual

y=h+k;
cout<<" RESULT OF Y=h+k IS "<<y<<"\n";
getch();

OUTPUT

Number in base 8 is : 10
Enter Decimal Number :5

The equivalent in octal is : 5

Result of y = h + k is : 15

11. 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 the member functions, implement a queue of integers
and double. Demonstrate the operations by displaying the contents of the queue after every
operation.

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

template <class T>


class queue
{
T a[max];
int front,rear;
public:
queue()
{
front=-1;
rear=-1;
}
void insert(T);
void delets();
void display();
};
template <class T>
void queue<T>::insert(T f)
{
if(rear==(max-1))
{

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 27 of 48


C++ Lab Manual

cout<<"\n queue is full::";


return;
}
else
{
rear++;
a[rear]=f;
if(front==-1)
front=0;
}
}

template <class T>


void queue<T>::delets()
{
if(front==-1)
{
cout<<"\n queue is empty::";
return;
}
else
{
cout<<"\n deleted element is"<<a[front]<<"\n";
front++;
if(front>rear)
{
front=-1;
rear=-1;
}
}
}
template <class T>
void queue<T>::display()
{
if(front==-1)
{
cout<<"\n queue is empty";
return;
}
else
{
for(int i=front;i<=rear;i++)
cout<<a[i]<<"\t";
}
}
void main()
{
clrscr();

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 28 of 48


C++ Lab Manual

queue<int> iq;
queue<double> dq;
int choice,item;
double fitem;
while(1)
{
cout<<"\n Queue using template:-";
cout<<"\n==========================";
cout<<"\n\t 1.insert int\n\t 2.delete int \n\t 3.inert double\n\t 4.delete double\n\t
5.exit";
cout<<"\n==========================";
cout<<"\n Enter your choice:-";
cin>>choice;
cout<<"\n==========================";
switch(choice)
{
case 1:
cout<<"\n enter element to insert:-";
cin>>item;
iq.insert(item);
iq.display();
break;
case 2:
iq.delets();
iq.display();
break;
case 3:
cout<<"\n Enter element to insert:-";
cin>>fitem;
dq.insert(fitem);
dq.display();
break;
case 4:
dq.delets();
dq.display();
break;
case 5:
exit(0);
break;
default:
cout<<"\n Enter appropriate choice:-";
break;
}
getch();
}

OUTPUT
Queue using template:-

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 29 of 48


C++ Lab Manual

==========================
1.insert int
2.delete int
3.inert double
4.delete double
5.exit
==========================
Enter your choice:-1
==========================
enter element to insert:-
1
1
Queue using template:-
==========================
1.insert int
2.delete int
3.inert double
4.delete double
5.exit
==========================
Enter your choice:-3
==========================
Enter element to insert:-3.56
3.56
Queue using template:-
==========================
1.insert int
2.delete int
3.inert double
4.delete double
5.exit
==========================
Enter your choice:-
3
==========================
Enter element to insert:-6.8
3.56 6.8
Queue using template:-
==========================
1.insert int
2.delete int
3.inert double
4.delete double
5.exit
==========================
Enter your choice:-1
enter element to insert:-7
1 7

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 30 of 48


C++ Lab Manual

Queue using template:-


==========================
1.insert int
2.delete int
3.inert double
4.delete double
5.exit
==========================
Enter your choice:-2
==========================
deleted element is1
7
Queue using template:-
==========================
1.insert int
2.delete int
3.inert double
4.delete double
5.exit
==========================
Enter your choice:-4
==========================
deleted element is3.56
6.8
Queue using template:-
==========================
1.insert int
2.delete int
3.inert double
4.delete double
5.exit
==========================
Enter your choice:-2
==========================
deleted element is7
queue is empty

12. Write a C++ program to create a class called DLIST (doubly Linked List) with member functions to
insert a node at a specified position and 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<stdlib.h>
#include<conio.h>
class Dlist
{
struct node
{

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 31 of 48


C++ Lab Manual

node *llink;
int data;
node *rlink;
}*start;
public:
Dlist()
{
start=NULL;
}
void insert_pos(int, int);
void delete_pos(int);
void display();
};

void Dlist::insert_pos(int pos, int item)


{
node *temp, *prev, *cur;
int p;

if(temp==NULL)
{
cout<<"memory allocation failed"<<endl;
exit(0);
}
temp=new node;
temp->data=item;
temp->llink=NULL;
temp->rlink=NULL;
if(pos==1)
{
temp->rlink=start;
start->llink=temp;
start=temp;
return;
}
p=1;
prev=NULL;
cur=start;

while(p!=pos && cur!=NULL)


{
prev=cur;
cur=cur->rlink;
p++;
}
if(p==pos)
{
prev->rlink=temp;

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 32 of 48


C++ Lab Manual

temp->llink=prev;
temp->rlink=cur;
cur->llink=temp;
}
else
cout<<"\ninvalid position";
}
void Dlist::delete_pos(int pos)
{
node *next, *temp, *prev, *cur;
int p;
if(start==NULL)
{
cout<<"\nlist is empty\n";
return;
}
if(pos==1)
{
temp=start;
start=start->rlink;
start->llink=NULL;
cout<<"\ndeleted element is:"<<temp->data<<endl;
delete temp;
return;
}
p=1;
prev=NULL;
cur=start;
while(p!=pos &&cur!=NULL)
{
prev=cur;
cur=cur->rlink;
p++;
}
if((p==pos)&&(cur!=NULL))
{
next=cur->rlink;
prev->rlink=next;
next->llink=prev;
cout<<"\ndeleted element is:"<<cur->data<<endl;
delete cur;
return;
}
else
cout<<"\ninvlid position\n";
return;
}
void Dlist::display()

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 33 of 48


C++ Lab Manual

{
node *temp;
if(start==NULL)
{
cout<<"\nlist is empty\n";
return;
}
cout<<"\ncontents of the list\n";
for(temp=start;temp!=NULL;temp=temp->rlink)
{
cout<<temp->data<<endl;
}
}
void main()
{
Dlist d;
int item, pos, ch;
clrscr();
while(1)
{
cout<<"\nDOUBLY LINKED LIST"<<endl;
cout<<"*************************"<<endl;
cout<<"1.insert at position\n";
cout<<"2.delete from position\n";
cout<<"3.display\n4.exit\n";
cout<<"Enter your choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the item:";
cin>>item;
cout<<"Enter the position:";
cin>>pos;
d.insert_pos(pos, item);
break;
case 2:
cout<<"\nEnter the position:"<<endl;
cin>>pos;
d.delete_pos(pos);
break;
case 3:
d.display();
break;
case 4: exit(0);
default:
cout<<"Enter correct choice\n";
getch();

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 34 of 48


C++ Lab Manual

}
}
}

OUTPUT

1.Insert at position
2.Delete from position
3.Display
4.Exit

Enter your choice :1


Enter the item : 5
Enter the position: 2
Invalid position

1. Insert at position
2.Delete from position
3.Display
4.Exit

Enter your choice :1


Enter the item : 1
Enter the position: 1

1. Insert at position
2.Delete from position
3.Display
4.Exit

Enter your choice :1


Enter the item : 2
Enter the position: 2
1..Insert at position
2.Delete from position
3.Display
4.Exit

Enter your choice :2


Enter the position: 2
The deleted element is : 2

1..Insert at position
2.Delete from position
3.Display
4.Exit

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 35 of 48


C++ Lab Manual

Enter your choice :2


Enter the position: 1
The deleted element is : 1

13. Write a C++ program to create a class called STUDENT with data members USN, Name and Age.
Using inheritance, create the classes UGSTUDENT and PGSTUDENT having fields as Semester, Fees
and Stipend. Enter the data for at least 5 students. Find the semester-wise average age for all UG
and PG students separately.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class student
{
protected:
char name[20];
char USN[11];
int age;
public:
void getstudent();
};
//deriving a class from the class student
class UGstudent:public student
{
int sem;
float fees, stipend;
public:
void getug();
friend void average(UGstudent *ug,int n);
};
//deriving a class from the class student
class PGstudent:public student
{
int sem;
float fees, stipend;
public:
void getpg();
friend void average(PGstudent *pg, int m);
};
//Function to accept the name, usn and age of the student
void student::getstudent()
{
cout<<"enter name, usn and age"<<endl;
cin>>name>>USN>>age;
}
//Function to accept the UG student details
void UGstudent::getug()

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 36 of 48


C++ Lab Manual

{
cout<<"enter semester no, fees and stipend"<<endl;
cin>>sem>>fees>>stipend;
}
//Functin to accept the PG student details
void PGstudent::getpg()
{
cout<<"enter semester no, fees and stipend"<<endl;
cin>>sem>>fees>>stipend;
}
//Function to calculate the average age of UG students
void average(UGstudent ug[], int n)
{
int count[10], i;
float sum[10];
for(i=0;i<n;i++)
{
count[i]=0;
sum[i]=0;
}
for(i=0;i<n;i++)
{
sum[ug[i].sem-1]=sum[ug[i].sem-1]+ug[i].age;
count[ug[i].sem-1]++;
}
for(i=0;i<4;i++)
{
if(count[i]!=0)
{
cout<<"the average age of semester "<<i+1;
cout<<"is:"<<sum[i]/count[i]<<endl;
}
}
}
//Function to calculate the average of PG student
void average(PGstudent pg[], int m)
{
int count[10], i;
float sum[10];
for(i=0;i<m;i++)
{
count[i]=0;
sum[i]=0;
}
for(i=0;i<m;i++)
{
sum[pg[i].sem-1]=sum[pg[i].sem-1]+pg[i].age;
count[pg[i].sem-1]++;

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 37 of 48


C++ Lab Manual

}
for(i=0;i<4;i++)
{
if(count[i]!=0)
{
cout<<"the average age of semester "<<i+1;
cout<<"is:"<<sum[i]/count[i]<<endl;
}
}
}
void main()
{
UGstudent ug[10];
PGstudent pg[10];
int choice, n, m, i;
clrscr();
while(1)
{
cout<<"1.ug student\n2.pg student\n 3.exit"<<endl;
cout<<"enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"enter no.of ug students"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
ug[i].getstudent();
ug[i].getug();
}
average(ug, n);
break;
case 2:
cout<<"enter no.of ug students"<<endl;
cin>>m;
for(i=0;i<m;i++)
{
pg[i].getstudent();
pg[i].getpg();
}
average(pg, m);
break;
case 3:
exit(0);
default:
cout<<"enter correct choice"<<endl;
getch();

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 38 of 48


C++ Lab Manual

}
}
}

OUTPUT

1. UG student
2. PG student
3. Exit

Enter your choice: 1


Enter the no.of students : 2
Enter name, usn, and age :

Raj
1ox07mca11
25

Enter fees, semester, and stipend :

12000
1
1000

1. UG student
2. PG student
3. Exit

Enter name, usn, and age :

ram
1ox07mca12
24

Enter fees, semester, and stipend :

12000
1
1000

The average age of semester 1 is : 24 . 5

14. 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 <<.
a. STRING s1 = “VTU”
b. STRING s2 = “BELGAUM”
c . STRING s3 = s1 + s2 (Use copy constructor)

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 39 of 48


C++ Lab Manual

#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
private:
char s[20];
public:
string(char x[]);
string(string &x);
friend string operator+(string s1,string s2);
//string operator+(string s2);
friend ostream &operator<<(ostream &print,string x);
};

//parameterized constructor to initialize the string


string::string(char x[])
{
strcpy(s,x);
}
//copy constructor
string::string(string &x)
{

strcpy(s,x.s);
cout<<s;
}
//overloaded function for operator + to concatenate two strings
string operator +(string s1,string s2)
{
string temp(s1); //calling the copy constructor
strcat(temp.s, s2.s);
return temp;
}

/*string string:: operator +(string s2)


{
string t=strcat(s, s2.s);
return t;
}*/
//overloaded operator << function to display the string
ostream &operator <<(ostream &print,string x)
{
print<<x.s<<endl;
return print;
}

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 40 of 48


C++ Lab Manual

void main()
{
clrscr();
// string s1="VTU ";
string s1("vtu");
cout<<"first string is:"<<s1;
// string s2="BELGAUM";
string s2("belgaum");
cout<<"second string is:"<<s2;
string s3=s1+s2;
cout<<"resulted string is:"<<s3;
getch();
}

OUTPUT

First String : VTU


Second String : BELGAUM
The Resulted String is : VTUBELGAUM.

15. Write a C++ program to create a class called BIN_TREE (Binary Tree) with member functions to
perform in-order, preorder and post-order traversals. Create a BIN_TREE object and demonstrate
the traversals.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class tree
{
private:
struct node
{
node *llink;
int data;
node *rlink;
}*root;
public:
tree()
{
root=NULL;
}
void insert();
void call(int);
void preorder(node *);
void inorder(node *);

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 41 of 48


C++ Lab Manual

void postorder(node *);


};
void tree:: insert()
{
node *temp,*prev,*cur;
int item;
temp=new node;
if(temp==NULL)
{
cout<<"Memory allocation failed\n";
exit(0);
}
cout<<"Enter the element to be inserted";
cin>>item;
temp->data=item;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
root=temp;
else
{
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
cur=(item<cur->data)?cur->llink:cur->rlink;
cout<<"Current:"<<cur<<endl;
}
cout<<item<<"<"<<prev->data<<endl;
if(item<prev->data)
prev->llink=temp;
else
prev->rlink=temp;
}
}
void tree::preorder(node *root)
{
if(root!=NULL)
{
cout<<root->data<<"\t";
preorder(root->llink);
preorder(root->rlink);
}
}
void tree::inorder(node *root)
{
if(root!=NULL)

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 42 of 48


C++ Lab Manual

{
inorder(root->llink);
cout<<root->data<<"\t";
inorder(root->rlink);
}
}
void tree::postorder(node *root)
{
if(root!=NULL)
{
postorder(root->llink);
postorder(root->rlink);
cout<<root->data<<"\t";
}
}
void tree::call(int ch)
{
switch(ch)
{
case 1:
preorder(root);
break;
case 2:
inorder(root);
break;
case 3:
postorder(root);
break;
}
}
void main()
{
clrscr();
tree t;
int choice;
while(1)
{
cout<<"\n1.insert\n";
cout<<"2.preorder\n3.inorder\n4.postorder\n";
cout<<"5.exit\n";
cout<<"Enter your choice";
cin>>choice;
switch(choice)
{
case 1:
t.insert();
break;
case 2:

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 43 of 48


C++ Lab Manual

cout<<"Preorder traversal";
t.call(1);
break;
case 3:
cout<<"Inorder traversal";
t.call(2);
break;
case 4:
cout<<"postorder traversal:";
t.call(3);
break;
case 5:
exit(0);
default:
cout<<"Enter correct choide\n";
}
}
}
OUTPUT

1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice1
Enter the element to be inserted19

1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice1
Enter the element to be inserted15
Current:0x8fdd0000
15<19

1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice1
Enter the element to be inserted21
Current:0x8fdd0000
21<19

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 44 of 48


C++ Lab Manual

1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice1
Enter the element to be inserted40
Current:0x8fdd0efe
Current:0x8fdd0000
40<21

1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice2
Preorder traversal19 15 21 40
1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice3
Inorder traversal15 19 21 40
1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice4
postorder traversal:15 40 21 19
1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice5

16. Write a C++ program to create a class called EXPRESSION. Using appropriate member functions
convert a given valid infix expression into postfix form. Display the infix and postfix expressions.
PROGRAM
/*infix to postfix*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class EXP
{

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 45 of 48


C++ Lab Manual

char infix[50],postfix[50],stack[50];
int top;
public:
EXP()
{
top=-1;
}
void getinfix();
void infix_postfix();
void push(char);
int pop();
int preced(char);
void putpostfix();
};
void EXP::getinfix()
{
int len;
cout<<"Enter a valid expression";
cin>>infix;
len=strlen(infix);
infix[len]=')';
infix[++len]='\0';
}
void EXP::infix_postfix()
{
int i,j=0;
char symbol,temp;
push('(');
for(i=0;infix[i]!='\0';i++)
{
symbol=infix[i];
switch(symbol)
{
case'(':push(symbol);
break;
case ')':temp=pop();
while(temp!='(')
{
postfix[j]=temp;
j++;
temp=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':while(preced(stack[top])>=preced(symbol))

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 46 of 48


C++ Lab Manual

{
temp=pop();
postfix[j]=temp;
j++;
}
push(symbol);
break;
default: postfix[j++]=symbol;
break;
}
}
postfix[j]='\0';
}
void EXP::push(char symb)
{
top=top+1;
stack[top]=symb;
}
int EXP::pop()
{
char symb;
symb=stack[top];
top=top-1;
return(symb);
}
int EXP::preced(char symb)
{
switch(symb)
{
case '^':return 3;
case '*':
case '/':return 2;
case '+':
case '-':return 1;
default: return 0;
}
}
void EXP::putpostfix()
{
cout<<"POSTFIX EXP:"<<postfix;
}
void main()
{
EXP e;
clrscr();
e.getinfix();
e.infix_postfix();
e.putpostfix();

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 47 of 48


C++ Lab Manual

getch();
getch();
}
OUTPUT
Enter a valid expression(A*D)/(C^(E+B)-C)
POSTFIX EXP:AD*CEB+^C-/

2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 48 of 48

You might also like