c++ lab programs

37
1. // GIVEN THAT AN "EMPLOYEE" CLASS CONTAINS THE FOLLOWING MEMBERS : // DATA MEMBER : Employee_Number,Employee_Name,Basic,DA,IT,Net_Sal. // MEMBER FUNCTION : To read data, to calculate net salary and to print // data members. #include<iostream.h> #include<conio.h> class emp { int empno; char empname[20]; float basic,da,it,gs,ns; public : void read() { cout<<"\n Enter the employee number : "; cin>>empno; cout<<"\n Enter the employee name : "; cin>>empname; cout<<"\n Enter basic salary : "; cin>>basic;

Upload: prakash-kumar-singh

Post on 27-Aug-2014

53 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: c++ Lab Programs

1.

// GIVEN THAT AN "EMPLOYEE" CLASS CONTAINS THE FOLLOWING MEMBERS :

// DATA MEMBER : Employee_Number,Employee_Name,Basic,DA,IT,Net_Sal.

// MEMBER FUNCTION : To read data, to calculate net salary and to print

// data members.

#include<iostream.h>

#include<conio.h>

class emp

{

int empno;

char empname[20];

float basic,da,it,gs,ns;

public :

void read()

{

cout<<"\n Enter the employee number : ";

cin>>empno;

cout<<"\n Enter the employee name : ";

cin>>empname;

cout<<"\n Enter basic salary : ";

cin>>basic;

}

Page 2: c++ Lab Programs

void cal_netsal()

{

da=basic*(52.0/100.0);

gs=basic+da;

it=gs*(30.0/100.0);

ns=basic+da-it;

}

void display()

{

cout<<" \n "<<empno;

cout<<"\t\t"<<empname;

cout<<" \t"<<basic;

cout<<" \t"<<da;

cout<<" \t"<<it;

cout<<" \t"<<ns;

}

};

void main()

{

emp e;

clrscr();

cout<<"\n Employee Salary Details ";

e.read();

Page 3: c++ Lab Programs

e.cal_netsal();

cout<<"\n Emp_No. \t Name \t Basic \t Da \t It \t Net Salary ";

cout<<"\n ------------------------------------------------------- ";

e.display();

cout<<"\n ------------------------------------------------------- ";

getch();

}

2.

// WRITE A C++ PROGRAM TO READ DATA OF N EMPLOYEES AND COMPUTE THE NET //

// SALARY OF EACH EMPLOYEE ( DA = 52% OF BASIC AND INCOME TAX = 30% OF //

// THE GROSS SALARY ) //

#include<iostream.h>

#include<conio.h>

class employee

{

int empno;

char empname[20];

float basic,da,it,ns,gs;

public :

void read()

{

Page 4: c++ Lab Programs

cout<<"\n Enter the Employee Number : ";

cin>>empno;

cout<<"\n Enter the Employee Name : ";

cin>>empname;

cout<<"\n Enter the Basic Salary : ";

cin>>basic;

}

void cal_netsal()

{

da=basic*0.52;

gs=basic+da;

it=gs*0.30;

ns=gs-it;

}

void display()

{

cout<<"\n"<<empno;

cout<<"\t"<<empname;

cout<<"\t"<<basic;

cout<<"\t"<<da;

cout<<"\t"<<it;

cout<<"\t"<<ns;

}

Page 5: c++ Lab Programs

};

void main()

{

int n,i;

employee e[20];

clrscr();

cout<<"\n Enter the number of Employee's : ";

cin>>n;

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

{

e[i].read();

e[i].cal_netsal();

}

cout<<"\n Emp_No \t Emp_Name \t Basic \t Da \t IT \t Net Salary ";

cout<<"\n ----------------------------------------------------- ";

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

{

e[i].display();

}

getch();

}

Page 6: c++ Lab Programs

3.

// DEFINE A STUDENT CLASS WITH USN, NAME & 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>

#include<stdlib.h>

class student

{

char usn[10];

char name[20];

int m1,m2,m3;

float avg;

public :

void read()

{

cout<<"\n Enter usn of student : ";

cin>>usn;

cout<<"\n Enter name of the student : ";

cin>>name;

cout<<"\n Enter the 3 subject marks : ";

cin>>m1>>m2>>m3;

Page 7: c++ Lab Programs

}

void average()

{

avg=0;

if(m1<=m2 && m1<=m3)

{

avg=(m2+m3)/2.0;

}

if(m2<=m1 && m2<=m3)

{

avg=(m1+m3)/2.0;

}

if(m3<=m1 && m3<=m2)

{

avg=(m3+m2)/2.0;

}

}

void display()

{

cout<<"\n"<<usn;

cout<<"\t"<<name;

cout<<"\t"<<m1;

cout<<"\t"<<m2;

Page 8: c++ Lab Programs

cout<<"\t"<<m3;

cout<<"\t"<<avg;

}

};

void main()

{

student s[10];

int n=0,i;

char ch='y';

clrscr();

while(ch=='y'||ch=='Y')

{

s[n].read();

s[n].average();

n++;

cout<<"\n Enter More Records (y/n) :";

cin>>ch;

}

cout<<"\n Student Records are : ";

cout<<"\n USN \t NAME \t TEST1 \t TEST2 \t TEST3 \t AVERAGE \n";

cout<<"_______________________________________________________";

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

s[i].display();

getch();

Page 9: c++ Lab Programs

}

4.

// WRITE A C++ PROGRAM TO CREATE A CLASS CALLED COMPLEX AND IMPLEMENT THE //

// FOLLOWING OVERLOADING FUNCTION ADD THAT RETURNS 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. //

#include<iostream.h>

#include<stdlib.h>

//#include<process.h>

#include<conio.h>

class complex

{

float real,imag;

public :

void read()

{

cout<<"\n Enter the real and imaginary part : ";

cin>>real>>imag;

}

complex add(complex c2)

{

Page 10: c++ Lab Programs

complex t;

t.real=real+c2.real;

t.imag=imag+c2.imag;

return t;

}

complex add(int n)

{

complex t;

t.real=real+n;

t.imag=imag;

return t;

}

void display()

{

if(imag<0)

{

cout<<"\n Sum = "<<real<<"-"<<imag<<"i"<<endl;

}

else

{

cout<<"\n Sum = "<<real<<"+"<<imag<<"i"<<endl;

}

};

Page 11: c++ Lab Programs

void main()

{

complex c1,c2,c3;

int n,ch;

clrscr();

//cout<<"\n 1. Add two Complex Numbers.";

//cout<<"\n 2. Add Complex Number and an Integer.";

//cout<<"\n 3. Exit.";

do

{

cout<<"\n 1. Add two Complex Numbers.";

cout<<"\n 2. Add Complex Number and an Integer.";

cout<<"\n 3. Exit.";

cout<<"\n Enter your choice : ";

cin>>ch;

switch(ch)

{

case 1 : cout<<"\n Enter first complex number : ";

c1.read();

cout<<"\n Enter second complex number : ";

c2.read();

c3=c1.add(c2);

c3.display();

break;

Page 12: c++ Lab Programs

case 2 : c1.read();

cout<<"\n Add integer number ";

cin>>n;

c3=c1.add(n);

c3.display();

break;

case 3 : exit(0);

}

}while(ch!=3);

getch();

}

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 ROM THE FRONT OF THE LIST. DEMONSTRATE ALL THE FUNCTIONS //

// AFTER CREATING A LIST OBJECT. //

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

Page 13: c++ Lab Programs

class node

{

public :

int info;

node *ptr;

};

class list

{

node *head;

public :

list()

{

head=NULL;

}

void insfront();

void delfront();

void display();

};

void list::insfront()

{

node *newnode;

newnode=new node;

int ele;

Page 14: c++ Lab Programs

cout<<"\n Enter the element to insert : ";

cin>>ele;

newnode->info=ele;

newnode->ptr=head;

head=newnode;

}

void list::delfront()

{

if(head==NULL)

{

cout<<"\n The list is empty ! Nothing to Display ";

}

else

{

head=head->ptr;

cout<<"\n The first element is deleted";

}

}

void list::display()

{

node *t;

if(head==NULL)

{

Page 15: c++ Lab Programs

cout<<"\n The list is empty ! Nothing to display";

}

else

{

t=head;

cout<<"\n The contents of the list are : ";

while(t!=NULL)

{

cout<<t->info<<" ";

t=t->ptr;

}

}

}

void main()

{

int ch;

list l;

clrscr();

do

{

cout<<"\n 1. Insert Front. \n 2. Delete Front \n 3. Display \n 4. Exit";

cout<<"\n Enter your choice : ";

cin>>ch;

switch(ch)

Page 16: c++ Lab Programs

{

case 1: l.insfront();

break;

case 2: l.delfront();

break;

case 3: l.display();

break;

case 4: exit(0);

default: cout<<"\n Invalid Input";

}

}while(ch!=4);

getch();

}

7.

// WRITE A C++ PROGRAM TO CREATE A STACK 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' & ELEMENT //

// IS AN INTEGER TO BE PUSHED ON THE TOP OF THE STACK. //

// (B). S1-;WHERE S1 IS AN OBJECT OF THE CLASS STACK '-' OPERATOR POPS THE //

Page 17: c++ Lab Programs

// 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<stdlib.h>

#include<conio.h>

#define MAX 5

int n;

class stack

{

int st[MAX];

public :

int top;

stack()

{

top=-1;

}

void operator+(int);

int operator-(int);

friend ostream & operator<<(ostream &,stack &)

};

void stack::operator+(int ele)

{

Page 18: c++ Lab Programs

st[++top]=ele;

}

int stack::operator-(int)

{

n=st[top--];

return n;

}

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

{

for(int i=s.top;i>=0;i--)

cout<<"\n"<<s.st[i];

return out;

}

void main()

{

stack s;

int ch,ele;

clrscr();

cout<<"\n Operations on the stack are : ";

cout<<"\n 1. PUSH. \n 2. POP \n 3. DISPLAY \n 4. EXIT \n";

do

{

Page 19: c++ Lab Programs

cout<<"\n Enter your choice : ";

cin>>ch;

switch(ch)

{

case 1: if(s.top==MAX-1)

cout<<"\nStack Overflow ";

else

{

cout<<"\n Enter the element to insert : ";

cin>>ele;

s+ele;

}

break;

case 2: if(s.top==-1)

cout<<"\n Stack Underflow";

else

{

s-1;

cout<<"\n Deleted Element is "<<n;

}

break;

case 3: if(s.top==-1)

cout<<"\n Stack is Empty";

Page 20: c++ Lab Programs

else

{

cout<<"\n The content of stack are ";

cout<<s;

}

break;

case 4: exit(0);

default: cout<<"\nInvalid choice";

}

}while(ch!=4);

getch();

}

9.

#include<iostream.h>

#include<conio.h>

class matrix

{

int m[5][5];

int r,c;

public: matrix()

{

Page 21: c++ Lab Programs

r=0,c=0;

}

void read();

friend int operator ==(matrix,matrix);

friend matrix operator +(matrix,matrix);

friend matrix operator -(matrix,matrix);

friend void operator <<(ostream &, matrix);

};

void matrix::read()

{

cout<<"\n Enter the order of the matrix : \n";

cin>>r>>c;

cout<<"Enter the elements : \n";

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

{

for(int j=0;j<c;j++)

{

cin>>m[i][j];

}

Page 22: c++ Lab Programs

}

}

int operator==(matrix a, matrix b)

{

if((a.r==b.r)&&(a.c==b.c))

return 1;

else

return 0;

}

matrix operator+(matrix a, matrix b)

{

matrix t;

for(int i=0;i<a.r;i++)

{

for(int j=0;j<a.c;j++)

{

t.m[i][j]=a.m[i][j]+b.m[i][j];

}

}

t.r=a.r;

t.c=a.c;

return t;

}

Page 23: c++ Lab Programs

matrix operator-(matrix a, matrix b)

{

matrix t;

for(int i=0;i<a.r;i++)

{

for(int j=0;j<a.c;j++)

{

t.m[i][j]=a.m[i][j]-b.m[i][j];

}

}

t.r=a.r;

t.c=a.c;

return t;

}

void operator <<(ostream &, matrix t)

{

for(int i=0;i<t.r;i++)

{

for(int j=0;j<t.c;j++)

{

cout<<t.m[i][j]<<'\t';

}

cout<<endl;

Page 24: c++ Lab Programs

}

}

void main()

{

matrix a,b,c,d;

clrscr();

cout<<"Taking input for First matrix : ";

a.read();

cout<<"Taking input for Second matrix : ";

b.read();

if(a==b)

{

cout<<"\n Matrix 1 is :\n";

cout<<a;

cout<<"\n Matrix 2 is :\n";

cout<<b;

c=a+b;

d=a-b;

cout<<"\n The sum of two matrix is : \n";

cout<<endl<<c;

cout<<"\n The difference of two matrix is : \n";

cout<<endl<<d;

}

Page 25: c++ Lab Programs

else

{

cout<<"\n Matrix operations are not possible ";

}

getch();

}

11.

// WRITE A C++ PROGRAM TO CREATE A CLASS CALLED QUEUE WITH MEMBER FUNCTIONS //

// TO ADD AN ELEMENT & TO DELETE AN ELEMENT FROM THE QUEUE.USING THE MEMBER //

// FUNCTIONS,IMPLEMENT A QUEUE OF INTEGER & DOUBLE.DEMONSTRATE THE //

// OPERATIONS BY DISPLAYING THE CONTENTS OF THE QUEUE AFTER EVERY OPERATION //

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#include<graphics.h>

#include<iomanip.h>

template <class T>

class queue

{

public :

queue(int max=4);

~queue()

Page 26: c++ Lab Programs

{

delete[] pqptr;

}

void insert(const T&);

int qfull()const

{

return rear==nsize;

}

int qempty()

{

return(front>=rear||rear==0);

}

T del();

void display();

private :

int nsize;

int rear;

int front;

T *pqptr;

Page 27: c++ Lab Programs

};

template <class T>

queue<T>::queue(int nx)

{

nsize = nx>0 && nx<100 ? nx:10;

front=0;

rear=0;

pqptr= new T[nsize];

}

template<class T>

void queue<T>::insert(const T &nitem)

{

if(qfull())

{

cout<<"\n Queue Overflow..."<<endl;

getch();

return;

}

else

{

pqptr[++rear]=nitem;

return;

}

Page 28: c++ Lab Programs

}

template <class T>

T queue<T>::del(void)

{

T p;

if(qempty())

{

cout<<"\n Queue Underflow..."<<endl;

getch();

return 0;

}

else

{

p=pqptr[front++];

cout<<"front = "<<front<<" and rear = "<<rear;

return p;

}

}

template <class T>

void queue<T>::display()

{

if(qempty())

{

Page 29: c++ Lab Programs

cout<<"\n Queue is Empty...";

getch();

}

else

{

cout<<"front = "<<front<<" and rear = "<<rear;

int i=front;

cout<<"\n Front ";

for(i=i+1;i<=rear;i++)

{

cout<<"<-"<<pqptr[i];

}

cout<<"<-Rear";

return;

}

}

int menu()

{

int m;

cout<<endl<<setw(10)<<"1. Insert.";

cout<<endl<<setw(10)<<"2. Delete.";

cout<<endl<<setw(10)<<"3. Display.";

cout<<endl<<setw(10)<<"4. Exit.";

cout<<endl<<setw(10)<<"Enter your choice : ";

Page 30: c++ Lab Programs

cin>>m;

return m;

}

int main()

{

int i;

int choice1=0,choice2=0;

double d;

queue<int>iq(4);

queue<double>dq(4);

clrscr();

while(choice1!=3)

{

cout<<endl<<setw(10)<<"1.Integer Queue ";

cout<<endl<<setw(10)<<"2.Double Queue ";

cout<<endl<<setw(7)<<"3.Exit ";

cout<<endl<<setw(10)<<"Enter your choice : ";

cin>>choice1;

switch(choice1)

{

case 1: while(choice2!=4)

{

choice2=menu();

Page 31: c++ Lab Programs

switch(choice2)

{

case 1: cout<<endl<<"Enter the element to be inserted : ";

cin>>i;

iq.insert(i);

break;

case 2: iq.del();

break;

case 3: iq.display();

break;

case 4:

exit(main());

break;

default : cout<<"\n Invalid input";

}

}

break;

case 2: choice2=0;

while(choice2!=4)

{

choice2=menu();

Page 32: c++ Lab Programs

switch(choice2)

{

case 1: cout<<endl<<"Enter the element to be inserted :";

cin>>d;

dq.insert(d);

break;

case 2: dq.del();

break;

case 3: dq.display();

break;

case 4:

exit(main());

default: cout<<"\n Invalid Input";

}

}

break;

case 3: exit(0);

default: cout<<"Invalid Input";

Page 33: c++ Lab Programs

break;

}

}

return(0);

}

14.

PROGRAM TO CONCATENATE TWO STRINGS USING OPERATOR OVERLOADING.

#include<iostream.h>

#include<conio.h>

#include<string.h>

class string

{

char s[30];

public:

string()

{

strcpy(s," ");

}

string(string &st)

{

strcpy(s,st.s);

}

Page 34: c++ Lab Programs

void input()

{

cout<<"\n 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)

{

cout<<sr.s;

}

void main()

{

Page 35: c++ Lab Programs

string s1,s2;

clrscr();

s1.input();

s2.input();

string s3 = s1 + s2;

cout<<"\n string 1 is : "<<s1;

cout<<"\n string 2 is : "<<s2;

cout<<"\n the concatenated string is : "<<s3;

getch();

}