Professional Documents
Culture Documents
1.Write a C++ program to read series of names, one per line, from standard input and write these
names spelled in reverse order to the standard output using I/O redirection and pipes. Repeat the
exercise using an input file specified by the user instead of the standard input and using an output file
specified by the user instead of the standard output
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
// function to read names from standard input and display reverse of same names on standard
output //
void standard_io()
{
cout<<"\n Enter the number of names \n";
int n;
cin>>n;
cout<<"\n Enter the names \n";
char name[10][20];
int i;
char *temp1;
for(i=0;i<n;i++)
{
cin>>name[i];
temp1=(char *)malloc(sizeof(name[i]));
temp1=strrev(name[i]);
cout<<"\n"<<temp1;
}
}
// function to read the names , write them into a user defined file and then write the reverse of names
into another user defined file //
void userfile_io()
{
cout<<"\n Enter the name of the file into which names have to be stored \n";
char ifilename[10];
cin>>ifilename;
cout<<"\n Enter the name of the file into which the names will have to be written in reverse order
\n";
char revfilename[20];
cin>>revfilename;
cout<<"\n Enter the number of names \n";
int n;
cin>>n;
cout<<"\n Enter the name \n";
char name[10][10];
-1-
char *temp1;
ofstream out(ifilename);
int i;
for(i=0;i<n;i++)
{
cin>>name[i];
out<<name[i]<<endl;
}
out.close();
ifstream in(ifilename);
char temp[10];
out.open(revfilename,ios::out);
while(in>>temp!=NULL)
{
temp1=(char *)malloc(sizeof(temp));
temp1=strrev(temp);
out<<temp1<<endl;
}
in.close();
out.close();
}
// main function begins //
int main()
{
int ch; do
{
cout<<"\n 1-Standara input/output \n 2-User specified file input/output \n 3-Exit \n Enter
your choice \n";
cin>>ch;
if(ch==1)
standard_io();
else if(ch==2)
userfile_io();
}
while(ch<3);
return 0;
}
// main function ends //
OR
-2-
#include<fstream.h>
#include<iostream.h>
#include<string.h>
#include<conio.h>
int main()
{
char name[10][20];
char temp[20];
char ifilename[15], ofilename[15];
int num;
int i=0;
cout<<"Enter how many person details you want to enter: "; cin>>num;
cout<<"Enter the file name where it has to be written: "; cin>>ofilename;
ofstream ofileobj(ofilename,ios::out);
cout<<"Enter person details: ";
while(i<num)
{
cin>>name[i];
ofileobj<<name[i]<<endl;
i++;
}
ofileobj.close();
cout<<"Enter the file name where output has to be written: \n ";
cin>>ifilename;
ifstream inobj(ofilename,ios::in);
ofileobj.open(ifilename,ios::out);
cout<<"The details of the person in reverse that are entered is:\n";
while(inobj >> temp != NULL)
{
cout<<strrev(temp)<<endl;
ofileobj<<temp<<endl;
}
getch();
return 0;
}
2. Write a C++ program to read and write student objects with fixed-length records and the fields
delimited by |. Implement pack ( ), unpack ( ), modify ( ) and search ( ) methods
-3-
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>
class student
{
public: char name[15],usn[15],age[5],sem[5],branch[15],buffer[45];
};
student s2[100];
void writeRecord()
//Function to add record to file
{
fstream app;
student s;
app.open("student.txt",ios::app);
//Open file in append mode
if(!app)
{
cout<<"cannot open the file in output mode";
getch();
exit(0);
}
cout<<"\n Enter the student name = "; cin>>s.name;
cout<<"\n Enter the usn = "; cin>>s.usn;
cout<<"\n Enter the age = "; cin>>s.age;
cout<<"\n Enter the sem = "; cin>>s.sem;
cout<<"\n Enter the branch = "; cin>>s.branch;
//packing the information
strcpy(s.buffer, s.name); strcat(s.buffer,"|");
strcat(s.buffer, s.usn); strcat(s.buffer,"|");
strcat(s.buffer, s.age); strcat(s.buffer,"|");
strcat(s.buffer, s.sem); strcat(s.buffer,"|");
strcat(s.buffer, s.branch);
int count=strlen(s.buffer);
for(int k=0;k<45-count;k++)
strcat(s.buffer,"!");
strcat(s.buffer,"\n");
app<<s.buffer; //writing the packed information to buffer
app.close();
}
void search()
-4-
{
fstream in;
char usn[15], extra[45];
in.open("student.txt",ios::in);
if(!in)
{
cout<<"\nUnable to open the file in input mode";
getch();
exit(0);
}
cout<<"\nEnter the record's usn you want to search = "; cin>>usn;
student s;
//Unpacking the record
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'|');
in.getline(extra,45,'\n');
if(strcmp(s.usn,usn)==0)
{
cout<<"\nRecord found";
cout<<"\n"<<s.name<<"\t"<<s.usn<<"\t"<<s.age<<"\t"<<s.sem<<"\t"<<s.branch;
getch();
return;
}
}
cout<<"\n Record not found";
getch();
return;
}
void displayFile()
{
student s;
int c,i;
char extra[45];
fstream in;
in.open("student.txt",ios::in);
if(!in)
{
cout<<"\nCannot open the file in output mode";
getch();
exit(0);
}
i=0;
printf("Name\t\tUsn\t\tAge\t\tSem\t\tBranch\n");
-5-
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'!');
in.getline(extra,45,'\n');
printf("\n%s\t\t%s\t\t%s\t\t%s\t\t%s",s.name,s.usn,s.age,s.sem,s.branch);
i++;
}
in.close();
getch();
}
void modify()
{
fstream in;
char usn[15],buffer[45],extra[45];
int i,j;
student s1[100];
in.open("student.txt",ios::in);
if(!in)
{
cout<<"\nUnable to open the file in input mode";
getch();
exit(0);
}
cout<<"\nEnter the usn"; cin>>usn;
i=0;
//Loading the file to Main memory
while(!in.eof())
{
in.getline(s1[i].name,15,'|');
in.getline(s1[i].usn,15,'|');
in.getline(s1[i].age,5,'|');
in.getline(s1[i].sem,5,'|');
in.getline(s1[i]. branch, 15,'\n');
in.getline(extra,45,'\n');
i++;
}
i--;
for(j=0;j<i;j++)
{
if(strcmp(usn,s1[j].usn)==0)
{
cout<<"\nThe old values of the record with usn "<<usn<<" are ";
cout<<"\nname = "<< s1[j].name;
cout<<"\nusn = "<< s1[j].usn;
cout<<"\nage = "<< s1[j].age;
cout<<"\nsem = "<< s1[j].sem;
-6-
-7-
3. Write a C++ program to read and write student objects with Variable - Length records using any
suitable record structure. Implement pack ( ), unpack ( ), modify ( ) and search ( ) methods.
-8-
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>
class student
{
public: char name[15],usn[15],age[5],sem[5],branch[15],buffer[100];
};
student s2[100];
void writeRecord()
{
fstream app;
student s;
app.open("student.txt",ios::app); //Open file in append mode
if(!app)
{
cout<<"cannot open the file in output mode";
getch();
exit(0);
}
cout<<"\n Enter the student name = "; cin>>s.name;
cout<<"\n Enter the usn = "; cin>>s.usn;
cout<<"\n Enter the age = "; cin>>s.age;
cout<<"\n Enter the sem = "; cin>>s.sem;
cout<<"\n Enter the branch = "; cin>>s.branch;
//packing the information
strcpy(s.buffer, s.name); strcat(s.buffer,"|");
strcat(s.buffer, s.usn); strcat(s.buffer,"|");
strcat(s.buffer, s.age); strcat(s.buffer,"|");
strcat(s.buffer, s.sem); strcat(s.buffer,"|");
strcat(s.buffer, s.branch);strcat(s.buffer,"\n");
app<<s.buffer; //writing the packed information to buffer
app.close();
}
void search()
{
fstream in;
char usn[15], extra[45];
in.open("student.txt",ios::in);
if(!in)
-9-
{
cout<<"\nUnable to open the file in input mode";
getch();
exit(0);
}
cout<<"\nEnter the record's usn you want to search = "; cin>>usn;
student s;
//Unpacking the record
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'\n');
if(strcmp(s.usn,usn)==0)
{
cout<<"\nRecord found";
cout<<"\n"<<s.name<<"\t"<<s.usn<<"\t"<<s.age<<"\t"<<s.sem<<"\t"<<s.branch;
getch();
return;
}
}
cout<<"\n Record not found";
getch();
return;
}
void displayFile()
{
student s;
int c,i;
fstream in;
in.open("student.txt",ios::in);
if(!in)
{
cout<<"\nCannot open the file in output mode";
getch();
exit(0);
}
i=0;
printf("Name\t\tUsn\t\tAge\t\tSem\t\tBranch\n");
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
- 10 -
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'!');
printf("\n%s\t\t%s\t\t%s\t\t%s\t\t%s",s.name,s.usn,s.age,s.sem,s.branch);
i++;
}
in.close();
getch();
}
void modify()
{
fstream in;
char usn[15],buffer[45],extra[45];
int i,j;
student s1[100];
in.open("student.txt",ios::in);
if(!in)
{
cout<<"\nUnable to open the file in input mode";
getch();
exit(0);
}
cout<<"\nEnter the usn"; cin>>usn;
i=0;
//Loading the file to Main memory
while(!in.eof())
{
in.getline(s1[i].name,15,'|');
in.getline(s1[i].usn,15,'|');
in.getline(s1[i].age,5,'|');
in.getline(s1[i].sem,5,'|');
in.getline(s1[i]. branch, 15,'\n');
i++;
}
i--;
for(j=0;j<i;j++)
{
if(strcmp(usn,s1[j].usn)==0)
{
cout<<"\nThe old values of the record with usn "<<usn<<" are ";
cout<<"\nname = "<< s1[j].name;
cout<<"\nusn = "<< s1[j].usn;
cout<<"\nage = "<< s1[j].age;
cout<<"\nsem = "<< s1[j].sem;
cout<<"\nbranch = "<< s1[j].branch;
cout<<"\nEnter the new values \n";
cout<<"\nname = "; cin>>s1[j].name;
cout<<"\nusn = "; cin>>s1[j].usn;
cout<<"\nage = "; cin>>s1[j].age;
- 11 -
- 12 -
switch(ch)
{
case 1: writeRecord();break;
case 2: displayFile();break;
case 3: modify();break;
case 4: search (); break;
case 0: exit(0);
default: cout<<"\nInvalid input....";break;
}
}
}
4. Write a C++ program to write student objects with Variable Length records using any suitable
record structure and to read from this file a student record using RRN
- 13 -
#include<iostream.h>
#include<fstream.h>
#define SIZEOFRECORD 66
int no_of_records;
class student
{
public:
char name[15], usn[15], branch[15], sem[15];
};
void displayontoterminal(student);
void pack(student);
void writefrombuffertofile(char[]);
void unpack(char *buffer)
{
student stu;
int i=0,j=0,count=0;
char temp[16];
while(buffer[i]!='!')
{
j=0;
while(buffer[i]!='|')
temp[j++]=buffer[i++];
i++;
count++;
temp[j]='\0';
switch(count)
{
case 1 : strcpy(stu.name,temp);
break;
case 2 : strcpy(stu.usn,temp);
break;
case 3 : strcpy(stu.branch,temp);
break;
case 4 : strcpy(stu.sem,temp);
break;
}
}
displayontoterminal(stu);
}
void writetofile()
{
student stu;
cout<<"Enter the name of the student\n";
cin>>stu.name;
cout<<"Enter the usn of the student";
cin>>stu.usn;
cout<<"Enter the branch of the student\n";
cin>>stu.branch;
- 14 -
- 15 -
writetofile<<buffer;
writetofile.close();
no_of_records++;
}
void displayontoterminal(student stu)
{
cout<<"Name\t\t\tusn\t\t\tBranch\t\t\tSemester\t\t\t\n";
cout<<stu.name<<"\t\t\t"<<stu.usn<<"\t\t\t"<<stu.branch<<"\t\t\t"<<stu.sem<<endl;
}
int main()
{
int ch=1;
while(ch)
{
cout<<"\n Enter 1. write to the file \n 2. Access record by RRN \n 3. Exit\n Enterur choice:";
cin>>ch;
switch(ch)
{
case 1:
writetofile();
break;
case 2:
accessrecordbyRRN();
break;
case 3:
exit(0);
}
}
return 0;
}
5. Write a C++ program to implement simple index on primary key for a file of student objects.
Implement add ( ), search ( ), delete ( ) using the index.
- 16 -
include<iostream>
#include<string.h>
#include<fstream>
#include<stdlib.h>
using namespace std;
//Record specification
class record
{
public: char age[5];
char usn[20],name[20],branch[5];
char sem[2];
}rec[20];
char st_no[5];
int no;
void retrieve_details()
{
fstream file2;
char name[20],usn[20],branch[5];
char ind[5],age[5],sem[5];
file2.open("record.txt",ios::in);
for(int i=0;i<no;i++) //Unpacking record data
{
file2.getline(ind,5,'|');
file2.getline(usn,20,'|');
file2.getline(name,20,'|');
file2.getline(age,5,'|');
file2.getline(sem,5,'|');
file2.getline(branch,5,'\n');
if(strcmp(ind,st_no)==0) //Required record found- so print details
{
cout<<"\n\n"<<"Student details are: ";
cout<<"\n\nUSN: "<<usn<<"\nName: "<<name<<"\nAge: "<<age<<"\nSem:
"<<sem<<"\nBranch: "<<branch<<"\n";
}
}
file2.close();
}
void delete_record(char usno[])
{
int i;
fstream file1, file2;
char age[5],sem[5],branch[5],usn[20],name[20],ind[5];
file2.open("record.txt",ios::in);
for(i=0;i<no;i++) //Unpack records
{
file2.getline(ind,5,'|');
file2.getline(usn,20,'|');
file2.getline(name,20,'|');
- 17 -
file2.getline(age,5,'|');
file2.getline(sem,5,'|');
file2.getline(branch,5,'\n');
strcpy(rec[i].usn,usn);
strcpy(rec[i].name,name);
strcpy(rec[i].age,age);
strcpy(rec[i].sem,sem);
strcpy(rec[i].branch,branch);
}
int flag=-1;
for(i=0;i<no;i++)
//Check for the record's existence
{
if(strcmp(rec[i].usn,usno)==0)
flag=i;
}
if(flag==-1) //Record not found
{
cout<<"Error !\n";
return;
}
if(flag==(no-1))
//Delete found record
{
no--;
cout<<"Deleted !\n";
return;
}
for(i=flag;i<no;i++)
{
rec[i]=rec[i+1];
}
no--;
cout<<"\nDeleted !\n";
file2.close();
file1.open("index.txt",ios::out); //Open index and record files
file2.open("record.txt",ios::out); //After deletion
for(i=0;i<no;i++)
//Pack index n record data onto files
{
file1<<rec[i].usn<<"|"<<i<<"\n";
file2<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"<<rec[i].age<<"|"<<rec[i].sem<<"|"<<rec[i].branch<<"\n
";
}
file1.close();
file2.close();
return;
}
int main()
{
fstream file1,file2;
int ch;
- 18 -
char rt_usn[20],st_usn[20];
char ind[2],name[20],age[2],sem[5],branch[5];
int i,flag,flag1;
file1.open("index.txt",ios::out);
file2.open("record.txt",ios::out);
if(!file1 || !file2)
{
cout<<"File creation Error! \n";
exit(0);
}
for(;;)
{
cout<<"\n1: Add Record"
<<"\n2: Search Record"
<<"\n3: Delete Record"
<<"\n4: Display Record";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the no. of students : "; cin>>no;
cout<<"Enter the details:\n";
for(i=0;i<no;i++) //Pack data onto the index and record files
{
//USN is the indexed data
cout<<"\nName: "; cin>>rec[i].name;
cout<<"Age: "; cin>>rec[i].age;
cout<<"USN: "; cin>>rec[i].usn;
cout<<"Sem: "; cin>>rec[i].sem;
cout<<"Branch: "; cin>>rec[i].branch;
file1<<rec[i].usn<<"|"<<i<<"\n";
file2<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"<<rec[i].age<<"|"<<rec[i].sem<<"|"<<rec[i].branch<<"\n
";
}
file1.close();
file2.close();
break;
case 2: cout<<"Enter USN whose record is to be displayed: ";
cin>>st_usn;
file1.open("index.txt",ios::in);
if(!file1)
{
cout<<"\nError !\n";
exit(0);
}
flag1=0;
for(i=0;i<no;i++)
{
file1.getline(rt_usn,20,'|'); //Unpack index file and
file1.getline(st_no,4,'\n'); //look for a match in the USN
if(strcmp(st_usn,rt_usn)==0)
{
retrieve_details();
//Retrieve details if index found
- 19 -
flag1=1;
}
}
if(!flag1)
cout<<"Record search failed!\n";
file1.close();
break;
case 3: cout<<"Enter USN whose record is to be deleted: ";
cin>>st_usn;
file1.open("index.txt", ios::in);
if(!file1)
{
cout<<"Error! \n";
exit(0);
}
flag=0;
for(i=0;i<no;i++)
{
file1.getline(rt_usn,20,'|'); //Search index file and
file1.getline(st_no,4,'\n'); //call del if index found
if(strcmp(st_usn, rt_usn)==0)
{
delete_record(rt_usn);
flag=1;
}
}
if(!flag)
cout<<"Deletion Failed\n";
file1.close();
break;
case 4: for(i=0;i<no;i++)
{
cout<<"\n\n USN: "<<rec[i].usn
<<"\n Name: "<<rec[i].name
<<"\n Age: "<<rec[i].age
<<"\n Sem: "<<rec[i].sem
<<"\n Branch: "<<rec[i].branch;
}
break;
default: cout<<"Invalid choice";
exit(0);
break;
}
}
}
OR
- 20 -
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
//Record specification
class record
{
public: char age[5];
char usn[20],name[20],branch[5];
char sem[2];
}rec[20];
char st_no[5];
int no;
void retrieve_details()
{
fstream file2;
char name[20],usn[20],branch[5];
char ind[5],age[5],sem[5];
file2.open("record.txt",ios::in);
for(int i=0;i<no;i++) //Unpacking record data
{
file2.getline(ind,5,'|');
file2.getline(usn,20,'|');
file2.getline(name,20,'|');
file2.getline(age,5,'|');
file2.getline(sem,5,'|');
file2.getline(branch,5,'\n');
if(strcmp(ind,st_no)==0) //Required record found- so print details
{
cout<<"\n\n"<<"Student details are: ";
cout<<"\n\nUSN: "<<usn<<"\nName: "<<name<<"\nAge: "<<age<<"\nSem:
"<<sem<<"\nBranch: "<<branch<<"\n";
}
}
file2.close();
}
void delete_record(char usno[])
{
int i;
fstream file1, file2;
char age[5],sem[5],branch[5],usn[20],name[20],ind[5];
file2.open("record.txt",ios::in);
for(i=0;i<no;i++) //Unpack records
{
file2.getline(ind,5,'|');
file2.getline(usn,20,'|');
file2.getline(name,20,'|');
file2.getline(age,5,'|');
- 21 -
file2.getline(sem,5,'|');
file2.getline(branch,5,'\n');
strcpy(rec[i].usn,usn);
strcpy(rec[i].name,name);
strcpy(rec[i].age,age);
strcpy(rec[i].sem,sem);
strcpy(rec[i].branch,branch);
}
int flag=-1;
for(i=0;i<no;i++)
//Check for the record's existence
{
if(strcmp(rec[i].usn,usno)==0)
flag=i;
}
if(flag==-1) //Record not found
{
cout<<"Error !\n";
return;
}
if(flag==(no-1))
//Delete found record
{
no--;
cout<<"Deleted !\n";
return;
}
for(i=flag;i<no;i++)
{
rec[i]=rec[i+1];
}
no--;
cout<<"\nDeleted !\n";
file2.close();
file1.open("index.txt",ios::out); //Open index and record files
file2.open("record.txt",ios::out); //After deletion
for(i=0;i<no;i++)
//Pack index n record data onto files
{
file1<<rec[i].usn<<"|"<<i<<"\n";
file2<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"<<rec[i].age<<"|"<<rec[i].sem<<"|"<<rec[i].branch<<"\n
";
}
file1.close();
file2.close();
return;
}
int main()
{
fstream file1,file2;
int ch;
char rt_usn[20],st_usn[20];
- 22 -
char ind[2],name[20],age[2],sem[5],branch[5];
int i,flag,flag1;
file1.open("index.txt",ios::out);
file2.open("record.txt",ios::out);
if(!file1 || !file2)
{
cout<<"File creation Error! \n";
exit(0);
}
for(;;)
{
cout<<"\n1: Add Record"
<<"\n2: Search Record"
<<"\n3: Delete Record"
<<"\n4: Display Record";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the no. of students : "; cin>>no;
cout<<"Enter the details:\n";
for(i=0;i<no;i++) //Pack data onto the index and record files
{
//USN is the indexed data
cout<<"\nName: "; cin>>rec[i].name;
cout<<"Age: "; cin>>rec[i].age;
cout<<"USN: "; cin>>rec[i].usn;
cout<<"Sem: "; cin>>rec[i].sem;
cout<<"Branch: "; cin>>rec[i].branch;
file1<<rec[i].usn<<"|"<<i<<"\n";
file2<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"<<rec[i].age<<"|"<<rec[i].sem<<"|"<<rec[i].branch<<"\n
";
}
file1.close();
file2.close();
break;
case 2: cout<<"Enter USN whose record is to be displayed: ";
cin>>st_usn;
file1.open("index.txt",ios::in);
if(!file1)
{
cout<<"\nError !\n";
exit(0);
}
flag1=0;
for(i=0;i<no;i++)
{
file1.getline(rt_usn,20,'|'); //Unpack index file and
file1.getline(st_no,4,'\n'); //look for a match in the USN
if(strcmp(st_usn,rt_usn)==0)
{
retrieve_details();
//Retrieve details if index found
flag1=1;
- 23 -
}
}
if(!flag1)
cout<<"Record search failed!\n";
file1.close();
break;
case 3: cout<<"Enter USN whose record is to be deleted: ";
cin>>st_usn;
file1.open("index.txt", ios::in);
if(!file1)
{
cout<<"Error! \n";
exit(0);
}
flag=0;
for(i=0;i<no;i++)
{
file1.getline(rt_usn,20,'|'); //Search index file and
file1.getline(st_no,4,'\n'); //call del if index found
if(strcmp(st_usn, rt_usn)==0)
{
delete_record(rt_usn);
flag=1;
}
}
if(!flag)
cout<<"Deletion Failed\n";
file1.close();
break;
case 4: for(i=0;i<no;i++)
{
cout<<"\n\n USN: "<<rec[i].usn
<<"\n Name: "<<rec[i].name
<<"\n Age: "<<rec[i].age
<<"\n Sem: "<<rec[i].sem
<<"\n Branch: "<<rec[i].branch;
}
break;
default: cout<<"Invalid choice";
exit(0);
break;
}
}
}
6. Write a C++ program to implement index on secondary key, the name, for a file of student objects.
Implement add ( ), search ( ), delete ( ) using the secondary index.
- 24 -
#include<iostream>
#include<string.h>
#include<fstream>
#include<stdlib.h>
using namespace std;
//using namespace std;
class record
{
public: char age[5];
char usn[20],name[20],branch[5]; char sem[2];
}rec[20],found[20];
char st_no[5],rt_name[20];
int no;
void sort_records()
{
int i,j;
record temp;
for(i=0;i<no-1;i++)
for(j=0;j<no-i-1;j++)
if(strcmp(rec[j].name, rec[j+1].name) > 0)
{
temp=rec[j];
rec[j]=rec[j+1];
rec[j+1]=temp;
}
}
void create_indexfile()
{
fstream index,index2;
int i;
index.open("secindex.txt",ios::out);
index2.open("record.txt",ios::out);
for(i=0;i<no;i++)
{
index<<rec[i].name<<"|"
<<rec[i].usn<<"|"<<i<<"\n";
index2<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"<<rec[i].age<<"|"
<<rec[i].sem<<"|"<<rec[i].branch<<"\n";
}
}
void retrieve_record(char* index)
{
fstream file;
int i;
char ind[2],usn[20],name[20],age[3],sem[3],branch[10];
file.open("record.txt",ios::in);
for(i=0;i<no;i++)
- 25 -
{
file.getline(ind,4,'|');
file.getline(usn,20,'|');
file.getline(name,20,'|');
file.getline(age,4,'|');
file.getline(sem,4,'|');
file.getline(branch,5,'\n');
if(strcmp(index,ind) == 0)
cout<<"\nUSN: "<<usn
<<"\nName: "<<name
<<"\nAge: "<<age
<<"\nSem: "<<sem
<<"\nBranch: "<<branch;
}
file.close();
return;
}
void retrieve_details()
{
int k=0,i;
char name[20],usn[20],ind[2];
char chusn[20];
char index[20][20];
fstream file;
file.open("secindex.txt",ios::in);
for(i=0;i<no;i++)
{
file.getline(name,20,'|');
file.getline(usn,20,'|');
file.getline(ind,4,'\n');
if(strcmp(name,rt_name) == 0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(index[k],ind);
k++;
}
}
file.close();
if(k==1)
{
retrieve_record(index[0]);
return;
}
else
{
cout<<"Please choose the candidate's USN: \n";
for(i=0;i<k;i++)
cout<<"Name: "<<found[i].name<<" USN: "<<found[i].usn<<endl;
}
cin>>chusn;
- 26 -
for(i=0;i<k;i++)
{
if(strcmp(chusn,found[i].usn) == 0)
{
retrieve_record(index[i]);
return;
}
}
cout<<"Invalid Entry! \n";
return;
}
void delete_record(char indx[])
{
int i;
fstream file1,file2;
char age[5],sem[5],branch[5],usn[20],name[20],ind[5];
char index[20][20];
file2.open("record.txt",ios::in);
for(i=0;i<no;i++)
{
file2.getline(ind,4,'|');
file2.getline(usn,20,'|');
file2.getline(name,20,'|');
file2.getline(age,5,'|');
file2.getline(sem,5,'|');
file2.getline(branch,8,'\n');
strcpy(index[i],ind);
strcpy(rec[i].usn,usn);
strcpy(rec[i].name,name);
strcpy(rec[i].age,age);
strcpy(rec[i].sem,sem);
strcpy(rec[i].branch,branch);
}
int flag=-1;
for(i=0;i<no;i++)
{
if(strcmp(index[i],indx) == 0)
flag=i;
}
if(flag==-1)
{
cout<<"Error!\n";
return;
}
if(flag==(no-1))
{
no--;
cout<<"Deleted!\n";
return;
}
for(i=flag;i<no;i++)
- 27 -
{
rec[i]=rec[i+1];
}
no--;
cout<<"Deleted!\n";
file2.close();
file1.open("secindex.txt",ios::out);
file2.open("record.txt",ios::out);
for(i=0;i<no;i++)
{
file1<<rec[i].name<<"|"<<rec[i].usn<<"|"<<i<<"\n";
file2<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"
<<rec[i].age<<"|"<<rec[i].sem<<"|"<<rec[i].branch<<"\n";
}
file1.close();
file2.close();
return;
}
void delete_index(char* nam)
{
fstream file;
int i;
int k=0;
char name[20],usn[20],ind[5],index[20][20],chusn[20];
file.open("secindex.txt",ios::in);
for(i=0;i<no;i++)
{
file.getline(name,20,'|');
file.getline(usn,20,'|');
file.getline(ind,4,'\n');
if(strcmp(nam,name)==0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(index[k],ind);
k++;
}
}
file.close();
if(k==1)
{
delete_record(index[0]);
return;
}
else
{
cout<<"Please choose the candidate's USN: \n";
for(i=0;i<k;i++)
{
- 28 -
- 29 -
- 30 -
break;
case 4: for(i=0;i<no;i++)
{
cout<<"\n\nUSN : "<<rec[i].usn
<<"\nName: "<<rec[i].name
<<"\nAge : "<<rec[i].age
<<"\nSem : "<<rec[i].sem
<<"\nBranch : "<<rec[i].branch<<"\n";
}
break;
default: cout<<"Invalid option !\n";
exit(0);
break;
}
}
return 0;
}
OR
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
//using namespace std;
class record
{
public: char age[5];
char usn[20],name[20],branch[5]; char sem[2];
}rec[20],found[20];
char st_no[5],rt_name[20];
int no;
void sort_records()
{
int i,j;
record temp;
for(i=0;i<no-1;i++)
for(j=0;j<no-i-1;j++)
if(strcmp(rec[j].name, rec[j+1].name) > 0)
{
temp=rec[j];
rec[j]=rec[j+1];
rec[j+1]=temp;
}
}
- 31 -
void create_indexfile()
{
fstream index,index2;
int i;
index.open("secindex.txt",ios::out);
index2.open("record.txt",ios::out);
for(i=0;i<no;i++)
{
index<<rec[i].name<<"|"
<<rec[i].usn<<"|"<<i<<"\n";
index2<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"<<rec[i].age<<"|"
<<rec[i].sem<<"|"<<rec[i].branch<<"\n";
}
}
void retrieve_record(char* index)
{
fstream file;
int i;
char ind[2],usn[20],name[20],age[3],sem[3],branch[10];
file.open("record.txt",ios::in);
for(i=0;i<no;i++)
{
file.getline(ind,4,'|');
file.getline(usn,20,'|');
file.getline(name,20,'|');
file.getline(age,4,'|');
file.getline(sem,4,'|');
file.getline(branch,5,'\n');
if(strcmp(index,ind) == 0)
cout<<"\nUSN: "<<usn
<<"\nName: "<<name
<<"\nAge: "<<age
<<"\nSem: "<<sem
<<"\nBranch: "<<branch;
}
file.close();
return;
}
void retrieve_details()
{
int k=0,i;
char name[20],usn[20],ind[2];
char chusn[20];
char index[20][20];
fstream file;
file.open("secindex.txt",ios::in);
for(i=0;i<no;i++)
{
file.getline(name,20,'|');
file.getline(usn,20,'|');
- 32 -
file.getline(ind,4,'\n');
if(strcmp(name,rt_name) == 0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(index[k],ind);
k++;
}
}
file.close();
if(k==1)
{
retrieve_record(index[0]);
return;
}
else
{
cout<<"Please choose the candidate's USN: \n";
for(i=0;i<k;i++)
cout<<"Name: "<<found[i].name<<" USN: "<<found[i].usn<<endl;
}
cin>>chusn;
for(i=0;i<k;i++)
{
if(strcmp(chusn,found[i].usn) == 0)
{
retrieve_record(index[i]);
return;
}
}
cout<<"Invalid Entry! \n";
return;
}
void delete_record(char indx[])
{
int i;
fstream file1,file2;
char age[5],sem[5],branch[5],usn[20],name[20],ind[5];
char index[20][20];
file2.open("record.txt",ios::in);
for(i=0;i<no;i++)
{
file2.getline(ind,4,'|');
file2.getline(usn,20,'|');
file2.getline(name,20,'|');
file2.getline(age,5,'|');
file2.getline(sem,5,'|');
file2.getline(branch,8,'\n');
strcpy(index[i],ind);
strcpy(rec[i].usn,usn);
strcpy(rec[i].name,name);
- 33 -
strcpy(rec[i].age,age);
strcpy(rec[i].sem,sem);
strcpy(rec[i].branch,branch);
}
int flag=-1;
for(i=0;i<no;i++)
{
if(strcmp(index[i],indx) == 0)
flag=i;
}
if(flag==-1)
{
cout<<"Error!\n";
return;
}
if(flag==(no-1))
{
no--;
cout<<"Deleted!\n";
return;
}
for(i=flag;i<no;i++)
{
rec[i]=rec[i+1];
}
no--;
cout<<"Deleted!\n";
file2.close();
file1.open("secindex.txt",ios::out);
file2.open("record.txt",ios::out);
for(i=0;i<no;i++)
{
file1<<rec[i].name<<"|"<<rec[i].usn<<"|"<<i<<"\n";
file2<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"
<<rec[i].age<<"|"<<rec[i].sem<<"|"<<rec[i].branch<<"\n";
}
file1.close();
file2.close();
return;
}
void delete_index(char* nam)
{
fstream file;
int i;
int k=0;
char name[20],usn[20],ind[5],index[20][20],chusn[20];
file.open("secindex.txt",ios::in);
for(i=0;i<no;i++)
{
- 34 -
file.getline(name,20,'|');
file.getline(usn,20,'|');
file.getline(ind,4,'\n');
if(strcmp(nam,name)==0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(index[k],ind);
k++;
}
}
file.close();
if(k==1)
{
delete_record(index[0]);
return;
}
else
{
cout<<"Please choose the candidate's USN: \n";
for(i=0;i<k;i++)
{
cout<<"Name: "<<found[i].name<<" USN: "<<found[i].usn<<endl;
}
}
cin>>chusn;
for(i=0;i<k;i++)
{
if(strcmp(chusn,found[i].usn)==0)
{
delete_record(index[i]);
return;
}
}
cout<<"Invalid Entry!\n";
return;
}
int main()
{
fstream file1,file2;
int ch;
char rt_usn[20],st_name[20],st_usn[20];
char ind[2],name[20],age[2],sem[5],branch[5];
int i,flag,flag1;
file1.open("index.txt",ios::out);
file2.open("record.txt",ios::out);
if(!file1 || !file2)
{
cout<<"File creation Error!\n";
exit(0);
}
- 35 -
for(;;)
{
cout<<"\nl:Add Record\n 2:Search Record"
<<"\n3:Delete Record\n 4:Display Record\n";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the no. of students : "; cin>>no;
cout<<"Enter the details :\n";
for(i=0;i<no;i++)
{
cout<<"\nName : "; cin>>rec[i].name;
cout<<"Age : "; cin>>rec[i].age;
cout<<"USN : "; cin>>rec[i].usn;
cout<<"Sem : "; cin>>rec[i].sem;
cout<<"Branch : "; cin>>rec[i].branch;
}
sort_records();
create_indexfile();
file1.close ();
file2.close();
break;
case 2: cout<<"Enter name of the student whose record is to be displayed\n";
cin>>st_name;
file1.open("secindex.txt",ios::in);
if(!file1)
{
cout<<"Error !\n";
exit(0);
}
flag1=0;
for(i=0;i<no;i++)
{
file1.getline(rt_name,20,'|');
file1.getline(rt_usn,20,'|');
file1.getline(st_no,4,'\n');
if(strcmp(st_name,rt_name)==0)
{
retrieve_details();
flag1=1;
goto one;
}
}
one: if(!flag1)
cout<<"Record search failed !\n";
file1.close();
break;
case 3: cout<<"Enter name of student whose record is to be deleted\n";
cin>>st_name;
- 36 -
file1.open("secindex.txt",ios::in);
if(!file1)
{
cout<<"Error !\n";
exit(0);
}
flag=0;
for(i=0;i<no;i++)
{
file1.getline(rt_name,20,'|');
file1.getline(rt_usn,20,'|');
file1.getline(ind,4,'\n');
if(strcmp(st_name,rt_name) == 0)
{
delete_index(rt_name);
flag=1;
}
}
if(!flag)
cout<<"Deletion failed !\n";
file1.close();
break;
case 4: for(i=0;i<no;i++)
{
cout<<"\n\nUSN : "<<rec[i].usn
<<"\nName: "<<rec[i].name
<<"\nAge : "<<rec[i].age
<<"\nSem : "<<rec[i].sem
<<"\nBranch : "<<rec[i].branch<<"\n";
}
break;
default: cout<<"Invalid option !\n";
exit(0);
break;
}
}
return 0;
}
7. Write a C++ program to read two lists of names and then match the names in the two lists using
Cosequential Match based on a single loop. Output the names common to both the lists
- 37 -
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream.h>
#include<iostream.h>
void writeLists()
{
fstream out1,out2;
int i,m,n;
char name[20];
out1.open("file1.txt",ios::out);
out2.open("file2.txt",ios::out);
if( (!out1) || (!out2) )
{
cout<<"Unable to open one of the list files";
getch();
exit(0);
}
cout<<"Enter no. of names you want to enter in file1"; cin>>m;
cout<<"\nEnter the names in ascending order\n";
for(i-0;i<m;i++)
{
cin>>name;
out1<<name;
out1<<'\n';
}
cout<<"Enter no. of names you want to enter in file2"; cin>>n;
cout<<"\nEnter the names in ascending order \n";
for(i=0;i<n;i++)
{
cin>>name;
out2<<name;
out2<<'\n';
}
out1.close();
out2.close();
}
void main()
{
char list1[100][20], list2[100][20];
int i,j,m,n;
clrscr();
fstream out1,out2,out3;
writeLists();
- 38 -
out1.open("file1.txt",ios::in);
out2.open("file2.txt",ios::in);
out3.open("file3.txt",ios::out);
if( (!out3) || (!out1) || (!out2) )
{
printf("Unable to open one of the file");
getch();
exit(0);
}
clrscr();
m=0;
n=0;
while(!out1.eof())
{
out1.getline(list1[m],20,'\n');
cout<<list1[m];
m++;
}
while(!out2.eof())
{
out2.getline(list2[n],20,'\n');
cout<<list2[n];
n++;
}
m--;
n--;
i=0;
j=0;
cout<<"\nElements common to both files are: \n ";
while(i<m && j<n)
{
if( strcmp(list1[i],list2[j]) == 0 )
{
out3<<list1[i];
cout<<list1[i]<<"\n";
out3<<'\n';
i++;
j++;
}
else if( strcmp(list1[i],list2[j]) < 0 )
i++;
else
j++;
}
getch();
}
8. Write a C++ program to read k Lists of names and merge them using k-way merge algorithm with
k = 8.
#include<iostream.h>
- 39 -
#include<fstream.h>
#include<string.h>
// Record specification
class record
{
public: char name[20];
char usn[20];
}rec[20];
int no;
fstream file[8];
//The first 8 files
char fname[8][8] = {"l.txt","2.txt","3.txt","4.txt","5.txt","6.txt","7.txt","8.txt"};
void merge_file(char* file1, char* file2, char* filename)
{
record recrd[20];
int k; k=0;
fstream f1,f2;
f1.open(file1,ios::in); //open the first file
f2.open(file2,ios::in); //open the second file
while(!f1.eof()) //Unpack and retrieve first file
{
f1.getline(recrd[k].name,20,'|');
f1.getline(recrd[k++].usn,20,'\n');
}
while(!f2.eof()) //Unpack and retrieve second file
{
f2.getline(recrd[k].name,20,'|');
f2.getline(recrd [k++].usn, 20,'\n');
}
record temp;
int t,y;
for(t=0;t<k-2;t++) //Sort the retrieved records
for(y=0;y<k-t-2;y++)
if(strcmp(recrd[y].name,recrd[y+1].name)>0)
{
temp=recrd[y];
recrd[y]=recrd[y+1];
recrd[y+1]=temp;
}
fstream temp1;
temp1.open(filename,ios::out);
//Open the file to be packed into
for(t=1;t<k-1;t++) //Pack the sorted records onto the file
temp1<<recrd[t].name<<"|"<<recrd[t].usn<<"\n";
f1.close();
- 40 -
f2.close();
temp1.close();
return;
}
void kwaymerge()
{
char filename[7][20]={"ll.txt","22.txt","33.txt","44.txt","lll.txt","222.txt","llll.txt"};
int i;
int k;
k=8;
for(i=0;i<8;i+=2) //Merge and sort the 8 original files onto
{ //the four files indicated {ll.txt,22.txt....}
merge_file(fname[i],fname[i+1],filename[k++]);
}
k=4;
for(i=0;i<4;i+=2) //Merge and sort the four files onto lll.txt and 222.txt
{
merge_file(filename[i],filename[i+1],filename[k++]);
}
//Merge and sort the two files onto the llll.txt file
merge_file(filename[4],filename[5],filename[6]);
return;
}
int main()
{
int i;
cout<<"Enter the no. of records : ";
cin>>no;
cout<<"\nEnter the details : \n";
for(i=0;i<8;i++) //Create 8 files to store the split data
file[i].open(fname[i],ios::out);
for(i=0;i<8;i++) //Split and pack data onto the files
{
cout<<"Name :"; cin>>rec[i].name;
cout<<"USN : ";cin>>rec[i].usn;
file[i%8]<<rec[i].name<<'|'<<rec[i].usn<<"\n";
}
for(i=0;i<8;i++)
file[i].close();
kwaymerge(); //Merge
fstream result;
result.open("llll.txt",ios::in);
cout<<"\nSorted Records : \n";
char name[20],usn[20];
for(i=0;i<8;i++) //Unpack the sorted records and dispL
{
result.getline(name,20,'|');
result.getline(usn,20,'\n');
cout<<"\nName : "<<name<<"\nUSN : "<<usn<<"\n";
}
return 0;
- 41 -
9. Write a C++ program to implement B-Tree for a given set of integers and its operations insert ( )
and search ( ). Display the tree.
// Program which maintains a B-tree of order 5.
- 42 -
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
const int MAX = 4 ;
const int MIN = 2 ;
struct btnode
{
int count;
int value[MAX + 1];
btnode *child[MAX + 1];
};
class btree
{
private : btnode *root ;
public : btree( ) ;
void insert(int val) ;
int setval(int val, btnode *n, int *p, btnode **c);
static btnode *search (int val, btnode *root, int *pos);
static int searchnode(int val, btnode *n, int *pos);
void iskeypresent( int val) ;
void fillnode( int val, btnode *c, btnode *n, int k );
void split( int val, btnode *c, btnode *n, int k, int *y, btnode **newnode );
void clear( btnode *root, int k );
void copysucc( btnode *root, int i );
void merge( int k ) ;
void show( );
static void display( btnode *root );
static void deltree( btnode *root );
~btree( );
};
// Initializes data member
btree :: btree( )
{
root=NULL;
}
// searches a key and checks whether it is present or not
void btree :: iskeypresent(int val)
{
int i ;
if( searchnode ( val, root, &i ) )
cout<< "Key found "<<endl;
else
cout<< "Key Not found "<<endl;
}
- 43 -
- 44 -
- 45 -
{
int i, mid ;
if ( k <= MIN )
mid = MIN ;
else
mid = MIN + 1 ;
*newnode = new btnode ;
for ( i = mid + 1 ; i <= MAX ; i++ )
{
( *newnode ) -> value[i - mid] = n -> value[i] ;
( *newnode ) -> child[i - mid] = n -> child[i] ;
}
( *newnode ) -> count = MAX - mid ;
n -> count = mid ;
if ( k <= MIN )
fillnode ( val, c, n, k ) ;
else
fillnode ( val, c, *newnode, k - mid ) ;
*y = n -> value[n -> count] ;
( *newnode ) -> child[0] = n -> child[n -> count] ;
n -> count-- ;
}
// removes the value from the node and adjusts the values
void btree :: clear ( btnode *root, int k )
{
int i ;
for ( i = k + 1 ; i <= root -> count ; i++ )
{
root -> value[i - 1] = root -> value[i] ;
root -> child[i - 1] = root -> child[i] ;
}
root -> count-- ;
}
// copies the successor of the value that is to be deleted
void btree :: copysucc ( btnode *root, int i )
{
btnode *temp = root -> child[i] ;
while ( temp -> child[0] )
temp = temp -> child[0] ;
root -> value[i] = temp -> value[1] ;
}
// merges two nodes
void btree :: merge ( int k )
{
btnode *temp1, *temp2 ;
temp1 = root -> child[k] ;
temp2 = root -> child[k - 1] ;
temp2 -> count++ ;
temp2 -> value[temp2 -> count] = root -> value[k] ;
- 46 -
- 47 -
btree :: ~btree( )
{
deltree ( root ) ;
}
void main( )
{
btree b ;
clrscr();
int num;
int choice=0;
while (choice!=4)
{
cout<<endl<<"l - Insert"<<endl;
cout<<"2 - Search"<<endl;
cout<<"3 - Display"<<endl;
cout<<"4 - Exit"<<endl;
cout<<"Enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 1: cout<<"\nEnter the elements to be inseretd into b tree \n";
cin>>num;
b.insert ( num ) ;
break;
case 2: cout<<"\nEnter the elements to be searched in b tree \n";
cin>>num;
b.iskeypresent ( num ) ;
break;
case 3: b.show( ) ;
break;
case 4: exit(0);
default: cout<<"\nInvalid Option \n";
}
}
}
10. Write a C++ program to implement B+ tree for a given set of integers and its operations insert ( ),
and search ( ). Display the tree.
#include<iostream.h>
- 48 -
#include<math.h>
#include<conio.h>
struct node
{
int ele[4];
int child[4];
node *next;
};
class bptree
{
public:
node *tree[20][20];
int count[20];
int leaf;
int path[20];
node *head;
bptree();
node* create_node();
void insert(int);
void main_search(int);
void display_tree();
void insert_node(node*,int);
void search(int);
int search_node(node*,int);
int nodefull(node*);
void split(node*);
};
bptree::bptree()
{
leaf=-1;
for(int i=0;i<10;i++)
{
count[i]=-1;
path[i]=-1;
}
}
node* bptree::create_node()
{
node *n;
n= new node;
for(int i=0;i<4;i++)
{
n->ele[i]=-1;
n->child[i]=-1;
}
n->next=NULL;
return n;
}
- 49 -
- 50 -
flag=1;
break;
}
cout<<"\n The Path traversed is:";
for(i=0;path[i]!=-1;i++)
cout<<path[i]<<"->";
if(flag)
cout<<"\nElement Found";
else cout<<"\nNot Found";
}
void bptree::display_tree()
{
int i,j,k;
for(i=0;i<=leaf;i++)
{
cout<<"\n\n Level-------------"<<i<<"\n";
for(j=0;j<=count[i];j++)
{
if(i!=leaf)
k=1;
else k=0;
//print first element only at leaf level
for(;tree[i][j]->ele[k] != -1;k++)
cout<<""<<tree[i][j]->ele[k];
cout<<"\t";
}
}
}
void bptree::search(int key)
{
int i,j,temp;
path[0]=0; //always start the path from root
if(leaf)
{
j=0;
for(i=0;i<leaf;i++)
{
temp=search_node(tree[i][j],key);
path[i+1]=temp;
j=temp;
}
}
}
int bptree::search_node(node *node1,int key)
{
for(int i=0;i<4;i++)
{
if(key < node1->ele[i])
- 51 -
return node1->child[i];
if((key >= node1->ele[i]) && (key < node1->ele[i+1]))
return node1->child[i];
else if(node1->ele[i+1] == -1)
return node1->child[i];
}
}
int bptree::nodefull(node *node1)
{
if(node1->ele[3]!=-1)return 1;
else return 0;
}
void bptree::insert_node(node *node1,int key)
{
int flag=0,count=-1,i,j,x,y,l;
node *newnode, *parent;
for(i=0;i<4;i++) if(node1->ele[i]!=-1) ++count;
i=0;
while(!flag && node1->ele[i] != -1)
{
if(node1->ele[i] > key)
{
flag=1;
for(int j=count; j>=i; j--)
node1->ele[j+1]=node1->ele[j];
node1->ele[i]=key;
}
i++;
}
if(!flag)
node1->ele[count+1]=key;
if(node1->ele[0]==key)
{
for(i=leaf-1;i>=0;i--)
{
x=path[i+1];
if(tree[i][path[i]]->ele[x]>key)
tree[i][path[i]]->ele[x]=key;
else
insert_node(tree[i][x],key);
}
}
for(i=0;i<=count+1;i++)
cout<<"\t\t"<<node1->ele[i];
}
void bptree::split(node *oldnode)
{
- 52 -
- 53 -
n2=tree[i][count[i]];
newnode->ele[2]=n2->ele[0];
newnode->child[2]=count[i];
}
}
}
int main()
{
bptree bt;
int choice,key;
clrscr();
while(1)
{
cout<<"\n\nMain Menu\n-----------------------\n 1.Insert\n 2.Search\n 3.DisplayTree\n 4.Exit\n\n Enter your
choice:";
cin>>choice;
switch(choice)
{
case 1:cout<<"\n Enter the element:";
cin>>key;
bt.insert(key);
break;
case 2:cout<<"Enter the key:";
cin>>key;
bt.main_search(key);
break;
case 3:bt.display_tree();
break;
case 4:return 0;
default:cout<<"\nEnter valid choice";
}
}
}
11. Write a C++ program to store and retrieve student data from file using hashing. Use any collision
resolution technique
#include<stdio.h>
- 54 -
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<fstream.h>
#include<iostream.h>
//Record specification
class node
{
public: char name[15],usn[15];
node *link;
};
node *h[29] //Array of record pointers equal to size of hash keys - 29
void insert()
{
char name[15], usn[15], buffer[50];
istream in;
ofstream out;
out.open("student.txt",ios::app); //opening student.txt in append mode
if(!out)
{
cout<<"\nUnable to open the file in append mode";
getch();
return;
}
cout<<"\nEnter the name = "; cin>>name;
cout<<"\n Enter the usn = "; cin>>usn;
strcpy(buffer,name); //Packing the record onto the file using '|' as a delimiter
strcat(buffer,"|");
strcat(buffer,usn);
strcat(buffer,"\n"); // \n delimiter for the record
out<<buffer;
// appending the packed record onto the file
out.close();
}
//Insert record into the hash table
void hash_insert(char name1[], char usn1[], int hash_key)
{
node *p,*prev,*curr;
p = new node; //dynamically allocate space using 'new'
strcpy(p->name,name1);
strcpy(p->usn,usn1);
p->link=NULL;
prev=NULL;
curr=h[hash_key];
if(curr==NULL)
//getting the hash pointer location Case: No collision
{
- 55 -
h[hash_key]=p;
return;
}
while(curr!=NULL)
{
prev=curr;
curr=curr->link;
}
prev->link=p;
}
void retrive()
{
fstream in;
char name[15],usn[15];
int j,count;
node *curr;
in.open("student.txt",ios::in); // open the record file in input mode
if(!in)
{
cout<<"\n Unable to open the file in input mode";
getch();
exit(0);
}
while(!in.eof())
{
in.getline(name,15,'|');
//unpacking the record
in.getline(usn,15,'\n');
count=0;
for(j=0; j<strlen(usn); j++) //Calculate sum of ascii values of USN
{
count=count+usn[j];
}
count=count%29;
//Hash Key = ASCII count% 29
hash_insert(name,usn,count);
}
cout<<"\n Enter the usn = "; cin>>usn;
count=0;
for(j=0; j<strlen(usn); j++)
// Calculating Hash Key
count=count+usn[j];
count=count%29;
curr=h[count];
if(curr == NULL)
{
cout<<"\nRecord not found";
getch();
return;
}
do
{
if(strcmp(curr->usn,usn)==0)
//When the record is found, retrieve
{
- 56 -
12. Write a C++ program to reclaim the free space resulting from the deletion of records using linked
lists.
#include<stdio.h>
- 57 -
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream.h>
#include<fstream.h>
#include<new.h>
class node
{
public: char name[20];
char usn[20];
node *link;
};
node *first=NULL;
void writeFile()
{
node *p;
char buffer[100];
fstream out;
out.open("student.txt", ios::out);
if(!out)
{
cout<<"\n Unable to open the file student.txt in out mode";
getch();
exit(0);
}
p=first;
while(p!=NULL)
{
strcpy(buffer,p->name);
strcat(buffer,"|");
strcat(buffer,p->usn);
strcat(buffer,"\n");
out<<buffer;
p=p->link;
}
}
void display()
{
node *p;
if(first==NULL)
{
cout<<"\nList is empty";
return;
}
p=first;
while(p!=NULL)
{
cout<<"|"<<p->name<<" "<<p->usn<<"|"<<"->";
p=p->link;
- 58 -
}
}
void Insert()
//Insert the record at the rear end
{
char name[20],usn[15];
node *p,*q;
cout<<"\n Enter name = "; cin>>name;
cout<<"\nEnter usn = "; cin>>usn;
p=new node;
strcpy(p->name,name);
strcpy(p->usn,usn);
p->link=NULL;
if(first==NULL)
{
first=p;
writeFile();
display(); //display the record on the screen
return;
}
for(q=first; q->link!=NULL; q=q->link)
{
;
}
q->link=p;
writeFile();
//writing the record to the file
display();
//display the records to the screen.
}
void Delete()
{
char usn[15];
node *curr,*prev,*del;
if(first==NULL)
{
printf("\nThe list is empty. Deletion is not possible");
return;
}
cout<<"\nEnter the usn to be deleted = "; cin>>usn;
if(strcmp(first->usn,usn)==0)
{
cout<<"\n Record deleted";
del = first;
delete del;
first=first->link;
writeFile();
return;
}
prev=NULL;
curr=first;
while( ( strcmp(curr->usn,usn) != 0 ) && curr!=NULL)
{
- 59 -
prev=curr;
curr=curr->link;
}
if(curr == NULL)
{
cout<<"\nThe student with usn "<<usn<<" is not present";
return;
}
prev->link = curr->link;
writeFile();
display();
//display the records to the screen
}
void main()
{
int ch;
clrscr();
for(;;)
{
cout<<"\n 1-Insert_rear \n 2-Delete_id \n 3-Exit \n Enter choice";
cin>>ch;
switch(ch)
{
case 1: Insert();
break;
case 2: Delete();
break;
case 3: exit(0);
default: cout<<"\n Invalid option";
break;
}
}
}
Appendix-I
A file structure is a combination of representations for the data in files and of operations for accessing the
data.
- 60 -
- 61 -
Where offset indicates the number of bytes the file pointer is moved from the location specified by position.
Position will take the following constraints defined in the ios class
ios::beg offset count from the beginning of the stream
ios::cur offset counted from the current position of the stream
ios::end - offset counted from the end of the stream.
Appendix-II
I/O redirection and pipes in UNIX
Usually, the output is displayed on the terminal but in some cases if the input of a file listing the program is
needed as i/p to another program or if the o/p is to be stored in a file than displaying on terminal or if i/p to
a terminal or if i/p to a program is taken from a file, than a common method called I/O redirection and pipes
are used.
Here Operating system switches from standard I/O and regular file I/O. there are three streams in UNIX
which are given below
Standard input-the file representing input, usually connected to keyboard
< file (redirects standard input to file)
Standard output-the file representing put, usually connected to
> file (redirects standard output to file)
Standard error- the file representing error messages that emanate from command or shell, usually
connected to display.
UNIX provides tools for redirecting the output of one program as input to other program. the mete character
is used for this purpose.
Appendix-III
- 62 -
Appendix-IV
Unix file system commands
Unix provides many commands for manipulating files.
Cat filenames:- Print the contents of the named text files.
Tail filename:- Prints the last ten lines of the text file.
Cp file1 file2:- Copy file1 to file2.
Mv file1 file2:- Move(rename) file1 to file2.
Rm filenames:- Remove(delete) the named files.
Chmod mode filename:- Changes the protection mode on the named file.
Ls:- List the contents of the directory.
Mkdir name:- Create a directory with the given name.
Rmdis name:- Remove the named directory
Appendix-V
- 63 -
Field Structures
A filed is the smallest logical meaningful unit of information in a file
There are many ways of adding structures to files to maintain the identity of the fields. Four of the most
common methods follow:1.
2.
3.
4.
5.
Fig (a)
Ames
Mary
123 Maple
Stillwater
OK74075
Mason
Alan
90 Eastgate
Ada
OK74820
<----10------><---10-----><--------10-----------><----------10---------><-------10------>
fig (b)
04Ames04Mary09123 Maple10Stillwater020OK74075
05Mason04Alan1190 Eastgate03Ada02OK74820
fig (c)
Ames | Mary| 123 Maple | Stillwater
| OK | 74075
Mason | Alan | 90 Eastgate | Ada | OK | 74820
fig (d)
last=Ames | first=Mary | address=123 Maple | city=Stillwater | state=OK | zip=74075
last=Mason | first=Alan | address=90 eastgate | city=Ada | state=OK | zip=74820
Reading a Stream of Fields
istream & opreator >> (istream & stream, person &p)
{ //read delemeted fields from file
char delim;
stream.getline(p.lastname,30,'1');
if (strlen(p.lastname==0)) return stream;
stream.getline(p.lastname,30,'1');
stream.getline(p.address,30,'1');
stream.getline(p.city,30,'1');
stream.getline(p.state,15,'1');
stream.getline(p.zipcode,10,'1');
}
The output of above program looks like this for delimited-field version
LastName
'Ames'
FisrtName
'Mary'
Address
'123 Maple'
Appendix-VI
- 64 -
Record Structure
A Record can be defined as a set of fields that belong together when the file is viewed in terms of a
higher level of organization. Like the notion of a filed, a record is another conceptual tool. Records do not
necessarily exist in the logical notion included in the file's structure.
A record in a file represents a structured data objects. The C++ programming examples are focused
on adding methods to classes to support using files to preserve the state of objects.
Following are some of the most often used methods for organizing the records of a file:
Method 1:- Make Records a Predictable Number of Bytes(Fixed-Length Records)
A fixed-length record file is one in which each record contains the same number of bytes. Fixed
length records are frequently used as containers to hold variable number of variable fields. It is also possible
to mix fixed & variable length field within a record.
Fig (a) Fixed-Length records with fixed-length fields
Ames
Mason
Mary
Alan
123 Maple
90 Eastgate
Stillwater
Ada
OK74075
OK74820
fig (e)
- 65 -
Data Files:
Ames:Mary:123:Maple:stillwater:OK:74075:Mason:Alan:90: Eastgate:Ada:OK74820
Index file:
00
40
- 66 -
Given our file structure of variable -length records proceeded by record length fields, it is easy to
write a program that reads through file, record by record displaying the fields from each of the records on
the screen .The program must read the length of a record, move the characters of the record into a buffer,
then break the record into fields .The code to read and break up the record is included into function read
variable person in the following program fragment,
In the read variable person (istream and stream, person and p)
{
//read a variable sized record from stream and store it in p//
short length;
stream read(&length,size of(length));
char *buffer=new char[length +1];
strem.read(buffer,length);
buffer[length]=0;
istrem strbuff(buffer);
strbuff>>p;
return 1;
}
- 67 -