0% found this document useful (0 votes)
54 views47 pages

Altered I-Semester Practical Final Printout 2019

The problem involves modeling a financial institution that has two types of accounts: simple and compound. A base Account class is defined with protected data members for account number and principal. Derived SimpleAccount and CompoundAccount classes calculate interest differently: - SimpleAccount calculates interest as a percentage of the principal - CompoundAccount calculates interest using the compound interest formula The classes encapsulate the account details and provide member functions to calculate interest accordingly.

Uploaded by

Aaron
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views47 pages

Altered I-Semester Practical Final Printout 2019

The problem involves modeling a financial institution that has two types of accounts: simple and compound. A base Account class is defined with protected data members for account number and principal. Derived SimpleAccount and CompoundAccount classes calculate interest differently: - SimpleAccount calculates interest as a percentage of the principal - CompoundAccount calculates interest using the compound interest formula The classes encapsulate the account details and provide member functions to calculate interest accordingly.

Uploaded by

Aaron
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

PROGRAM NO:1 DATE:

PROBLEM STATEMENT: CLASSES AND OBJECTS

To write a menu driven program to define a class CARRENTAL with the following
specifications and to display the details of the car for the given car type.
Private members:
carid, aboutcar,cartype rent.
A member function assignment() to assign the following values for rent as per the given
cartype.
Car type Rent
Small 1000
Van 800
Big 2500
Public members:
-getcar() to accept the values of all dtat members and call the function assignrent() to
assign rent.
-showcar() to view the details of the car.
-retcartype() to return cartype.

SOURCE CODE:
//classes and objects
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class carrental
{
int carid;
char aboutcar[20];
char cartype[20];
float rent;
void assignrent()
{
if(!strcmp(cartype,"small"))
rent=1000;
else if(!strcmp(cartype,"van"))
rent=800;
else if(!strcmp(cartype,"big"))
rent=2500;
}
public:
void getcar()
{
cout<<"\n ENTER THE CARID::";cin>>carid;
cout<<"\n ABOUT CAR::";gets(aboutcar);

1
cout<<"\n CAR TYPE::";gets(cartype);
assignrent();
}
void showcar()
{

cout<<"\n CARTYPE::";cout<<cartype;
cout<<"\n CARID::";cout<<carid;
cout<<"\n ABOUT CAR::";cout<<aboutcar;
cout<<"\n RENT::";cout<<rent;
}
char *retcartype()
{
return(cartype);
}
};
void main()
{
clrscr();
carrental c[3];int found=0;
char searchcar[20];
for(int i=0;i<3;i++)
{
c[i].getcar();
}
for(i=0;i<3;i++)
{
cout<<"\n CAR INFORMATION::"<<i+1;
c[i].showcar();
}
cout<<"\n ENTER THE CAR TYPE YOU WANT TO BOOK";gets(searchcar);
for(i=0;i<3;i++)
{
if(strcmpi(c[i].retcartype(),searchcar)==0)
{
c[i].showcar();found=1;
} }
if(found==0)
cout<<searchcar<<"is not available";
getch();
}

2
OUTPUT VALIDATION:-
CAR INFORMATION::1
CARTYPE::small
CARID::1
ABOUT CAR::wagonar,3 seater capacity small
RENT::1000
CAR INFORMATION::2
CARTYPE::van
CARID::2
ABOUT CAR::innova,5 seatercapacity van
RENT::800
CAR INFORMATION::3
CARTYPE::big
CARID::3
ABOUT CAR::mini bus,10 seater capacity big
RENT::2500
ENTER THE CAR TYPE YOU WANT TO BOOKbig
CARTYPE::big
CARID::3
ABOUT CAR::mini bus,10 seater capacity big
RENT::2500

3
PROGRAM NO:2 DATE:

PROBLEM STATEMENT: NESTED CLASSES

To write a C++ program to implement the concept of class employee having the following
members:
Data members: eno, ename, date of joining which has dd, mm, yy as a nested structure
basic,hra,da,pf,netsalary
Member functions: input()- to accept the values of employee number, name, date of joining
and basic.
Print()- to print the details of the employee with salary.
Compute()- to calculate the hra as 10% of basic, da as 15% of basic, pf as 12% of basic
and net salary = basic+hra+da-pf.
The program should print the details of those employees whose date of joining is after 2005
and who earns more than 30000.
SOURCE CODE:

//Nested classes

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class doj
{
public:
int date; char month[20]; long year;
};
class emp
{
int eno; char name[20];
float basic,hra,da,pf,netsalary;
public:
doj d;
float retnetsalary()
{
return netsalary;
}
void input()
{
cout<<"\n enter the employee no::";
cin>>eno;
cout<<"\n enter the employee name::";
gets(name);
cout<<"\n enter the date of joining(dd/mm/yy";
cin>>d.date;
gets(d.month);
4
cin>>d.year;
cout<<"\n enter the basic amount::";
cin>>basic;
}
void print()
{
cout<<"\n EMPLOYEE NO::";cout<<eno;
cout<<"\n EMPLOYEE NAME::";cout<<name;
cout<<"\n DATE OF JOINING::";cout<<d.date<<":"<<d.month<<":"<<d.year;
cout<<"\n BASIC AMOUNT::";cout<<basic;
cout<<"\n NET SALARY::";cout<<netsalary;
}
void compute()
{
hra=0.1*basic;
da=0.15*basic;
pf=0.12*basic;
netsalary= basic+hra+da+pf;
}
};
void main()
{
clrscr();
emp e[2];
for(int i=0;i<2;i++)
{
e[i].input();
e[i].compute();
}
for(i=0;i<2;i++)
{
if(e[i].d.year>2005 && e[i].retnetsalary()>=30000)
e[i].print();
}
getch();
}

5
OUTPUT VALIDATION

enter the employee no::1


enter the employee name::akash
enter the date of joining(dd/mm/yy)12 june 2001
enter the basic amount::20000
enter the employee no::2
enter the employee name::rahul
enter the date of joining(dd/mm/yy)13 july 2008
enter the basic amount::50000

EMPLOYEE NO::2
EMPLOYEE NAME::rahul
DATE OF JOINING::13:july:2008
BASIC AMOUNT::50000
NET SALARY::68500

6
PROGRAM NO: 3 DATE:

PROBLEM STATEMENT: MATRIX ARITHMETIC USING CLASSES


Write a menu driven program to perform the following matrix operations:
a) Create matrix of size m,n
b) Display sum of principal diagonal elements,right diagonal,left diagonal elements
c) Sum of the matrix
d) Difference of matrix
e) Product of two matrix
Define a class to perform the above operations.
SOURCE CODE:
//Matrix Arithmetic Using Classes
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
class matrix
{
public:
int a[10][10];int r,c;
matrix();
void readdata();
void display();
void sum(matrix m,matrix n);
void prod(matrix m,matrix n);
void diagonal(matrix m);
}m1,m2,m3;
matrix::matrix()
{
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
a[i][j]=0;
}
void matrix::readdata()
{
cout<<"\n enter the rows and coloums";
cin>>r>>c;
cout<<"\n enter the elements::";
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cin>>a[i][j];
}

7
void matrix::display()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
cout<<"\t"<<a[i][j];
cout<<"\n";
}
}
void matrix::sum(matrix m, matrix n)
{
if(m.r!=n.r || m.c!=n.c)
{
cout<<"\n matrix are not equal.....";getch();exit(0);}
for(int i=0;i<m.r;i++)
{
for(int j=0;j<m.c;j++)
{ a[i][j]=m.a[i][j]+n.a[i][j];
}
}
cout<<"\n sum of two matrix is\n";
for(i=0;i<m.r;i++)
{
for(int j=0;j<m.c;j++)
cout<<"\t"<<a[i][j];
cout<<"\n";
}
}
void matrix::diagonal(matrix m)
{
int sum=0,psum=0,rsum=0;
if(m.r!=m.c)
{
cout<<"\n matrix is not square";getch();exit(0);}
r=m.r-1;
for(int i=0;i<=m.r;i++)
{
psum+=m.a[i][i];
rsum+=a[i][r-i];
}
cout<<"\n PRINCIPAL DIAGONAL:"<<psum;
cout<<"\n RIGHT DIAGONAL:"<<rsum;

8
if(r%2==0)
sum=psum+rsum-(a[r/2][r/2]);
cout<<"\n ADDITION OF BOTH THE DIAGONALS"<<sum;
}
void matrix::prod(matrix m,matrix n)
{
if(m.c!=n.r)
{ cout<<"\n matrix are not compatible for product";
getch();
exit(0);
}
for(int i=0;i<m.r;i++)
for(int j=0;j<n.c;j++)
a[i][j]=0;
for(i=0;i<m.r;i++)
{
for(j=0;j<n.c;j++)
{
for(int k=0;k<n.r;k++)
a[i][j]+=m.a[i][k]*n.a[k][j];
}
}
cout<<"\n multiplication of two matrix is\n";
for(i=0;i<m.r;i++)
{
for(int j=0;j<m.c;j++)
cout<<"\t"<<a[i][j];
cout<<"\n";
}
}
void menu()
{
clrscr();
int opt=0;
while(1)
{
cout<<"\n 1.READ DATA\n 2.DISPLAY DATA \n 3.ADDITION\n";
cout<<"4.PRODUCT "<<"\n 5.DIAGONAL \n 6.QUIT\n ";
cout<<"\n enter choice";
cin>>opt;
switch(opt)
{

9
case 1:m1.readdata();
m2.readdata();
break;
case 2:cout<<"\n matrix A\n";
m1.display();
cout<<"\n matrix B\n";
m2.display();
break;
case 3:cout<<"\n sum of two matrix\n";
m3.sum(m1,m2);
cout<<"\n matrix A\n ";m1.display();
cout<<"\n matrix B\n";m2.display();
break;
case 4:cout<<"\n product of two matrix\n";
m3.prod(m1,m2);
cout<<"\n matrix A\n ";m1.display();
cout<<"\n matrix B\n ";m2.display();
break;
case 5:m1.display();
m1.diagonal(m1);
break;
case 6:exit(0);
}
}
}
void main()
{
menu();
}

10
OUTPUT VALIDATION:-
1.READ DATA
2.DISPLAY DATA
3.ADDITION
4..PRODUCT
5.DIAGONAL
6.QUIT
enter choice1
enter the rows and coloums3 3
enter the elements1 2 3 4 5 6 7 8 9
enter the rows and coloums33
enter the elements9 8 7 6 5 4 3 2 1
1.READ DATA
2.DISPLAY DATA
3.ADDITION
4..PRODUCT
5.DIAGONAL
6.QUIT
enter choice2
matrix A
1 2 3
4 5 6
7 8 9
matrix B
9 8 7
6 5 4
3 2 1
1.READ DATA
2.DISPLAY DATA
3.ADDITION
4..PRODUCT
5.DIAGONAL
6.QUIT
enter choice3
sum of two matrix is
10 10 10
10 10 10
10 10 10

matrix A
1 2 3
4 5 6
7 8 9
11
matrix B
9 8 7
6 5 4
3 2 1
1.READ DATA
2.DISPLAY DATA
3.ADDITION
4..PRODUCT
5.DIAGONAL
6.QUIT
enter choice4
multiplication of two matrix is
30 24 18
84 69 54
138 114 90
matrix A
1 2 3
4 5 6
7 8 9
matrix B
9 8 7
6 5 4
1.READ DATA
2.DISPLAY DATA
3.ADDITION
4..PRODUCT
5.DIAGONAL
6.QUIT
enter choice5
1 2 3
4 5 6
7 8 9
PRINCIPAL DIAGONAL:15
RIGHT DIAGONAL:15
ADDITION OF BOTH THE DIAGONALS 25
1.READ DATA
2.DISPLAY DATA
3.ADDITION
4..PRODUCT
5.DIAGONAL
6.QUIT
enter choice6

12
PROGRAM NO: 4 DATE:

PROBLEM STATEMENT:FINANCIAL INSTITUTION

A Financial Institution has only 2 kinds of accounts simple and compound. The class
account has 2 protected attributes accno and principle. The interest for simple account is
calculated as I= (PNR)/100.The interest for compound account is calculated by
A=P (1+R/100) n
Model the above situation by defining classes account, simple and compound where simple
and compound are derived from account>Write appropriate constructors for passing
various attributes for the 3 classes. Write the function double interest that calculate the
appropriate interest for simple and compound and also create a function display for all the
3 classes which prints out all the attributes values.
SOURCE CODE:
//FINANCIAL INSTITUTION

#include<iostream.h>
#include<conio.h>
#include<math.h>
class account
{
protected:
int accno;
double principle;
public:
account(int a,double b)
{
accno=a;
principle=b;
}
};
class simple: public account
{
double n,r,si;
public:
simple(int acc, double p, double year, double rate):account(acc,p)
{
n=year;r=rate;
}
double interest()
{
si=(principle*n*r)/100;
return(si);
}
13
void display()
{
cout<<"\n\n \t FINANCIAL INSTITUTION";
cout<<"\n \n simple interest\n\n";
cout<<"\n account:"<<accno;
cout<<"\n principle:Rs"<<principle;
cout<<"\n rate:"<<r;
cout<<" per annum";
cout<<"\n time:"<<n<<"years";
}
};
class compound: public account
{
double n,r,ci;
public:
compound(int acc, double p, double year, double rate):account(acc,p)
{
n=year;r=rate;
}
double interest()
{
ci=(principle*pow((1+r/100),n));
return(ci);
}
void display()
{
cout<<"\n\n \t FINANCIAL INSTITUTION";
cout<<"\n \n compound interest\n\n";
cout<<"\n account:"<<accno;
cout<<"\n principle:Rs"<<principle;
cout<<"\n rate:"<<r<<" per annum";
cout<<"\n time:"<<n<<"years";
}
};

void main()
{
clrscr();
double principle,r,n,i;
int accno,ch;
cout<<"\n \n \t\t financial institution";
cout<<"\n enter the account np";
cin>>accno;
cout<<"\n enter the principle";
cin>>principle;
cout<<"\n enter the time:";
14
cin>>n;
cout<<"\n enter the rate";
cin>>r;
cout<<"\n 1.simple interest";
cout<<"\n 2.coumpound interest";
cout<<"\n enter your choice";
cin>>ch;
if(ch==1)
{
simple o1(accno,principle,n,r);
i=o1.interest();
o1.display();
cout<<"\n Interest"<<i;
getch();
}
if(ch==2)
{
compound o2(accno,principle,n,r);
i=o2.interest();
o2.display();
cout<<"\n Interest"<<i;
getch();
}
}

15
OUTPUT VALIDATION:-
enter the account np1
enter the principle1000
enter the time:2
enter the rate2
1. simple interest
2. compound interest
enter your choice1
FINANCIAL INSTITUTION
simple interest
account:1
principle:Rs1000
rate:2 per annum
time:2years
Interest40
FINANCIAL INSTITUTION
compound interest
account:1
principle:Rs1000
rate:2 per annum
time:2years
Interest1040.4

16
PROGRAM NO: 5 DATE:

PROBLEM STATEMENT: HOSPITAL DETAILS (INHERITANCE WITH FILE HANDLING)


A Hospital wants to create a database regarding its Indore patients.
The information includes [name, dateof admission, date of discharge, disease].
Create a structure to store data as its members and a base class to store the
above information. Member function should include function to enter data and
display the list of patients in the database. Create a derived class to store age of patients.
Develop a program to list the information of all pediatric patients.
SOURCE CODE:

//HOSPITAL DETAILS (INHERITANCE WITH FILE HANDLING)

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
struct date
{
int year, day, month;
};
class hospital
{
char name[20],disease[20];
date doa, dod;
public:
void enter()
{
clrscr();
cout<<"\n \n \n HOSPITAL DETAILS";
cout<<"\n NAME::";gets(name);
cout<<"\n DISEASE::";gets(disease);
cout<<"\n \n DATE OF ADMISSION";
cout<<"\n DATE::";cin>>doa.day;
cout<<"\n MONTH::";cin>>doa.month;
cout<<"\n YEAR::";cin>>doa.year;
cout<<"\n \n DATE OF DISCHARGE";
cout<<"\n DATE::";cin>>dod.day;
cout<<"\n MONTH::";cin>>dod.month;
cout<<"\n YEAR::";cin>>dod.year;
}
void display()
{
clrscr();
cout<<"\n\n \n HOSPITAL DETAILS\n";
17
cout<<"\n NAME::";cout<<name;
cout<<"\n DISEASE::";cout<<disease;
cout<<"\n DATE OF ADMISSION::";
cout<<doa.day;cout<<"\\";
cout<<doa.month;cout<<"\\";
cout<<doa.year;cout<<"\\";
cout<<"\n DATE OF DISCHARGE::";
cout<<dod.day;cout<<"\\";
cout<<dod.month;cout<<"\\";
cout<<dod.year;cout<<"\\";
}
};
class hospitalage: public hospital
{
public:
float age;
void enterage()
{
enter();
cout<<"\n AGE:";cin>>age;
}
void displayage()
{
display();
cout<<"\n AGE:";cout<<age;
getch();
}
}o1;
void enterdata()
{
ofstream fout("h.dat",ios::binary|ios::app);
o1.enterage();
fout.write((char *)&o1,sizeof(o1));
fout.close();
}
void displaydata()
{
char f='n';
ifstream fin("h.dat",ios::binary|ios::in);
while(fin.read((char *)&o1,sizeof(o1)))
{
if(fin.eof())

18
break;
if(o1.age<=12)
{
f='f';
o1.display();
}
else
f='n';
getch();
}
if(f=='n')
{
clrscr();
cout<<"\n\n NO MORE PEDIATRIC PATIENTS!!!";
getch();
}
}
void menu()
{
clrscr();
int chk;
up:
cout<<"\n \n \n \t XYZ HOSPITAL\n\n\n";
cout<<"\t \t\t1.NEW PATIENT \n";
cout<<"\t \t\t2.DISPLAY PEDIATRIC DETAIL \n";
cout<<"\t \t\t3.EXIT \n";
cout<<"\n ENTER YOUR CHOICE::";
cin>>chk;
switch(chk)
{
case 1:enterdata();
break;
case 2:displaydata();
break;
case 3:exit(0);
}
goto up;
}
void main()
{
menu();
}

19
OUTPUT VALIDATION:-

XYZ HOSPITAL
1. NEW PATIENT
2. DISPLAY PEDIATRIC DETAIL
3. EXIT

ENTER YOUR CHOICE::1


HOSPITAL DETAILS
NAME::nikhil
DISEASE::typhoid
DATE OD ADMISSION
DATE::3
MONTH::10
YEAR::2016
DATE OF DISCHARGE
DATE::9
MONTH::10
YEAR::2016
AGE:10
XYZ HOSPITAL
1. NEW PATIENT
2. DISPLAY PEDIATRIC DETAIL
3. EXIT
ENTER YOUR CHOICE::2
HOSPITAL DETAILS
NAME::nikhil
DISEASE::typhoid
DATE OF ADMISSION::3\10\2016\
DATE OF DISCHARGE::9\10\2016\
XYZ HOSPITAL
1. NEW PATIENT
2. DISPLAY PEDIATRIC DETAIL
3. EXIT
ENTER YOUR CHOICE::3

20
PROGRAM NO: 6 DATE:

PROBLEM STATEMENT: BINARY FILE APPEND

The binary file “prd2.dat” has the class:-


class product
{
int prdcode; char prdname[20]; float prdprice;
public:
product();
product(int,char *,float);
product(product &p);
~product();
Readdata();
Displaydata();
int rtncode();
char *rtnname();
float rtnprice();
};
Write a menu driven program to:-
a) Create a binary file “prd2.dat” that stores the details of products.
b) To append details to an existing file.
c) To display the details of the file after each operation.
SOURCE CODE:

//BINARY FILE APPEND


#include<fstream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
#include<string.h>
class product
{
int pcode;
char pname[20];
float price;
public:
product();
product(int,char *,float);
product(product &p);
~product();
void readdata();
void display();
int retcode();
21
char *retname();
float retprice();
}p;
product::product()
{
pcode=0;price=0;strcpy(pname," ");
}
void product::readdata()
{
cout<<"\n enter product code,name,price";
cin>>pcode;gets(pname);cin>>price;
}
void product::display()
{
cout<<"\n PRODUCT CODE:";cout<<pcode;
cout<<"\n PRODUCT NAME:";cout<<pname;
cout<<"\n PRICE:";cout<<price;
}
int product::retcode()
{ return(pcode);
}
char* product::retname()
{ return(pname);
}
float product::retprice()
{ return(price);
}
product::~product()
{ cout<<"\n destructor";
}
void accept()
{ product p;
char opt='y';
ofstream file;
file.open("prd2.bin",ios::binary|ios::out);
for(int i=1;opt=='y';i++)
{ p.readdata();
file.write((char *)&p,sizeof(p));
cout<<"\n continue(y/n):";
cin>>opt;
}
file.close(); }

22
void display()
{ product p;
ifstream file;
file.open("prd2.bin",ios::binary|ios::in);
while(file.read((char*)&p,sizeof(p)))
{ p.display();
}
file.close();
}
void appenddata()
{
char opt='y';
product p;
ofstream file;
file.open("prd2.bin",ios::app|ios::binary);
while(opt=='y')
{
p.readdata();
file.write((char *)&p,sizeof(p));
cout<<"\n Do you want to continue...";
cin>>opt;
} }
void menu()
{
int opt=9;
while(opt)
{
cout<<"\n CHOOSE"<<"\n 1.CREATE DATA\n 2.DISPLAY DATA\n 3. APPEND DATA";
cout<<"\n 4.EXIT ";
cin>>opt;
switch(opt)
{
case 1:accept();break;
case 2:display();break;
case 3:appenddata();break;
case 4:exit(0);
}}}
void main()
{
clrscr();
menu();
getch(); }

23
OUTPUT VALIDATION:
CHOOSE
1.CREATE DATA
2.DISPLAY DATA
3. APPEND DATA
4.EXIT 1
enter product code,name,price101
pendrive
550
continue(y/n):y
enter product code,name,price102
scanner
12000
continue(y/n):n
CHOOSE
1.CREATE DATA
2.DISPLAY DATA
3. APPEND DATA
4.EXIT 2
PRODUCT CODE:101
PRODUCT NAME:pendrive
PRICE:550
PRODUCT CODE:102
PRODUCT NAME:scanner
PRICE:12000
CHOOSE
1.CREATE DATA
2.DISPLAY DATA
3. APPEND DATA
4.EXIT 3
enter product code,name,price103
printer
5000
Do you want to continue...n
CHOOSE
1.CREATE DATA
2.DISPLAY DATA
3. APPEND DATA
4.EXIT 4
Destructor

24
PROGRAM NO: 7 DATE:

PROBLEM STATEMENT:BINARY FILE SEARCHING


Write a menu driven program to perform the following:
To display the details of a given record based on given code/name/price.
SOURCE CODE:
//BINARY FILE SEARCHING
#include<fstream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
#include<string.h>
class product
{
int pcode;
char pname[20];
float price;
public:
product();
~product();
void readdata();
void display();
int retcode();
char *retname();
float retprice();
}p;
product::product()
{ pcode=0;price=0;strcpy(pname," "); }

product::~product()
{
cout<<"\n destructor";
}
void product::readdata()
{
cout<<"\n enter product code,name,price";
cin>>pcode;gets(pname);cin>>price;
}
void product::display()
{
cout<<"\n PRODUCT CODE:";cout<<pcode;
cout<<"\n PRODUCT NAME:";cout<<pname;
cout<<"\n PRICE::";cout<<price;
}
int product::retcode()
{
return(pcode);
}

25
char* product::retname()
{ return(pname); }
float product::retprice()
{
return(price);
}
void filedisplay(int code)//by number
{
int f=0;
fstream file("prd1.bin",ios::binary|ios::in);
file.seekg(0);
while(file.read((char *)&p,sizeof(p)))
{
if(code==p.retcode())
{
p.display();f=1;break;
} }
if(f==0)
cout<<"\n record not found";
file.close();
}
void filedisplay(char * name)//by name
{
int f=0;
fstream file("prd1.bin",ios::binary|ios::in);
while(file)
{
file.read((char *)&p,sizeof(p));
if(strcmp(name,p.retname())==0)
{
p.display();f=1;break;

}}
if(f==0)
cout<<"\n record not found";
file.close();
}
void filedisplay(float price)//by price
{ int f=0;
fstream file("prd1.bin",ios::binary|ios::in);
while(file)
{
file.read((char *)&p,sizeof(p));
if(price==p.retprice())
{
p.display();f=1;break;
}}
if(f==0)
cout<<"\n record not found";
26
file.close();
}
void filecreate()
{ int no;
fstream file("prd1.bin",ios::binary|ios::app);
if(!file)
{ cout<<"\n error in association";getch();exit(0); }
cout<<"\n enter number of products";cin>>no;
for(int i=0;i<no;i++)
{ p.readdata();
file.write((char *)&p,sizeof(p)); }
file.close(); }
void menu()
{ char c='y';
int opt=0;
while(c=='y' || c=='Y')
{ cout<<"\n 1.CREATE FILE..."
<<"\n 2.DISPLAY PRODUCT DETAILS USING PRODUCT CODE"
<<"\n 3.DISPLAY PRODUCT DETAILS USING PRODUCT NAME"
<<"\n 4.DISPLAY PRODUCT DETAILS USING PRODUCT PRICE"
<<"\n 5.EXIT";
cout<<"\n enter your choice";
cin>>opt;
switch(opt)
{
case 1:filecreate();break;
case 2:int code;
cout<<"\n enter the product code";cin>>code;
filedisplay(code);break;
case 3: char name[20];
cout<<"\n enter product name";
cin>>name;
filedisplay(name);break;
case 4:float price;
cout<<"\n enter product price";
cin>>price;
filedisplay(price);
break;
case 5:cout<<"\n quitting the program";
exit(0);
}
cout<<"\n Do you want to continue"; cin>>c;
} }
void main()
{
clrscr();
menu();
exit(0);
}
27
OUTPUT VALIDATION:
1. CREATE FILE...
2. DISPLAY PRODUCT DETAILS USING PRODUCT CODE
3. DISPLAY PRODUCT DETAILS USING PRODUCT NAME
4. DISPLAY PRODUCT DETAILS USING PRODUCT PRICE
5. EXIT
enter your choice1
enter number of products1
enter product code,name,price
101
pen
12
Do you want to continue y
1. CREATE FILE...
2. DISPLAY PRODUCT DETAILS USING PRODUCT CODE
3. DISPLAY PRODUCT DETAILS USING PRODUCT NAME
4. DISPLAY PRODUCT DETAILS USING PRODUCT PRICE
5. EXIT
enter your choice2
enter the product code101
PRODUCT CODE:101
PRODUCT NAME: pen
PRICE::12
Do you want to continue y
1. CREATE FILE...
2. DISPLAY PRODUCT DETAILS USING PRODUCT CODE
3. DISPLAY PRODUCT DETAILS USING PRODUCT NAME
4. DISPLAY PRODUCT DETAILS USING PRODUCT PRICE
5. EXIT
enter your choice3
enter product name pen
PRODUCT CODE: 101
PRODUCT NAME: pen
PRICE::12
Do you want to continuey?
1. CREATE FILE...
2. DISPLAY PRODUCT DETAILS USING PRODUCT CODE
3. DISPLAY PRODUCT DETAILS USING PRODUCT NAME
4.DISPLAY PRODUCT DETAILS USING PRODUCT PRICE
5.EXIT

28
enter your choice4
enter product price12
PRODUCT CODE:101
PRODUCT NAME:pen
PRICE::12
Do you want to continuey
1.CREATE FILE...
2.DISPLAY PRODUCT DETAILS USING PRODUCT CODE
3.DISPLAY PRODUCT DETAILS USING PRODUCT NAME
4.DISPLAY PRODUCT DETAILS USING PRODUCT PRICE
5.EXIT
enter your choice5
Do you want to continuen
destructor

29
PROGRAM NO: 8 DATE:

PROBLEM STATEMENT:BINARY FILE MODIFICATION


Write a program to modify the details of the records as requested by the user:
a) By a given code.
b) By displaying every record and update if requested.
c) Display the details of the file.
SOURCE CODE:
//BINARY FILE MODIFICATION
#include<fstream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<process.h>
fstream file;
class product
{
int pcode;
char pname[20];
float price;
public:
void accept()
{ cout<<"\n Enter the product code,product name,product price";
cin>>pcode;gets(pname);cin>>price;
}
void display()
{
cout<<"\n product code:"<<pcode;
cout<<"\n product name:"<<pname;
cout<<"\n price:"<<price;
}
int retcode()
{
return pcode;
}
}p;
void extract()
{
file.open("prd1.bin",ios::in|ios::binary);
if(!file)
{ cout<<"\n Error in file association";
getch();exit(0);
}
while(file.read((char *)&p,sizeof(p)))
{ p.display();}
file.close(); }

30
void modifycode()
{
long pos=0;
file.open("prd1.bin",ios::in|ios::out|ios::binary);
if(!file)
{
cout<<"\n Error in file association";
getch();exit(0);
}
char ch; int code;
cout<<"\n Enter product" ;
int f=0;
cin>>code;
while(file)
{file.read((char *)&p,sizeof(p));
if(p.retcode()==code)
{
f=1;
p.display();
cout<<"\n Enter the details to be modified ";
p.accept();
file.seekg(pos,ios::beg);
file.write((char *)&p,sizeof(p));
}
pos=file.tellg();
}
file.close();
if(f==0)
cout<<"\n code not found";
else
{ cout<<"\n modified details are";extract();}
}

void filemodify()
{
long pos=0;
file.open("prd1.bin",ios::in|ios::out|ios::binary);
if(!file)
{ cout<<"\n Error in file association";
getch();exit(0);
}
char ch='y';
while(file.read((char *)&p,sizeof(p)))
{
p.display();
cout<<"\n correct <y/n>";cin>>ch;

31
if(ch=='y' || ch=='Y')
{
file.seekg(pos);
p.accept();
file.write((char *)&p,sizeof(p));
}
pos=file.tellg();
}
file.close();
}
void menu()
{
int opt; char ch='y';
while(ch=='y')
{
cout<<"\n 1.MODIFY BY CODE";
cout<<"\n 2.SELECT AND MODIFY";
cout<<"\n 3.DISPLAY";
cout<<"\n 4.EXIT";
cout<<"\n Enter your choice ";cin>>opt;
switch(opt)
{
case 1:modifycode();break;
case 2:filemodify();break;
case 3:extract();break;
case 4:exit(0);
}
cout<<"\n Do you want to continue ";cin>>ch;}
}
void main()
{
clrscr();
menu();
getch();
}

32
OUTPUT VALIDATION:
1.MODIFY BY CODE
2.SELECT AND MODIFY
3.DISPLAY
4.EXIT
Enter your choice 3
product code:101
product name:pen
price:12
Do you want to continue y
1.MODIFY BY CODE
2.SELECT AND MODIFY
3.DISPLAY
4.EXIT
Enter your choice 1
Enter the details to be modified
Enter the product code,productname,product price101
gelpen
20
modified details are
product code:101
product name:gelpen
price:20
Do you want to continue y
1.MODIFY BY CODE
2.SELECT AND MODIFY
3.DISPLAY
4.EXIT
Enter your choice 2
product code:101
product name:gelpen
price:20
correct <y/n>y
Enter the product code,productname,product price101
parkerpen
120
Do you want to continue y
1.MODIFY BY CODE
2.SELECT AND MODIFY
3.DISPLAY
4.EXIT
Enter your choice 3
product code:101
product name:parkerpen
price:120
Do you want to continue y
1.MODIFY BY CODE
2.SELECT AND MODIFY
3.DISPLAY
4.EXIT
Enter your choice 4

33
PROGRAM NO: 9 DATE:

PROBLEM STATEMENT:BINARY FILE DELETION


Write a menu driven program to delete the records of the product file as requested
a) To display every record present in the file, delete the record if user requests to
delete it.
b) To display the file details before and after deletion
SOURCE CODE:
#include<fstream.h>
#include<ctype.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<process.h>
fstream file;
class product
{
int pcode;
char pname[20];
float price;
public:
void accept()
{
cout<<"\n Enter the product code,productname,product price";
cin>>pcode;gets(pname);cin>>price;
}
void display()
{
cout<<"\n product code:"<<pcode;
cout<<"\n product name:"<<pname;
cout<<"\n price:"<<price;
}
int retcode()
{
return pcode;
}
}p;
void file_extract()
{
file.open("prd2.bin",ios::in|ios::binary);
if(!file)
{ cout<<"\n Error in file association";
getch();exit(0);

34
while(file.read((char *)&p,sizeof(p)))
{
p.display();
}
file.close();
}
void file_delete()
{
fstreamfile,new_file;
file.open("prd2.bin",ios::in|ios::out|ios::binary);
if(!file)
{
cout<<"file does not exists";
getch();
exit(0);
}
new_file.open("temp.bin",ios::out|ios::binary);
if(!new_file)
{
cout<<"file does not exists";
getch();
exit(0);
}
char delete_rec='n';
while(file.read((char *)&p,sizeof(p)))
{
p.display();
cout<<"\nDelete record????<y/n>";
cin>>delete_rec;
delete_rec=tolower(delete_rec);
if(delete_rec=='y')

cout<<"\nDeletedsuccessfulllllllllllly";

else

new_file.write((char*)&p,sizeof(p)); }

file.close();
new_file.close();
remove("prd2.bin");
rename("temp.bin","prd2.bin");
getch();

}
35
void menu()
{
int opt;
clrscr();
m: cout<<"\n1.read file..\n2.delete rec by rec....\n3.exit";
cout<<"\nEnterur choice :";
cin>>opt;
switch(opt)
{
case 1:
file_extract();
break;
case 2:
file_delete();
break;
case 3:
exit(0);
}
goto m;
getch();
}
void main()
{
clrscr();
menu();
}

36
OUTPUT VALIDATION :
1.read file..
2.delete rec by rec....
3.exit
Enter ur choice :1
product code:101
product name:pendrive
price:550
product code:102
product name:scanner
price:12000
product code:103
product name:printer
price:5000
1.read file..
2.delete rec by rec....
3.exit
Enter ur choice :2
product code:101
product name:pendrive
price:550
Delete record????<y/n>n
product code:102
product name:scanner
price:12000
Delete record????<y/n>n
product code:103
product name:printer
price:5000
Delete record????<y/n>y
Deleted successfulllllllllllly
1.read file..
2.delete rec by rec....
3.exit
Enter ur choice :1
product code:101
product name:pendrive
price:550
product code:102
product name:scanner
price:12000
1.read file..
2.delete rec by rec....
3.exit
Enter ur choice :3

37
PROGRAM NO: 10 DATE:
PROBLEM STATEMENT: FILE OPERATIONS
Write a complete menu driven program to perform the following operations
a) Create a text file
b) Count the no of alphabets and vowels in the file
c) Count the no of lines in the file
SOURCE CODE:
#include<fstream.h>
#include<ctype.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
char text[100];
void create()
{ inti,j,k;
ofstreamfout("text1.txt",ios::app);
cout<<"\nNumber of lines:";
cin>>i;
for(j=0;j<i;j++)
{
cout<<"\n enter line:"<<j+1<<":";
gets(text);
k=0;
while(text[k]!='\0')
{
fout<<text[k];
k++;
} fout<<'\n';
}
}
void countlines()
{ char alp; int count=0;
ifstream fin("text1.txt",ios::in);
while(1)
{ alp=fin.get();
if(fin.eof())
break;
if(alp=='\n')
count++;
}
cout<<"\n no of lines is "<<count;
}

38
void countalpha()
{
char alp;
int count=0;
ifstream fin("text1.txt");
while(1)
{
alp=fin.get();
if(fin.eof())
break;
if(isalpha(alp))
count++;
}
cout<<"no of alphabets:"<<count;
}
void countvol()
{ char alp;
int count=0;
ifstream fin("text1.txt");
while(1)
{ alp=fin.get();
if(fin.eof())
break;
switch(alp)
{
case'A':
case'E':
case'I':
case'O':
case'U':
case'a':
case'e':
case'i':
case'o':
case'u':
count++;
}
}
cout<<"\nno of vowels:"<<count;
}

39
void display()
{
int i=0;
ifstream fin("text1.txt",ios::in);
fin.seekg(0);
while(!fin.eof()&&++i)
{ fin.getline(text,99);
if(fin.eof())
break;
cout<<"line"<<i<<":"<<text<<"\n\n";
} }
void main()
{ int chk;
up: cout<<"\n\n\t\t\t File Operations\n";
cout<<"[1]Create text file"
<<"\n[2]count no of alphabets and vowels"
<<"\n[3]count no of lines"
<<"\n[0]exit\n"
<<"enter ur choice:";
cin>>chk;
clrscr();
switch(chk)
{ case 0: exit(0);
case 1: create();
break;
case 2: display();
countalpha();
countvol();
break;
case 3: display();
countlines();
break;
}
goto up; }

40
OUTPUT VALIDATION :
File Operations
[1]Create text file
[2]count no of alphabets and vowels
[3]count no of lines
[0]exit
enter ur choice:1
Number of lines:1
enter line:1:time is precious
File Operations
[1]Create text file
[2]count no of alphabets and vowels
[3]count no of lines
[0]exit
enter ur choice:2
line1:time is precious
no of alphabets:16
no of vowels:7
[1]Create text file
[2]count no of alphabets and vowels
[3]count no of lines
[0]exit
enter ur choice:3
line1:time is precious
no of lines is 1
[1]Create text file
[2]count no of alphabets and vowels
[3]count no of lines
[0]exit
enter ur choice:0

41
PROGRAM NO:11 DATE:

PROBLEM STATEMENT: FILE OPERATIONS


Write a complete menu driven program to perform the following operations
a) Create a text file
b) Count the no of three letter words and display them
SOURCE CODE:
//text file operations
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
void createfile()
{
ofstream fout("first1.txt",ios::out);
char line[80];int n;
cout<<"\n enter no of lines::";cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\n enter line::"<<i+1;
gets(line);
fout<<line;
fout<<"\n";
}
fout.close();
}
void display()
{
char line[80];
ifstream fin("first1.txt");
while(!fin.eof())
{
fin.getline(line,80,'\n');
cout<<line<<endl;
}
fin.close();
}
void threeletter()
{
ifstream fin("first1.txt");
char word[20];
int countthree=0;
42
while(!fin.eof())
{
fin>>word;
if(strlen(word)==3)
{
countthree++;
cout<<word<<"\t";
}
}
cout<<"\n no of three letter word are::"<<countthree;
fin.close();
}
void menu()
{
clrscr();
int n;
char ch='y';
do
{
cout<<"\n 1.CREATE THE TEXT FILE";
cout<<"\n 2.COUNT NO OF THREE LETTER WORDS IN THE FILE";
cout<<"\n 3.DISPLAY FILE CONTENTS";
cout<<"\n 4.EXIT";
cout<<"\n enter your choice::";
cin>>n;
switch(n)
{
case 1:createfile();break;
case 2:threeletter();break;
case 3:display();
case 4:exit(0);
}
cout<<"do u want to continue(y/n)";cin>>ch;
}while(ch=='y');
}
void main()
{
menu();
}

43
OUTPUT VALIDATION:
1.CREATE THE TEXT FILE
2.COUNT NO OF THREE LETTER WORDS IN THE FILE
3.DISPLAY FILE CONTENTS
4.EXIT
enter your choice::1
enter no of lines::2
enter line::1tit for tat
enter line::2old is gold
do u want to continue(y/n)y
1.CREATE THE TEXT FILE
2.COUNT NO OF THREE LETTER WORDS IN THE FILE
3.DISPLAY FILE CONTENTS
4.EXIT
enter your choice::2
tit for tat old
no of three letter word are::4
do u want to continue(y/n)y
1.CREATE THE TEXT FILE
2.COUNT NO OF THREE LETTER WORDS IN THE FILE
3.DISPLAY FILE CONTENTS
4.EXIT
enter your choice::3
tit for tat
old is gold
1.CREATE THE TEXT FILE
2.COUNT NO OF THREE LETTER WORDS IN THE FILE
3.DISPLAY FILE CONTENTS
4.EXIT
enter your choice::4
do u want to continue(y/n)n

44
PROGRAM NO:12 DATE:

PROBLEM STATEMENT: POINTERS

To write a program to perform the operations of the strings as follows:


a) length of the string.
b) copying the given string to another.
c) reversing the string.
SOURCE CODE:

// POINTERS
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void strlength(char *x)
{
int i=0;
for(int j=0;x[j]!='\0';j++)
++i;
cout<<"LENGTH OF THE STRING::"<<x<<" IS::"<<i;
}
void copy(char *x)
{
char *z;
for(int i=0;x[i]!='\0';i++)
z[i]=x[i];
cout<<"COPIED STRING IS::"<<z;
cout<<"ORIGINAL STRING IS::"<<x;
}
void reverse(char *x)
{
char temp;
int l=strlen(x);
for(int i=0,j=l-1;i<=l/2;i++,j--)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
cout<<"REVERSE OF THE STRING IS::"<<x;

45
void main()
{
clrscr();
int ch;
char rep='y';
while(rep=='y')
{
cout<<"\n 1.Length of string";
cout<<"\n 2.Copy";
cout<<"\n 3.Reverse";
cout<<"\n 4.Joining";
cout<<"\n Enter your choice:";
cin>>ch;
char *x=new char[20];
cout<<"\n enter the string::";
gets(x);
switch(ch)
{
case 1:strlength(x);
break;
case 2: copy(x);
break;
case 3:reverse(x);
break;
}
delete x;
cout<<"\n Do you want to continue:";
cin>>rep;
}
getch();
}

46
OUTPUT VALIDATION:-

1.Length of string
2.Copy
3.Reverse
4.Joining
Enter your choice:1
enter the string::helloworld
LENGTH OF THE STRING::helloworld IS::10
Do you want to continue:y
1.Length of string
2.Copy
3.Reverse
4.Joining
Enter your choice:2
enter the string::time is precious
COPIED STRING IS::time is preciousORIGINAL STRING IS::time is precious
Do you want to continue:y

1.Length of string
2.Copy
3.Reverse
4.Joining
Enter your choice:3
enter the string::precious
REVERSE OF THE STRING IS::suocierp
Do you want to continue:n

47

You might also like