Upload
hatuong
View
217
Download
2
Embed Size (px)
Citation preview
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 1
1. Given that an EMPLOYEE class contains following members: data members:
Employee number, Employee name, Basic, DA, IT, Net Salary and print data
members. Write a C++ program to read the data of N employee and compute Net
salary of each employee (DA=52% of Basic and Income Tax (IT) =30% of the
gross salary)
#include <iostream.h>
#include <conio.h>
class employee
{
char name[10];
int no;
float basic;
float da;
float it;
float ns;
float gs;
public:
void input()
{
cout <<"Enter number:";
cin >> no;
cout <<"Enter name:";
cin >> name;
cout <<"Enter salary:";
cin >> basic;
}
void calculate()
{
da = 0.52 * basic;
gs = da + basic;
it = 0.3 * gs;
ns = gs - it;
}
void output()
{
cout<<no<<'\t'<<name<<'\t'<<basic<<'\t'<<ns<<'\t'<<gs <<'\n';
}
};
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 2
void main()
{
employee emp[20];
int n,i;
clrscr();
cout << "Enter no of employees:";
cin >> n;
for(i=0;i<n;i++)
{
emp[i].input();
emp[i].calculate();
}
cout<<"NUMBER"<<'\t'<<"NAME"<<'\t'<<"BASIC"<<'\t'<<"NET"<<'\t'
<<"GROSS" << "\n";
for(i=0;i<n;i++)
{
emp[i].output();
}
getch();
}
OUTPUT: ENTER NUMBER OF EMPLOYEE: 2
ENTER EMPLOYEE NUMBER: 200
ENTER NAME : SURESH
ENTER BASIC SALARY: 5000
ENTER EMPLOYEE NUMBER:201
ENTER NAME: VINOD
ENTER BASIC SALARY: 6000
NUMBER NAME BASIC NET GROSS
100 SURESH 5000 5320 7600
101 VINOD 6000 6384 9120
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 3
2. Define a STUDENT class with USN, Name and MARKS in 3tests of subject. Declare
an array of 10 STUDENT objects. Using appropriate functions. Print USN, print the
USN, Name and the average marks of all the students.
#include <iostream.h>
#include <conio.h>
#define small(a,b) (a<b)?a:b
class student
{
int no ;
char name[10];
int s1;
int s2;
int s3;
float avg;
public :
void input()
{
cout <<"Enter USN: " ;
cin >> no;
cout << "Enter the name:";
cin >> name;
cout << "Subject 1:";
cin >> s1;
cout << "Subject 2:";
cin >> s2;
cout << "Subject 3:";
cin >> s3;
}
void calculate()
{
int s;
s =small (s1, (small (s2,s3) );
avg = (s1+s2+s3-s)/2 ;
}
void output()
{
cout<<no<<'\t'<<name<<'\t'<<s1<<'\t'<<s2<<'\t'<<s3<<'\t'<<avg<<’\n’;
}
};
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 4
void main()
{
student e[10];
int n,i;
clrscr();
cout << "Enter the no of students:";
cin >> n;
for(i=0;i<n;i++)
{
e[i].input();
e[i].calculate();
}
cout<<"USN"<<'\t'<<"NAME"<<'\t'<<"SUB1"<<'\t'<<" SUB2"<<'\t'<<" SUB3"
<<'\t'<<"AVG BEST 2"<<endl;
for(i=0;i<n;i++)
{
e[i].output();
}
getch();
}
OUTPUT:
ENTER THE NUMBER OF STUDENT: 2
ENTER USN: 1RN04MCA30
ENTER THE NAME : ANOOP
SUBJECT 1:42
SUBJECT 2:49
SUBJECT 3:44
ENTER USN:1RN04MCA31
ENTER THE NAME:RAMA
SUBJECT 1: 47
SUBJECT 2:49
SUBJECT 3:20
USN NAME SUB1 SUB2 SUB3 AVG BEST 2
1RN04MCA30 ANOOP 42 49 44 46.5
1RN04MCA31 RAMA 47 49 20 42.5
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 5
3. Write a C++ program to create a class called COMPLEX and implement the
following overloading functions ADD that return a COMPLEX number. I. ADD (a,
s2) - where a is an integer (real part) and s2 is a complex number. II. ADD (s1, s2)-
where s1 & s2 are complex numbers.
#include <iostream.h>
#include <conio.h>
#include <math.h>
class complex
{
int real;
int img;
public:
void input()
{
cout << "enter real and img part" << '\n';
cin >> real >> img;
}
void output()
{
if(img<0)
cout << real<< img << "i" << '\n';
else
cout << real << "+"<< "i" << img << '\n';
}
friend complex add(int,complex)
friend complex add(complex,complex)
};
complex add(int a , complex s2)
{
complex temp;
temp.real = s2.real + a;
temp.img = s2.img;
return temp;
}
complex add(complex s1, complex s2)
{
complex s3;
s3.real = s1.real + s2.real;
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 6
s3.img = s1.img + s2.img;
return s3;
}
void main()
{
complex s1,s2,sum1,sum2;
int a;
clrscr();
s1.input();
s2.input();
cout << "first complex number is:"<< '\n';
s1.output();
cout << "second complex no is:"<< '\n';
s2.output();
cout << "enter the value of a :";
cin >> a;
sum1 = add(a,s2);
sum2 = add(s1,s2);
cout << "output of integer and complex no is:"<< '\n';
sum1.output();
cout << "output of complex and complex no is:"<< '\n';
sum2.output();
getch();
}
OUTPUT:
ENTER REAL PART : 4
ENTER IMAGINARY PART: 8
ENTER REAL PART:3
ENTER IMAGINARY PART: -4
SUM OF 4+8i AND 3+-4i = 7+4i
ENTER AN INTEGER:5
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 7
4. Write a C++ program to create a class called LIST(linked list) with member function
to insert an element from the front of the list. Demonstrate all the function after
creating a list odject.
#include<iostream.h>
#include <conio.h>
#include <process.h>
struct node
{
int data;
struct node *link;
};
typedef struct node NODE;
class List
{
NODE *start;
public:
List( )
{ start=NULL; }
void insert_front();
void delete_front();
void display();
};
void List :: insert_front()
{
NODE *temp;
int item;
temp = new NODE;
if(temp==NULL)
{
cout<<endl<<"memory allocation failed"<<'\n';
exit(0);
}
cout<<"enter the data item to be inserted:";
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 8
cin>> item;
temp->data =item;
temp->link =start;
start = temp;
}
void List :: delete_front()
{
NODE *temp;
if(start==NULL)
{
cout<<"list does not exist"<<endl; return;
}
temp = start;
start = temp->link;
cout << "deleted item is: "<< temp->data<<'\n';
delete temp;
}
void List :: display()
{
NODE *temp;
if(start==NULL)
{
cout<<"list does not exist"<<'\n';
return;
}
temp = start;
cout << "data present is" <<'\n';
while(temp != NULL)
{
cout << temp->data<<'\n';
temp=temp->link;
}
}
void main()
{
List Lt;
int oper;
clrscr();
for(;;)
{
cout <<"1.insert 2.delete 3.display 4.exit"<<'\n';
cout<<"Enter your option :";
cin>> oper;
switch(oper)
{
case 1: Lt.insert_front();
break;
case 2: Lt.delete_front();
break;
case 3: Lt.display();
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 9
break;
case 4:
default: exit(0);
}
}
}
OUTPUT: 1.Insert front
2.Delete front
3.Display
4.Exit
Enter your option:1
Enter the element:36
1.Insert front
2.Delete front
3.Display
4.Exit
Enter your option:1
Enter the element:80
1.Insert front
2.Delete front
3.Display
4.Exit
Enter your option:1
Enter the element:44
1.Insert front
2.Delete front
3.Display
4.Exit
Enter your option:3
Content of the list are:
44
80
36
1.Insert front
2.Delete front
3.Display
4.Exit
Enter your option:2
Deleted element is:44
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 10
5. Write a C++ program to create a template function for Quick sort & demonstrate
sorting of integer & double.
#include<iostream.h>
#include<conio.h>
template<class x>
void quick(x a[], int low, int high)
{
register int pos;
if(low<high)
{
pos=partition(a,low,high);
quick(a,low,pos-1);
quick(a,pos+1,high);
}
return;
}
template<class x>
int partition(x*a,int low,int high)
{
x key,temp;
int left,right,true=1;
key=a[low];
left=low+1;
right=high;
while(true)
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 11
{
while((left<high)&&(key>=a[left]))
left++;
while(key<a[right])
right--;
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);
}
}
return 0;
}
void main()
{
int n,low,high,i;
int a[10];
double b[10];
clrscr();
cout<<"Enter the size of integer array";
cin>>n;
cout<<"Enter the elements of first array";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the elements of double";
for(i=0;i<n;i++)
cin>>b[i];
low=0;
high=n-1;
quick(a,low,high);
quick(b,low,high);
cout<<"The sorted array is";
for(i=0;i<n;i++)
cout<<endl<<a[i];
cout<<'\n'<<"The sorted double array is";
for(i=0;i<n;i++)
cout<<endl<<b[i];
getch();
}
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 12
6. Write a C++ program to create a class called STACK using an array of integer.
Implement the following operations overloading operators + &-.
i) 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.
ii) s1=s1--, where s1 is an object of the class STACK full conditions. Also display the
content of stack after each operation by overloading the operator<<.
#include <iostream.h>
#include <conio.h>
#include <process.h>
#define max 5
class stack
{
int top,s[max];
public:
stack()
{
top=-1;
}
stack operator +(int n)
{
OUTPUT:
Enter array size:
5
Enter the elements of integer array
38
234
-34
764
3
Enter the elements of Double array
45.4
23.98
232.78
-34.4
284.4
The sorted list of integers:
-34 3 38 234 764
The sorted list of Doubles:
-34.4 23.98 45.4 232.78 284.4
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 13
if(top==max-1)
{
cout<<"stack is full"<<endl;
}
top++;
s[top]=n;
cout<<(*this);
return *this;
}
stack operator--()
{
int n;
if(top==-1)
{
cout<<"stack is empty"<<endl;
return *this;
}
n=s[top];
cout<<"deleted element is:";
cout<<n<<endl;
top--;
cout<<(*this);
return *this;
}
friend void operator <<(ostream &, stack );
};
void operator<<(ostream &ct, stack st)
{
int i ;
if(st.top == -1)
ct<<"stack is empty"<<endl;
for(i=st.top;i>=0;i--)
ct<<st.s[i]<<endl;
}
void main()
{
stack s1,s2;
int opt,a;
clrscr();
cout<<"stack operations"<<endl;
for(;;)
{
cout<<"1.push 2.pop 3.display 4.exit"<<endl;
cout<<"enter ur option"<<endl;
cin>>opt;
switch(opt)
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 14
{
case 1 : cout <<"enter the element to be inserted"<<endl;
cin>>a;
s1=s2 +a;
break;
case 2 : --s1;
break;
case 3 : cout<<s1;
break;
case 4 :
default: exit(0);
}
}
}
OUTPUT:
1.Push
2.pop
3.Exit
Enter your option:1
Enter the element:54
Content of the stack:
54
1.Push
2.pop
3.Exit
Enter your option:1
Enter the element:78
Content of the stack:
78
54
1.Push
2.pop
3.Exit
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 15
7. Write a C++ program to create a class called DATE. Accept two valid dates in the
from of dd/mm/yyyy. Implement the following operation by overloading the operator
+ & -. After every operation display the results by overloading the operator<<
i) no_of_days=d1-d2, where d1 and d2 are DATE objects, d1>=d2, no_of_days is an
integer.
ii) d2=d1+no_of_days, where d1 is DATE object and no_of_days is an integer.
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 20
8). Write a C++ program to create a class 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 operators + and – respectively. Display the result by
overloading the operator<<.
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 21
#include <iostream.h>
#include<conio.h>
class matrix
{
int a[5][5];
int row, col;
public:
void read_order()
{
cin>>row>>col;
}
void read_matrix()
{
int i,j;
for(i=0; i<row; i++)
for(j=0; j<col; j++)
cin>>a[i][j];
}
int operator ==(matrix mt)
{
if(row == mt.row && col == mt.col)
{
cout<<"matrix addition and subtraction is possible"<<endl;
return 1;
}
return 0;
}
matrix operator +(matrix mp)
{
matrix temp;
int i,j;
for(i=0; i<row; i++)
for(j=0; j<col; j++)
temp.a[i][j]=a[i][j] + mp.a[i][j];
temp.row= row;
temp.col= col;
return temp;
}
matrix operator -(matrix ms)
{
matrix temp;
int i,j;
for(i=0; i<row; i++)
for(j=0; j<col; j++)
temp.a[i][j] = a[i][j] - ms.a[i][j];
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 22
temp.row = row;
temp.col = col;
return temp;
}
friend void operator <<(ostream &, matrix)
};
void operator <<(ostream &ct, matrix mb)
{
int i,j;
for(i=0; i<mb.row; i++)
{
for(j=0; j<mb.col; j++)
{
ct<<mb.a[i][j]<<'\t';
}
ct<<endl;
}
}
void main()
{ matrix m1,m2,m3,m4;
clrscr();
cout<<"enter the order of the first matrix"<<endl;
m1.read_order();
cout<<"enter the order ofthe second matrix"<<endl;
m2.read_order();
if(m1==m2)
{
cout<<"enter the elements of the first matrix"<<endl;
m1.read_matrix();
cout<<"enter the elements of the second matrix"<<endl;
m2.read_matrix();
cout<<"matrix 1"<<endl;
cout<<m1;
cout<<"matrix 2"<<endl;
cout<<m2;
m3=m1+m2;
m4=m1-m2;
cout<<"the sum of 2 matrix is ";
cout<<endl<<m3;
cout<<endl<<"the difference of 2 matrix is ";
cout <<endl<<m4;
}
else
cout<<"matrix addition is not possible"<<endl;
}
OUTPUT:
ENTER THE NUMBER OF ROWS:3
ENTER THE NUMBER OF COLOUMNS:2
ENTER THE NUMBER OF ROWS:2
ENTER THE NUMBER OF COLOUMNS:3
INVALID INPUT
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 24
9. Write a C++ program to create a class called OCTAL which has the characteristics
of an octal number. Implement the following operation by writing an appropriate
constructor and an overloaded operator +.
i) OCTAL h = x; where x is an integer.
ii) 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 value of h and y.
#include <iostream.h>
#include <conio.h>
class octal
{
int oform;
public:
octal (int x);
int operator + (int x);
friend void operator <<(ostream &print,octal x)
};
octal :: octal(int x)
{
int rem ,c=1;
oform = 0;
while (x>0)
{
rem = x % 8;
x = x/8;
oform = oform + rem *c;
c = c * 10;
}
}
int octal :: operator +(int x)
{
int r,temp=oform;
int oform=0,c=1;
while(temp)
{
r = temp %10;
temp = temp / 10;
oform = oform +r *c;
c = c *8;
}
return oform +x;
}
void operator <<(ostream &print,octal x)
{
print<<x.oform;
}
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 25
void main()
{
int x,y,k;
clrscr();
cout<<"enter the value"<<endl;
cin >>x;
octal h = x;
cout <<"the oform of x is:"<<h;
cout <<endl<<"enter the value of integer k"<<endl;
cin>>k;
y=h+k;
cout <<"the value added to the given no is :"<<endl;
cout<<y;
getch();
}
OUTPUT:
ENTER AN INTEGER:150
THE OCTAL EQUIVALENT OF 150 IS: 226
ENTER THE VALUE OF INTEGER K: -3
THE VALUE ADDED TO THE GIVEN NO IS: 147
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 26
10 Write a C++ program to create a class called QUEUE with member function to add
an element and to delete an element from the queue of integer and double. Demonstrate
the operation by displaying the content of the queue after every operation.
#include <iostream.h>
#include <conio.h>
#include <process.h>
#define max 5
template <class Q>class QUEUE
{
int f,r;
Q q[max];
public:
QUEUE()
{
f=-1;
r=-1;
}
void insert(Q);
void del();
}
template <class Q> void QUEUE<Q>::insert(Q item)
{
if (r==max-1)
{
cout<<"queue is full"<<endl;
return;
}
r++;
q[r]=item;
if(f==-1)
f++;
if(f==-1)
{
cout<<"queue is empty"<<endl;
return;
}
cout<<"contents of the queue"<<endl;
for(int i=f;i<=r;i++)
cout<<q[i]<<endl;
}
template<class Q>void QUEUE<Q>::del()
{
Q item;
if(f==-1)
{
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 27
cout<<"queue is empty"<<endl;
return;
}
item =q[f];
cout<<"deleted element is"<<'\n'<<item<<endl;
if(f==r)
f=r=-1;
else
f++;
if(f==-1)
{
cout<<"queue is empty"<<endl;
return;
}
cout<<"contents of the queue"<<endl;
for(int i=f;i<=r;i++)
cout<<q[i]<<endl;
}
void main()
{
QUEUE<int>x;
QUEUE<double>y;
int ch,i;
double d;
clrscr();
cout<<"queue operations"<<endl;
for(;;)
{
cout<<"1.insert integer 2.delete integer"<<endl;
cout<<"3.insert double 4.delete double "<<endl;
cout<<"5.exit"<<endl;
cout<<"enter ur option"<<endl;
cin>>ch;
switch(ch)
{
case 1 : cout<<"enter an integer"<<endl;
cin>>i;
x.insert(i);
break;
case 2 : x.del();
break;
case 3 : cout<<"enter a double"<<endl;
cin>>d;
y.insert(d);
break;
case 4 : y.del();
break;
case 5 :
default: exit(0);
}
}
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 28
getch();
}
OUTPUT:
“QUEUE OPERATIONS:”
1.INSERT INTEGER 2.DELETE INTEGER
3. ISERT DOUBLE 4.DELETE DOUBLE
5.EXIT
ENTER YOUR OPTION:1
ENTER AN INTEGER: 45
CONTENT OF THE QUEUE:
45
1.INSERT INTEGER 2.DELETE INTEGER
3. ISERT DOUBLE 4.DELETE DOUBLE
5.EXIT
ENTER YOUR OPTION:1
ENTER AN INTEGER: 65
CONTENT OF THE QUEUE:
45
65
1.INSERT INTEGER 2.DELETE INTEGER
3. ISERT DOUBLE 4.DELETE DOUBLE
5.EXIT
ENTER YOUR OPTION:3
ENTER AN DOUBLE:33.65
CONTENT OF THE QUEUE:
33.65
1.INSERT INTEGER 2.DELETE INTEGER
3. ISERT DOUBLE 4.DELETE DOUBLE
5.EXIT
ENTER YOUR OPTION:3
ENTER AN DOUBLE: 45.65
CONTENT OF THE QUEUE:
33.65
45.65
1.INSERT INTEGER 2.DELETE INTEGER
3. ISERT DOUBLE 4.DELETE DOUBLE
5.EXIT
ENTER YOUR OPTION:2
DELETED ELEMENT=45
CONTENT OF THE QUEUE:
65
1.INSERT INTEGER 2.DELETE INTEGER
3. ISERT DOUBLE 4.DELETE DOUBLE
5.EXIT
ENTER YOUR OPTION:4
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 29
11. Write a C++ program to create a class called DLIST (Double Linked List) with
member function to insert a node at a specified position and delete a node from a
specified position of the list. Demonstrate the operation by displaying the content of the
list after ever operation.
#include <iostream.h>
#include <conio.h>
struct node
{
int data;
struct node *llink;
struct node *rlink;
};
typedef struct node NODE;
class dlist
{
NODE *start;
public:
dlist()
{
start=NULL;
}
~dlist()
{
delete start;
}
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;
temp=new NODE;
if(temp==NULL)
{
cout<<"memory allocation failed"<<endl;
return;
}
temp->data =item;
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 30
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;
temp->llink=prev;
temp->rlink=cur;
cur->llink=temp;
}
else
cout<<"invalid position"<<endl;
}
void dlist :: delete_pos(int pos)
{
NODE *next,*temp,*prev,*cur;
int p;
if(start==NULL)
{
cout<<"list is empty";
return;
}
if(pos==1)
{
temp=start;
start=start->rlink;
start->llink=NULL;
cout<<"delted element is"<<temp->data<<endl;
delete temp;
return;
}
p=1;
prev=NULL;
cur=start;
while(p!=pos&&cur!=NULL)
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 31
{
prev =cur;
cur=cur->rlink;
p++;
}
if(p==pos)
{
next=cur->rlink;
prev->rlink=next;
next->llink=prev;
cout<<"deleted element is"<<cur->data<<endl;
delete cur;
return;
}
else
cout<<"invalid position"<<endl;
return;
}
void dlist::display()
{
NODE *temp;
if(start==NULL)
{
cout<<"list is empty"<<endl;
return;
}
temp=start;
cout<<"contents of the list"<<endl;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->rlink;
}
}
void main()
{
dlist obj;
int item,pos,ch;
cout<<"doubly linked list operations"<<endl;
for(;;)
{
cout<<"1.insert at position 2.delete from position"<<endl;
cout<<"3.display 4.exit"<<endl;
cout<<"enter ur option"<<endl;
cin>>ch;
switch(ch)
{
case 1 :cout<<"enter the item"<<endl;
cin>>item;
cout<<"enter the position"<<endl;
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 32
cin>>pos;
obj.insert_pos(pos,item);
break;
case 2 :cout<<"enter the positiom"<<endl;
cin>>pos;
obj.delete_pos(pos);
break;
case 3 :obj.display();
break;
case 4 :return;
}
}
getch();
}
OUTPUT:
1.INSERT AT POSITION
2.DELETE FROM POSITION
3.DISPLAY
4.EXIT
ENTER YOUR OPTION:1
ENTER THE ITEM:653
ENTER POSITION:1
CONTENT OF LIST:
653
1.INSERT AT POSITION
2.DELETE FROM POSITION
3.DISPLAY
4.EXIT
ENTER YOUR OPTION:1
ENTER THE ITEM:44
ENTER POSITION:2
CONTENT OF LIST:
653
44
1.INSERT AT POSITION
2.DELETE FROM POSITION
3.DISPLAY
4.EXIT
ENTER YOUR OPTION:1
ENTER THE ITEM:54
ENTER POSITION:3
CONTENT OF THE LIST:
653
44
54
1.INSERT AT POSITION
2.DELETE FROM POSITION
3.DISPLAY
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 33
12. Write a C++ program to create a class called STUDENT with data memebres USN,
Name & Age. Using inheritance, create the classes UGSTUDENT & PGSTUDENT
having fields as semester wise average age for all UG & PG student separately.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class STUDENT
{
protected:
char name[20];
char usn[11];
int age;
public:
void readST();
};
class UGSTUDENT:public STUDENT
{
int sem;
float fees,stipend;
public:
void readUG();
friend void avg(UGSTUDENT *,int);
};
class PGSTUDENT: public STUDENT
{
int sem;
float fees,stipend;
public:
void readPG();
friend void avg(PGSTUDENT *,int);
};
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 34
void STUDENT::readST()
{
cout<<"\n Enter Name\n";
cin>>name;
cout<<"Enter USN\n";
cin>>usn;
cout<<"Enter age\n";
cin>>age;
}
void UGSTUDENT::readUG()
{
cout<<"\n Enter semester\n";
cin>>sem;
cout<<"Enter Fees\n";
cin>>fees;
cout<<"Enter stipend\n";
cin>>stipend;
}
void PGSTUDENT::readPG()
{
cout<<"\n Enter semester\n";
cin>>sem;
cout<<"Enter Fees\n";
cin>>fees;
cout<<"Enter stipend\n";
cin>>stipend;
}
void avg(UGSTUDENT ug[],int n)
{
int i,count[10];
float sum_age[10];
for(i=0;i<10;i++)
{
count[i]=0;
sum_age[i]=0.0;
}
for(i=0;i<n;i++)
{
sum_age[ug[i].sem-1]+=ug[i].age;
count[ug[i].sem-1]++;
}
for(i=0;i<8;i++)
{
if(count[i]!=0)
{
cout<<"The average age of sem\n"<<i+1;
cout<<"is:"<<sum_age[i]/count[i]<<endl;
}
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 35
}
}
void avg(PGSTUDENT pg[],int m)
{
int i,count[10];
float sum_age[10];
for(i=0;i<10;i++)
{
count[i]=0;
sum_age[i]=0.0;
}
for(i=0;i<m;i++)
{
sum_age[pg[i].sem -1] += pg[i].age;
count[pg[i].sem-1]++;
}
for(i=0;i<4;i++)
{
if(count[i]!=0)
{
cout<<"The average age of sem\n"<<i+1;
cout<<"is:"<<sum_age[i]/count[i]<<endl;
}
}
}
void main()
{
UGSTUDENT ug[10];
PGSTUDENT pg[10];
int ch,n,m,i;
clrscr();
for(;;)
{
cout<<"1.UG student\n2.PG student\n3.Exit\n";
cout<<"Enter ur choice\n";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter no of UG students:\n";
cin>>n;
for(i=0;i<n;i++)
{
ug[i].readST();
ug[i].readUG();
}
avg(ug,n);
break;
case 2: cout<<"Enter no of PG students:\n";
cin>>m;
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 36
for(i=0;i<m;i++)
{
pg[i].readST();
pg[i].readPG();
}
avg(pg,m);
break;
default:exit(0);
}
}
}
OUTPUT:
1.UG STUDENT
2.PG STUDENT
3.EXIT
ENTER YOUR CHOICE:1
ENTER THE NUMBER OF UG STUDENTS:2
ENTER NAME:ANIL
ENTER USN:1RN04EC05
ENTER AGE:23
ENTER SEMESTER:3
ENTER FEES:20000
ENTER STIPEND:1100
ENTER NAME:VIJAY
ENTER USN:1RN04IS05
ENTER AGE:20
ENTER SEMESTER:1
ENTER FEES:25000
ENTER STIPEND:1000
THE AVERAGE AGE OF SEMESTER
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 37
13. Write a C++ program to create a class STRING and implement the following
operations. Display the results after every operation by overloading the operator <<.
i) STRING s1=”VTU”
ii) STRING s2=”BELGUAM”
iii) STRING s3=s1+s2(Use Copy Constructor)
#include <iostream.h>
#include<conio.h>
#include <string.h>
class string
{
char s[20];
public:
string
{
strcpy(str,” “);
}
string(string &st)
{
strcpy(s,st.s);
}
void input()
{
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 38
cout<<"enter the string"<<endl;
cin>>s;
}
string operator +(string sp)
{
string temp;
strcpy(temp.s,s);
strcat(temp.s,sp.s);
return temp;
}
friend void operator <<(ostream &,string)
};
void operator<<(ostream&,string sr)
{
c<<sr.s;
}
void main()
{
string s1,s2;
clrscr();
s1.input();
s2.input();
string s3 = s1 + s2;
cout<< "string s1 :"<<s1;
cout<<endl<<"string s2 :"<<s2;
cout<<endl<<"the concatented string :"<<s3;
getch();
}
OUTPUT:
S1=VTU
S2=BELGAUM
S1+S2=VTUBELGAUM
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 39
14. Write a C++ program to create a class called BIN_TREE (binary tree) with member
function to perform in-order, pre-order, post-order traversals. Create a BIN_TREE object
and demonstrate the traversals.
#include <iostream.h>
#include <conio.h>
#include <process.h>
struct node
{
int data;
struct node *llink;
struct node *rlink;
};
typedef struct node NODE;
class tree
{
NODE *root;
public:
tree()
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 40
{
root =NULL;
}
void insert();
void call(int);
void preorder(NODE *);
void postorder(NODE *);
void inorder(NODE *);
};
void tree :: insert()
{
NODE *temp,*prev,*cur;
int item;
temp = new NODE;
if(temp==NULL)
{
cout<<"memory allocation failed"<<endl;
exit(0);
}
cout<<"enter the element to be inserted"<<endl;
cin>>item;
temp->data =item;
temp->rlink =NULL;
temp->llink =NULL;
if(root==NULL)
root =temp;
else
{
prev =NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
cur=(item<cur->data)?cur->llink:cur->rlink;
}
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)
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 41
{
if(root!=NULL)
{
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()
{
tree t;
int opt;
clrscr();
for(;;)
{
cout<<endl<<"1.insert an element "<<endl;
cout<<"2.pre order traversal"<<endl;
cout<<"3.inorder traversal"<<endl;
cout<<"4.postorder traversal"<<endl;
cout<<"5.exit"<<endl;
cout<<"enter ur option"<<endl;
cin>>opt;
switch(opt)
{
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 42
case 1:t.insert();
break;
case 2:cout<<"preorder traversal"<<endl;
t.call(1);
break;
case 3:cout<<"inorder traversal"<<endl;
t.call(2);
break;
case 4:cout<<"postorder traversal"<<endl;
t.call(3);
break;
case 5:
default: exit(0);
}
}
}
OUTPUT:
1.INSERT AN ELEMENT TO TREE
2.PRE-ORDER TRAVERSAL
3.IN-ORDER TRAVERSAL
4.POST-ORDER TRAVERSAL
5.EXIT
ENTER YOUR OPTION:1
ENTER AN ELEMENT TO BE INSERTED:34
1.INSERT AN ELEMENT TO TREE
2.PRE-ORDER TRAVERSAL
3.IN-ORDER TRAVERSAL
4.POST-ORDER TRAVERSAL
5.EXIT
ENTER YOUR OPTION:1
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 43
1.INSERT AN ELEMENT TO TREE
2.PRE-ORDER TRAVERSAL
3.IN-ORDER TRAVERSAL
4.POST-ORDER TRAVERSAL
5.EXIT
ENTER YOUR OPTION:1
ENTER AN ELEMENT TO BE INSERTED:30
1.INSERT AN ELEMENT TO TREE
2.PRE-ORDER TRAVERSAL
3.IN-ORDER TRAVERSAL
4.POST-ORDER TRAVERSAL
5.EXIT
ENTER YOUR OPTION:2
PRE-ORDER TRAVERSAL
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 44
15. Write a C++ program to create a class called EXPRESSION. Using appropriate
member functions to convert a given valid infix expression to postfix expression form,
Display the infix and postfix expression.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class expression
{
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 45
char infix[15],postfix[15],s[15];
int top;
public:
expression()
{
top=-1;
}
void read()
{
cout<<"enter a vaild infix expression"<<endl;
cin>>infix;
}
int inputpre(char);
int stackpre(char);
char pop();
void infix_to_postfix();
void push(char);
void display();
};
int expression::inputpre(char sym)
{
switch(sym)
{
case '+':
case '-':return 1;
case '*':
case '/':return 3;
case '^':
case '$':return 6;
case '(':return 9;
case ')':return 0;
default:return 7;
}
}
int expression::stackpre(char sym)
{
switch(sym)
{
case '+':
case '-':return 2;
case '*':
case '/':return 4;
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 46
case '^':
case '$':return 5;
case '(':return 0;
case '#':return -1;
default:return 8;
}
}
void expression::push(char item)
{
s[++top]=item;
return;
}
char expression::pop()
{
return s[top--];
}
void expression::infix_to_postfix()
{
int i,j=0;
char sym;
push('#');
for(i=0;i<strlen(infix);i++)
{
sym=infix[i];
while(stackpre(s[top])>inputpre(sym))
{
postfix[j]=pop();
j++;
}
if(stackpre(s[top])!=inputpre(sym))
push(sym);
else
pop();
}
while(s[top]!='#')
{
postfix[j++]=pop();
}
}
void expression::display()
{
cout<<"the postfix expression is:"<<postfix<<endl;
}
void main()
{
expression exp;
clrscr();
exp.read();
exp.infix_to_postfix();
Downloaded from www.pencilji.com
Downloaded from www.pencilji.com 47
exp.display();
getch();
}
OUTPUT:
ENTER A VALID INFIX EXPRESSION: (A+B)*C-D
THE POSTFIX EXPRESSION IS: AB+C*D-
ENTER A VALID INFIX EXPRESSION:(((P+W)-Z)^(D/N)*U)
THE POSTFIX EXPRESSION IS : PW+Z-DN/^U*