Sunteți pe pagina 1din 87

FILES AND INFORMATION STRUCTURES LAB.

ASSIGNMENT NO. 1
Program on Text File handling. Write a C++ program to create text file with each record having the structure shown below. Student USN: 10 characters. Student Name: 15 characters. Student Address: 10characters. Student Course: 05 characters. Student Branch: 05 characters. Write necessary methods to Read, Write & Display the records.

Program: #include<iostream.h>
Page 1

FILES AND INFORMATION STRUCTURES LAB. #include<conio.h> #include<fstream.h> class student { public:char usn[10]; char name[15]; char add[10]; char course[5]; char branch[5]; student() { usn[0]=name[0]=add[0]=course[0]=branch[0]=0; } void read() { cout<<"Enter the usn : "; cin>>usn; cout<<"Enter the name : "; cin>>name; cout<<"Enter the address : "; cin>>add; cout<<"Enter the course : "; cin>>course; cout<<"Enter the branch : "; cin>>branch; } void display() { cout<<usn<<"\t"<<name<<"\t"; cout<<add<<"\t"<<course<<"\t"<<branch<<"\n"; } }; ostream& operator<<(ostream& file,student &s) { file<<s.usn<<"\n"; file<<s.name<<"\n"; file<<s.add<<"\n"; file<<s.course<<"\n"; file<<s.branch<<"\n"; return file; } istream& operator>>(istream& file,student &s) { file.getline(s.usn,10,'\n'); file.getline(s.name,15,'\n'); file.getline(s.add,5,'\n'); file.getline(s.course,10,'\n'); file.getline(s.branch,5,'\n'); return file; } void main()
Page 2

FILES AND INFORMATION STRUCTURES LAB. { student s; int i=0; int flag=1,choice; char filename[10],ch='y'; fstream file; clrscr(); while(flag) { cout<<"\nMENU\n1.Create a file\n2.Display the records\n"; cout<<"3.Exit\nEnter your choice : "; cin>>choice; switch(choice) { case 1: cout<<"\nEnter the name of the file to be created : "; cin>>filename; file.open(filename,ios::out); cout<<"\nAdd records to the file... :\n"; while(ch=='y') { i++; cout<<"\nEnter the record of Student "<<i<<" :\n"; s.read(); file<<s; cout<<"\nEnter more records....(y/n) :"; cin>>ch; } file.close(); break; case 2: file.open(filename,ios::in); cout<<"\n\n"; cout<<"The contents of file are....\n"; cout<<"________________________________________\n"; cout<<"Usn\tName\tAddress\tCourse\tBranch\n"; cout<<"________________________________________\n"; while(!file.eof()) { file>>s; s.display(); } file.close(); break; case 3: flag=0;break; default: cout<<"Invalid choice"; } } getch();} Output: MENU 1.Create a file 2.Display the records 3.Exit
Page 3

FILES AND INFORMATION STRUCTURES LAB. Enter your choice : 1 Enter the name of the file to be created : A.DAT Add records to the file... : Enter the record of Student 1 : Enter the usn : 2BA07IS210 Enter the name : ARUN Enter the address : BAGALKOT Enter the course : BE Enter the branch : ISE Enter more records....(y/n) :y Enter the record of Student 2 : Enter the usn : 2BA07IS091 Enter the name : JAY Enter the address : BAGALKOT Enter the course : MCA Enter the branch : ISE Enter more records....(y/n) :n MENU 1.Create a file 2.Display the records 3.Exit Enter your choice : 2 The contents of file are.... _________________________________________________ Usn Name Address Course Branch _________________________________________________ 2BA07IS210 ARUN BAGALKOT BE ISE 2BA07IS091 JAY BAGALKOT MCA ISE MENU 1.Create a file 2.Display the records 3.Exit Enter your choice :3

Page 4

FILES AND INFORMATION STRUCTURES LAB.

ASSIGNMENT NO. 2
Program on Binary File handling. Write a C++ program to create Binary file with each record having the structure shown below. Employee ID: Alphanumeric. Employee Phone Number: Positive integer. Employee Basic Salary: Positive integer. Write necessary methods to Read, Write & Display the records.

Program: #include<iostream.h> #include<conio.h> #include<fstream.h> class person { public: char id[10]; int ph;
Page 5

FILES AND INFORMATION STRUCTURES LAB. int sal; person() { id[0]=0; ph=0; sal=0; } void read() { cout<<"\n Enter the id : "; cin>>id; cout<<"Enter the phone no : "; cin>>ph; cout<<"Enter the salary : "; cin>>sal; } void display() { cout<<id<<"\t"<<ph<<"\t"<<sal<<endl; } }; ostream& operator<<(ostream& file,person &p) { file.write((char*)& p,sizeof(p)); return file; } istream& operator>>(istream& file,person &p) { file.read((char*)& p,sizeof(p)); return file; } void main() { person p; int i=0; int flag=1,choice; char filename[10],ch='y'; fstream file; clrscr(); while(flag) { cout<<"\nMENU\n1.create file record\n2.Display the records\n"; cout<<"3.exit\nEnter your choice : "; cin>>choice; switch(choice) { case 1: cout<<"Enter the name of the file to be created : "; cin>>filename; file.open(filename,ios::out|ios::binary);
Page 6

FILES AND INFORMATION STRUCTURES LAB. cout<<"\nAdd records to the file..... :\n"; while(ch=='y') { i++; cout<<"\nEnter record of Person "<<i<<" :\t"; p.read(); file<<p; cout<<"\nAdd more records......(y/n) : "; cin>>ch; } file.close(); break; case 2: file.open(filename,ios::in|ios::binary); cout<<"\n\n"; cout<<"The contents of file are....\n"; cout<<"_______________________\n"; cout<<"ID\tPh.no.\tSalary\n"; cout<<"_______________________\n"; while(!file.eof()) { file>>p; if(!file.fail()) p.display(); } file.close(); break; case 3: flag=0;break; default: cout<<"\nInvalid choice\n"; } } getch(); }

Output: MENU 1.Create file record 2.Display the records 3.exit Enter your choice : 1 Enter the name of the file to be created : ASIGN-2.DAT Add records to the file..... : Enter record of Person 1 : Enter the id : 12
Page 7

FILES AND INFORMATION STRUCTURES LAB. Enter the phone no : 123456 Enter the salary : 2500 Add more records......(y/n) : y Enter record of Person 2 : Enter the id : 34 Enter the phone no : 567890 Enter the salary : 3000 Add more records......(y/n) : y Enter record of Person 3 : Enter the id : 56 Enter the phone no : 456789 Enter the salary : 4000 Add more records......(y/n) : n MENU 1.Create file record 2.Display the records 3.exit Enter your choice : 2 The contents of file are.... _____________________________ ID. Ph.no. Salary _____________________________ 12 123456 2500 34 567890 3000 56 456789 4000

Page 8

FILES AND INFORMATION STRUCTURES LAB.

ASSIGNMENT NO. 3
Program on file handling using Delimited field, variable length (using length indicated) record structure. Write a C++ program to create file with delimited fields, variable length record having fields shown below Customer ID: 10 characters. Customer Name: 10 characters. Customer Address: 25 characters. Write necessary methods to Read, Write & Display the records.

Program: #include<iostream.h> #include<conio.h> #include<fstream.h> #include<process.h> #include<stdlib.h> #include<string.h> #include<stdio.h> class delimbuffer { public: delimbuffer(char delim='|',int maxbytes=100); void clear(); int pack(const char *,int size=-1); int write(ostream &)const; int read (istream &);
Page 9

FILES AND INFORMATION STRUCTURES LAB. int unpack(char *); private: char *buffer,delims,delimstr[2]; int buffersize,nextByte,nextfield,MaxBytes; }; delimbuffer::delimbuffer(char delim,int maxbytes) { delims=delim; delimstr[1]=delims; delimstr[2]=0; if(maxbytes<0) maxbytes=0; MaxBytes=maxbytes; buffer=new char[MaxBytes]; buffersize=0; } void delimbuffer::clear() { nextByte=0; buffersize=0; buffer[0]=0; } int delimbuffer::pack(const char *str,int size) { short len; if(size>=0) len=size; else len=strlen(str); if(len>strlen(str)) return 0; int start=nextByte; nextByte+=len+1; if(nextByte>MaxBytes) return 0; memcpy(&buffer[start],str,len); buffer[start+len]=delims; buffersize=nextByte; return 1; } int delimbuffer::unpack(char *str) { int len=-1; int start=nextByte; for(int i=start;i<buffersize;i++) { if(buffer[i]==delims) { len=i-start;
Page 10

FILES AND INFORMATION STRUCTURES LAB. break; } } if(len==-1) return 0; nextByte+=len+1; if(nextByte>buffersize) return 0; strncpy(str,&buffer[start],len); str[len]=0; return -1; } int delimbuffer::write(ostream& file)const { file.write((char*)&buffersize,sizeof(buffersize)); file.write(buffer,buffersize); if(file.fail()) return -1; return 1; } int delimbuffer::read(istream& file) { clear(); file.read((char*)&buffersize,sizeof(buffersize)); if(file.fail()) return 0; if(buffersize>MaxBytes) return 0; file.read(buffer,buffersize); if(file.fail()) return -1; return 1; } class customer { public: char id[10]; char name[10]; char add[25]; customer() { id[0]=name[0]=add[0]=0; } void read() { cout<<"\nEnter the id : "; cin>>id; cout<<"Enter the name : "; cin>>name; cout<<"Enter the address : "; cin>>add; }
Page 11

FILES AND INFORMATION STRUCTURES LAB. void display() { cout<<id<<"\t"<<name<<"\t"<<add<<"\n"; } int pack(delimbuffer& buffer)const; int unpack(delimbuffer&); }; int customer::pack(delimbuffer &buffer)const { int res; buffer.clear(); res=buffer.pack(id); res=res&& buffer.pack(name); res=res&& buffer.pack(add); return res; } int customer::unpack(delimbuffer &dbuffer) { int res; res=dbuffer.unpack(id); res=res&& dbuffer.unpack(name); res=res&& dbuffer.unpack(add); return res; } void main() { fstream file; customer c; delimbuffer dbuffer; char filename[10],ch='y'; int choice,flag=1,i=0; clrscr(); cout<<"\nEnter the file name to be created : "; cin>>filename; while(flag) { cout<<"\nMENU\n1.Add records to the file\n2.Display the contents of the file\n"; cout<<"3.Exit\nEnter your choice : "; cin>>choice; switch(choice) { case 1: file.open(filename,ios::out); while(ch=='y') { i++; cout<<"Enter the information of customer "<<i; c.read(); c.pack(dbuffer); dbuffer.write(file);
Page 12

FILES AND INFORMATION STRUCTURES LAB. cout<<"\nAdd more records.....(y/n) : "; cin>>ch; } file.close(); break; case 2: file.open(filename,ios::in); file.seekg(0,ios::beg); dbuffer.read(file); cout<<"The contents of the file are...\n"; cout<<"__________________________\n"; cout<<"ID\tName\tAddress\n"; cout<<"__________________________\n"; while(!file.eof()) { c.unpack(dbuffer); c.display(); dbuffer.read(file); } file.close(); break; case 3: flag=0;break; default: cout<<"\nInvalid choice\n"; } } getch(); } Output: Enter the file name to be created : ASIGN-3.DAT MENU 1.Add records to the file 2.Display the contents of the file 3.Exit Enter your choice : 1 Enter the information of customer 1 Enter the id : 12 Enter the name : ARUN Enter the address : BAGALKOT Add more records.....(y/n) : y Enter the information of customer 2 Enter the id : 34 Enter the name : VINAY Enter the address : HUBLI Add more records.....(y/n) : y Enter the information of customer 3
Page 13

FILES AND INFORMATION STRUCTURES LAB. Enter the id : 56 Enter the name : VIJAY Enter the address : GADAG MENU 1.Add records to the file 2.Display the contents of the file 3.Exit Enter your choice : 2 The contents of the file are... __________________________ ID Name Address __________________________ 12 ARUN BAGALKOT 34 VINAY HUBLI 56 VIJAY GADAG MENU 1.Add records to the file 2.Display the contents of the file 3.Exit Enter your choice :3

ASSIGNMENT NO. 4
Program on file handling using Delimited record structure with each field begins with length indicator. Write a C++ program to create file with delimited record structure where each fileld shown below begins with length indicator. Teacher ID: Non-zero integers. First Name: 10 characters. Last Name: 10 characters. Details: 15 characters.
Page 14

FILES AND INFORMATION STRUCTURES LAB. Write necessary methods to Read, Write & Display the records.

Program: #include<iostream.h> #include<conio.h> #include<string.h> #include<process.h> #include<stdio.h> #include<fstream.h> class lenfieldbuffer { public: lenfieldbuffer(int maxbytes=100); void clear(); int pack(const char*,int size); int unpack(char*); int write(ostream&)const; int read(istream&); char *buffer; int buffersize,nextbyte,Maxbyte; }; lenfieldbuffer::lenfieldbuffer(int maxbyte) { if(maxbyte<0) maxbyte=0; Maxbyte=maxbyte; buffer=new char(Maxbyte); buffersize=0; }
Page 15

FILES AND INFORMATION STRUCTURES LAB. void lenfieldbuffer::clear() { nextbyte=0; } int lenfieldbuffer::pack(const char* str,int size=-1) { short len; if(size>0) len=size; else len=strlen(str); if(len>strlen(str)) return 0; int start=nextbyte; nextbyte+=len+sizeof(len); if(nextbyte>Maxbyte) return 0; memcpy(&buffer[start],&len,sizeof(len)); strncpy(&buffer[start+sizeof(len)],str,len); buffersize=nextbyte; return 1; } int lenfieldbuffer::unpack(char* str) { short len; if(nextbyte>=buffersize) return 0; int start=nextbyte; memcpy(&len,&buffer[start],sizeof(len)); nextbyte+=len+sizeof(len); if(nextbyte>buffersize) return 0; strncpy(str,&buffer[start+sizeof(len)],len); str[len]=0; return 1; } int lenfieldbuffer::write(ostream& file)const { file.write((char*)&buffersize,sizeof(buffersize)); file.write(buffer,buffersize); if(file.fail()) return -1; return 1; } int lenfieldbuffer::read(istream& file) { clear(); file.read((char*)&buffersize,sizeof(buffersize));
Page 16

FILES AND INFORMATION STRUCTURES LAB. if(file.fail()) return 0; if(buffersize>Maxbyte) return 0; file.read(buffer,buffersize); if(file.fail()) return -1; return 1; } class teacher { public: int id; char fname[10],lname[10],details[15]; teacher() { id=fname[0]=lname[0]=details[0]=0; } void read() { cout<<"\nEnter the id : "; cin>>id; cout<<"Enter the first name : "; cin>>fname; cout<<"Enter the last name : "; cin>>lname; cout<<"Enter the details : "; cin>>details; } void display() { cout<<id<<"\t"<<fname<<"\t"<<lname<<"\t"<<details<<"\n"; } int pack(lenfieldbuffer& buffer); int unpack(lenfieldbuffer&); }; int teacher::pack(lenfieldbuffer &buffer) { int res; buffer.clear(); res=buffer.pack((char*)&id); res=res&& buffer.pack(fname); res=res&& buffer.pack(lname); res=res&& buffer.pack(details); return res; } int teacher::unpack(lenfieldbuffer &dbuffer) { int res;
Page 17

FILES AND INFORMATION STRUCTURES LAB. res=dbuffer.unpack((char*)&id); res=res&& dbuffer.unpack(fname); res=res&& dbuffer.unpack(lname); res=res&& dbuffer.unpack(details); return res; } void main() { fstream file; teacher c; lenfieldbuffer dbuffer; char filename[10],ch='y'; int choice,flag=1,i=0; clrscr(); cout<<"\nEnter the file name to be created : "; cin>>filename; while(flag) { cout<<"\n\tMENU\n1.Add records to the file\n2.Display the contents of the file\n"; cout<<"3.Exit\nEnter your choice : "; cin>>choice; switch(choice) { case 1: file.open(filename,ios::out); while(ch=='y') { i++; cout<<"\nEnter the information of teacher "<<i<<" :"; c.read(); c.pack(dbuffer); dbuffer.write(file); cout<<"\nAdd more records.....(y/n) : "; cin>>ch; } file.close(); break; case 2: file.open(filename,ios::in); file.seekg(0,ios::beg); dbuffer.read(file); cout<<"The contents of the file are... :\n"; cout<<"________________________________\n"; cout<<"ID\tName\tSurname\tDetails\n"; cout<<"________________________________\n"; while(!file.eof()) { c.unpack(dbuffer); c.display(); dbuffer.read(file); } file.close(); cout<<"________________________________\n";
Page 18

FILES AND INFORMATION STRUCTURES LAB. break; case 3:flag=0;break; default:cout<<"\nInvalid choice\n"; } } getch(); }

Output: Enter the file name to be created : ASIGN-3.DAT MENU 1.Add records to the file 2.Display the contents of the file 3.Exit Enter your choice : 1 Enter the information of teacher 1 : Enter the id : 12 Enter the first name : ARUN Enter the last name : KUMAR Enter the details : IS-DEPT Add more records.....(y/n) : y Enter the information of teacher 2 : Enter the id : 34 Enter the first name : VIKAS Enter the last name : PATEL Enter the details : CS-DEPT Add more records.....(y/n) : y Enter the information of teacher 3 : Enter the id : 56 Enter the first name : VINAY Enter the last name : PATIL Enter the details : EC-DEPT MENU 1.Add records to the file 2.Display the contents of the file 3.Exit Enter your choice : 2
Page 19

FILES AND INFORMATION STRUCTURES LAB. The contents of the file are... : ________________________________ ID Name Surname Details ________________________________ 12 ARUN KUMAR IS-DEPT 34 VIKAS PATEL CS-DEPT 56 VINAY PATIL EC-DEPT ________________________________ MENU 1.Add records to the file 2.Display the contents of the file 3.Exit Enter your choice : 3

ASSIGNMENT NO. 5
Program on file handling using Fixed field, fixed record structure. Write a C++ program to create file with Fixed field, fixed record structure, having the field shown below. Subject Code: 10 characters. Subject Name: 15 characters. Faculty: 10 characters. Write necessary methods to Read, Write & Display the records.

Page 20

FILES AND INFORMATION STRUCTURES LAB.

\ Program: #include<iostream.h> #include<conio.h> #include<string.h> #include<process.h> #include<stdio.h> #include<fstream.h> #includec:\tc\bin\fixlenbu.h class fixedfieldbuffer:public fixedlenbuffer { public: fixedfieldbuffer(int maxfield=10,int recordsize=100); int init(int maxfield); int clear(); fixedfieldbuffer(int numfields,int *fieldsize); int init(int maxfield,int *fieldsize); int addfield(int fieldsize); int pack(const char* field,int size=-1); int unpack(char *field); int write(ostream& stream)const; int read(istream& stream); int *Fieldsize,Maxfield,Numfield,nextfield; }; static int sumfieldsize(int numfields,int *fieldsize) { int sum=0; for(int i=0;i<numfields;i++) sum+=fieldsize[i]; return sum; } fixedfieldbuffer::fixedfieldbuffer(int maxfields,int recordsize) :fixedlenbuffer(recordsize) { init (maxfields); } int fixedfieldbuffer::init(int maxfield) {
Page 21

FILES AND INFORMATION STRUCTURES LAB. clear(); if(maxfield<0) maxfield=0; Maxfield=maxfield; Fieldsize=new int[Maxfield]; buffersize=0; Numfield=0; return 1; } int fixedfieldbuffer::clear() { fixedlenbuffer::clear(); nextfield=0; return 1; } fixedfieldbuffer::fixedfieldbuffer(int numfields,int *fieldsize) :fixedlenbuffer(sumfieldsize(numfields,fieldsize)) { init(numfields,fieldsize); } int fixedfieldbuffer::init(int numfield,int *fieldsize) { initialize=1; init(numfield); for(int j=0;j<numfield;j++) addfield(fieldsize[j]); return 1; } int fixedfieldbuffer::addfield(int fieldsize) { initialize=1; if(Numfield==Maxfield) return 0; if(buffersize+fieldsize>Maxbyte) return 0; Fieldsize[Numfield]=fieldsize; Numfield++; buffersize+=fieldsize; return 1; } int fixedfieldbuffer::pack(const char* field,int size) { if(nextfield==Numfield||!packing) return -1; int start=nextbyte; int packsize=Fieldsize[nextfield]; if(size!=-1&&packsize!=size) return -1; memcpy(&buffer[start],field,packsize); nextbyte+=packsize;
Page 22

FILES AND INFORMATION STRUCTURES LAB. nextfield++; if(nextfield==Numfield) { packing=-1; nextfield=nextbyte=0; } return packsize; } int fixedfieldbuffer::unpack(char *field) { packing=0; if(nextfield==Numfield) return -1; int start=nextbyte; int packsize=Fieldsize[nextfield]; memcpy(field,&buffer[start],packsize); nextbyte+=packsize; nextfield++; if(nextfield==Numfield) clear(); return packsize; } int fixedfieldbuffer::write(ostream& stream)const { int res=fixedlenbuffer::write(stream); return res; } int fixedfieldbuffer::read(istream& stream) { int res=fixedlenbuffer::read(stream); return res; } class student { public: char scode[10]; char name[15]; char fac[10]; student() { scode[0]=name[0]=fac[0]=0; } void read() { cout<<"\nEnter the subject code : "; cin>>scode; cout<<"Enter the name : "; cin>>name; cout<<"Enter the faculty : "; cin>>fac;
Page 23

FILES AND INFORMATION STRUCTURES LAB. } void display() { cout<<scode<<"\t"<<name<<"\t"<<fac<<"\n"; } int pack(fixedfieldbuffer&); int unpack(fixedfieldbuffer&); }; int student::pack(fixedfieldbuffer& fbuff) { int res; fbuff.clear(); res=fbuff.pack(scode); res=res&&fbuff.pack(name); res=res&&fbuff.pack(fac); return res; } int student::unpack(fixedfieldbuffer& fbuff) { int res; res=fbuff.unpack(scode); res=res&&fbuff.unpack(name); res=res&&fbuff.unpack(fac); return res; } void main() { fstream file; student s; int static fs[]={10,15,10}; fixedfieldbuffer fbuff(3,fs); char filename[10],ch='y'; int choice,flag=1,i=0; clrscr(); cout<<"Enter the filename to be created : "; cin>>filename; while(flag) { cout<<"\nMENU\n1.Add records\n2.display\n"; cout<<"3.exit\nEnter your choice : "; cin>>choice; switch(choice) { case 1: file.open(filename,ios::out); while(ch=='y') { i++; cout<<"Enter the information of teacher "<<i<<" :"; s.read();
Page 24

FILES AND INFORMATION STRUCTURES LAB. s.pack(fbuff); fbuff.write(file); cout<<"\nAdd more records....(y/n): "; cin>>ch; } file.close(); break; case 2: file.open(filename,ios::in); file.seekg(0,ios::beg); cout<<"The contents of the file are.....\n"; cout<<"_____________________________\n"; cout<<"S.code\tName\tFaculty\n"; cout<<"_____________________________\n"; while(1) { fbuff.read(file); if(file.fail()) break; s.unpack(fbuff); s.display(); } file.close(); cout<<"_____________________________\n"; break; case 3: flag=0;break; default: cout<<"\nInvalid choice\n"; } } getch(); }

Page 25

FILES AND INFORMATION STRUCTURES LAB.

Output: Enter the filename to be created : ASIGN-5.DAT MENU 1.Add records 2.display 3.exit Enter your choice : 1 Enter the information of teacher 1 : Enter the subject code : UIS201C Enter the name : MATHEMATICS Enter the faculty : MRS.V.M.PATIL Add more records....(y/n): y Enter the information of teacher 2 : Enter the subject code : UIS202C Enter the name : PHYSICS Enter the faculty : MR.A.SHETTY Add more records....(y/n): n MENU 1.Add records 2.display 3.exit Enter your choice : 2 The contents of the file are..... ________________________________________________ S.code Name Faculty ________________________________________________ UIS201C MATHEMATICS MRS.V.M.PATIL
Page 26

FILES AND INFORMATION STRUCTURES LAB. UIS202C PHYSICS MR.A.SHETTY _______________________________________________ MENU 1.Add records 2.display 3.exit Enter your choice :3

ASSIGNMENT NO. 6
Program on Key sorting technique. Write a C++ program to create file with, each record having the structure shown below. Item number: Positive integer. Item Name: 10 characters. Item type: 10 characters. Item price: Float variable. Quantity: Integer variable. Write necessary methods to Read, Write & Display the records & methods to sort the records with Item number as the Key using Key Sorting Technique.

Page 27

FILES AND INFORMATION STRUCTURES LAB.

Program: #include<iostream.h> #include<conio.h> #include<fstream.h> #include<string.h> #include<process.h> #includec:\tc\bin\delimbuf.h static int i=0; class item { public: char nm[10],ty[10]; int pr; int no,qt; item(); void read(); void display(); int pack(delimbuffer &)const; int unpack(delimbuffer &); }; item::item() { nm[0]=0;ty[0]=0;pr=0;no=0;qt=0; } void item::read() { cout<<"Enter the item number: ";cin>>no; cout<<"Enter the item name: ";cin>>nm; cout<<"Enter the item type: ";cin>>ty; cout<<"Enter the item price ";cin>>pr; cout<<"Enter the quantity: ";cin>>qt; } void item::display() {
Page 28

FILES AND INFORMATION STRUCTURES LAB.

cout<<no<<"\t"<<nm<<"\t"<<ty<<"\t"<<pr<<"\t"<<qt<<endl; } int item::pack(delimbuffer &Buffer)const { int res; Buffer.clear(); res=Buffer.pack((char *)&no); res=res&&Buffer.pack(nm); res=res&&Buffer.pack(ty); res=res&&Buffer.pack((char *)&pr); res=res&&Buffer.pack((char *)&qt); return res; } int item::unpack(delimbuffer &Buffer) { int res; res=Buffer.unpack((char *)&no); res=res&&Buffer.unpack(nm); res=res&&Buffer.unpack(ty); res=res&&Buffer.unpack((char *)&pr); res=res&&Buffer.unpack((char *)&qt); return res; } class keytype { public: int key,recadr; }; void sort(keytype keynode[],int numkeys) { int l,k,temp1,temp2; for(l=1;l<numkeys;l++) { for(k=0;k<numkeys-l;k++) { if(keynode[k].key>keynode[k+1].key) { temp1=keynode[k].key; keynode[k].key=keynode[k+1].key; keynode[k+1].key=temp1; temp2=keynode[k].recadr; keynode[k].recadr=keynode[k+1].recadr; keynode[k+1].recadr=temp2; } } } } void main()
Page 29

FILES AND INFORMATION STRUCTURES LAB. { delimbuffer dbuffer; item c; fstream file,sortfp; keytype keynode[50]; char filename[10],filenm[10]; clrscr(); int ch,ra,j,n; cout<<"Enter the filename :"; cin>>filename; file.open(filename,ios::out); file.close(); while(1) { cout<<"\nMENU\n1.Insert new record\n2.Display unsorted file\n"; cout<<"3.Display sorted file\n4.exit\n"; cout<<"Enter the choice :"; cin>>ch; switch(ch) { case 1: file.open(filename,ios::app); file.seekp(0,ios::end); cout<<"Enter the details of customer "<<i+1<<":\n"; c.read(); keynode[i].key=c.no; c.pack(dbuffer); ra=dbuffer.write(file); keynode[i].recadr=ra; sort(keynode,i+1); i++; file.close(); break; case 2: file.open(filename,ios::in); cout<<"The contents of the unsorted file are....:\n"; cout<<"____________________________________________"; cout<<"\nIt.no\tIt.name\tType\tPrice\tQuantity\n"; cout<<"____________________________________________\n"; while(1) { dbuffer.read(file); if(file.fail()) break; c.unpack(dbuffer); c.display(); } cout<<"____________________________________________\n"; file.close(); break; case 3: file.open(filename,ios::in); sortfp.open("sort.dat",ios::out); for(j=0;j<i;j++) {
Page 30

FILES AND INFORMATION STRUCTURES LAB. int ra=keynode[j].recadr; file.seekg(ra,ios::beg); dbuffer.read(file); dbuffer.write(sortfp); } sortfp.close(); file.close(); sortfp.open("sort.dat",ios::in); cout<<"The contents of the sorted file are....:\n"; cout<<"____________________________________________"; cout<<"\nIt.no\tIt.name\tType\tPrice\tQuantity\n"; cout<<"____________________________________________\n"; while(1) { dbuffer.read(sortfp); if(sortfp.fail()) break; c.unpack(dbuffer); c.display(); } cout<<"____________________________________________\n"; sortfp.close(); break; case 4: exit(0); default: cout<<"\nInvalid choice\n"; } } }

Page 31

FILES AND INFORMATION STRUCTURES LAB.

Output: Enter the filename :ASIG-6.DAT MENU 1.Insert new record 2.Display unsorted file 3.Display sorted file 4.Exit Enter the choice :1 Enter the details of customer 1: Enter the item number: 23 Enter the item name: MONTEX Enter the item type: PENS Enter the item price 20 Enter the quantity: 50 MENU 1.Insert new record 2.Display unsorted file 3.Display sorted file 4.Exit Enter the choice :1 Enter the details of customer 2: Enter the item number: 11 Enter the item name: CELLO Enter the item type: PENCILS Enter the item price 10 Enter the quantity: 60 MENU 1.Insert new record 2.Display unsorted file 3.Display sorted file 4.Exit Enter the choice :2 The contents of the unsorted file are....: ____________________________________________ It.no It.name Type Price Quantity ____________________________________________ 23 MONTEX PENS 20 50 11 CELLO PENCILS 10 60 ____________________________________________ MENU
Page 32

FILES AND INFORMATION STRUCTURES LAB. 1.Insert new record 2.Display unsorted file 3.Display sorted file 4.Exit Enter the choice :3 The contents of the sorted file are....: ____________________________________________ It.no It.name Type Price Quantity ____________________________________________ 11 CELLO PENCILS 10 60 23 MONTEX PENS 20 50 ____________________________________________ MENU 1.Insert new record 2.Display unsorted file 3.Display sorted file 4.Exit Enter the choice :4

Page 33

FILES AND INFORMATION STRUCTURES LAB.

ASSIGNMENT NO. 7
Program to search a record using an Index. Write a c++ program to create file with each record having a structure and develop an index for the file. Deptname: 15 characters No of students: integer No of teaching staff: integer No of non-teaching staff:integer Develop an index for the above file with department name as the primary keys

Page 34

FILES AND INFORMATION STRUCTURES LAB. Program: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> #include<string.h> #include<process.h> #include "c:\tc\bin\delimbuf.h" static int i=0; class department { public: char dname[15]; int nos,nts,nnts; department() { dname[0]=nos=nts=nnts=0; } void read() { cout<<"\nEnter deptname,stno,tchno,nontchno\n "; cin>>dname; cin>>nos; cin>>nts; cin>>nnts; } void display() { cout<<"\n"<<dname<<"\t\t"<<nos<<"\t\t"<<nts<<"\t\t"<<nnts<<"\n"; } int pack(delimbuffer &)const; int unpack(delimbuffer &); }; class index { public: index(int maxkeys=100); ~index(); int search(char *key); int insert(char *key,int recadr); void clear(); int display(); int display1(); int init(int maxkeys); protected : int Maxkeys,Numkeys; char **Keys; int *Recadr; int find(char *key);
Page 35

FILES AND INFORMATION STRUCTURES LAB. }; index::index(int maxkeys):Numkeys(0),Keys(0),Recadr(0){init(maxkeys);} index::~index() { delete Keys; delete Recadr; } void index::clear() { delete Keys; delete Recadr; Numkeys=0; init(Maxkeys); } int index::init(int maxkeys) { if(maxkeys<=0) { Maxkeys=0; return 0; } Maxkeys=maxkeys; Keys=new char*[maxkeys]; Recadr=new int[maxkeys]; return 1; } int index::insert(char *key,int recadr) { if(Numkeys==Maxkeys) return 0; for(int i=Numkeys-1;i>=0;i--) { if(strcmp(key,Keys[i])>0)break; Keys[i+1]=Keys[i]; Recadr[i+1]=Recadr[i]; } Keys[i+1]=strdup(key); Recadr[i+1]=recadr; Numkeys++; return 1; } int index::find(char *key) { for(int i=0;i<=Numkeys;i++) { if(strcmp(Keys[i],key)==0) return i; else if(strcmp(Keys[i],key)>0)
Page 36

FILES AND INFORMATION STRUCTURES LAB. return -1; } return -1; } int index::search(char *key) { int found=find(key); if(found<0)return -1; else return Recadr[found]; } int index::display() { int k; if(Numkeys==0){cout<<" empty record\n";} for(i=0;i<Numkeys;i++) cout<<"\n"<<Keys[i]<<"\t"<<Recadr[i]; return k; } int index::display1() { int k; if(Numkeys==0) {cout<<" empty record \n";} return k; } int department::pack(delimbuffer &Buffer)const { int res; Buffer.clear(); res=Buffer.pack(dname); res=res&&Buffer.pack((char *)&nos); res=res&&Buffer.pack((char *)&nts); res=res&&Buffer.pack((char *)&nnts); return res; } int department::unpack(delimbuffer &Buffer) { int res; res=res&&Buffer.unpack(dname); res=Buffer.unpack((char *)&nos); res=res&&Buffer.unpack((char *)&nts); res=res&&Buffer.unpack((char *)&nnts); return res; } void main() { index index;
Page 37

FILES AND INFORMATION STRUCTURES LAB. delimbuffer dbuffer; fstream file,tmpfile; department d; long int val,inradr; char filename[10],ksearch[20],*inkey; int choice,flag=1; clrscr(); cout<<"Enter the file name\n"; cin>>filename; file.open(filename,ios::out); file.close(); while(flag) { cout<<"\n-----------MENU--------------\n1:Insert record\n2:Display primary index\n3:Searching record using primary index\n"; cout<<"4:Display all records\n5: Exit\n Enter your choice\n"; cin>>choice; switch(choice) { case 1: clrscr(); file.open(filename,ios::app); file.seekp(0,ios::end); d.read(); val=index.search(d.dname); if(val>=0) cout<<"\tDuplicate record\n"; else { inradr=file.tellp(); index.insert(d.dname,inradr); d.pack(dbuffer); dbuffer.write(file); } file.close(); break; case 2: clrscr(); index.display(); break; case 3: clrscr(); cout<<"Enter the deptname to be searched\n"; cin>>ksearch; val=index.search(ksearch); if(val<0) { cout<<"\nRecord not found...."; break; } file.open(filename,ios::in); file.seekg(val,ios::beg); dbuffer.read(file); if(file.fail())break; d.unpack(dbuffer);
Page 38

FILES AND INFORMATION STRUCTURES LAB. d.display(); file.close(); break; case 4: clrscr(); file.open(filename,ios::in); if(file.fail()) { cout<<"File empty\n"; break; } int k; k=index.display1(); if(k<=0) { cout<<"file empty\n"; break; } else { while(1) { dbuffer.read(file); if(file.fail()) break; d.unpack(dbuffer); d.display(); } } file.close(); break; case 5: flag=0;break; } } getch(); }

Output: Enter the file name: s.dat ------------MENU-----------1:Insert record 2:Display primary index 3:Searching record using primary index 4:Display all records 5:Exit
Page 39

FILES AND INFORMATION STRUCTURES LAB.

Enter your choice: 1 Enter deptname:is Enter student no:1 Enter teaching staff:2 Enter non-teaching staff:3 ------------MENU-----------1:Insert record 2:Display primary index 3:Searching record using primaryindex 4:Display all records 5:Exit Enter your choice: 1 Enter deptname:cs Enter student no:3 Enter teaching staff:2 Enter non-teaching staff:1 ------------MENU-----------1:Insert record 2:Display primary index 3:Searching record using primaryindex 4:Display all records 5:Exit Enter your choice: 2 pkey(deptname) Record address -----------------------------------cs 10 is 0 ------------MENU-----------1:Insert record 2:Display primary index 3:Searching record using primaryindex 4:Display all records 5:Exit Enter your choice: 3 Enter the deptname to be searched:is deptname studno teaching staff non-teachingstaff ----------------------------------------------------------------------------is 1 2 3 ------------MENU-----------1:Insert record 2:Display primary index 3:Searching record using primaryindex 4:Display all records 5:Exit
Page 40

FILES AND INFORMATION STRUCTURES LAB.

Enter your choice: 4 deptname studno teaching staff non-teachingstaff -----------------------------------------------------------------------------is 1 2 3 cs 3 2 1

Page 41

FILES AND INFORMATION STRUCTURES LAB.

ASSIGNMENT NO. 8
Program to search a record using secondary index Write a C++ program to create file with each record having the structure shown below. Patient number:10 characters Patient name:10 characters Disease:15 characters Develop secondary index for the above file with the patient name as the secondary key.

Program: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> #include<string.h> #include<process.h> #include "c:\tc\bin\delimbuf.h" #include "c:\tc\bin\prind.h" int i=0; class patient { public:char num[10],name[10],disease[15]; patient() {
Page 42

FILES AND INFORMATION STRUCTURES LAB. num[0]=name[0]=disease[0]=0; } void read() { cout<<"\nEnter the patient no,name,disease\n "; cin>>num; cin>>name; cin>>disease; } void display() { cout<<"\n"<<num<<"\t"<<name<<"\t"<<disease<<"\n"; } int pack(delimbuffer &)const; int unpack(delimbuffer &); }; int patient::pack(delimbuffer &Buffer)const { int res; Buffer.clear(); res=Buffer.pack(num); res=res&&Buffer.pack(name); res=res&&Buffer.pack(disease); return res; } int patient::unpack(delimbuffer &Buffer) { int res; res=res&&Buffer.unpack(num); res=Buffer.unpack(name); res=res&&Buffer.unpack(disease); return res; } class sindex { public: sindex(int maxkeys=100,int unique=1); ~ sindex(); void search(char *key,fstream& file,index& index,char *filename,delimbuffer dbuffer); int insert(char *skey,char *pkey); void clear(); void display(); int init(int maxkeys,int unique); protected : int Maxkeys,Numkeys,Unique; char **sKeys,**pKeys; }; sindex::sindex(int maxkeys,int unique):Numkeys(0),sKeys(0),pKeys(0) {init(maxkeys,unique);}
Page 43

FILES AND INFORMATION STRUCTURES LAB. sindex::~sindex() { delete sKeys; delete pKeys; } void sindex::clear() { delete sKeys; delete pKeys; Numkeys=0; init(Maxkeys,Unique); } int sindex::init(int maxkeys,int u) { Unique=u!=0; if(maxkeys<=0) { Maxkeys=0; return 0; } Maxkeys=maxkeys; sKeys=new char*[maxkeys]; pKeys=new char*[maxkeys]; return 1; } int sindex::insert(char *skey,char *pkey) { if(Numkeys==Maxkeys) return 0; for(int i=Numkeys-1;i>=0;i--) { if(strcmp(skey,sKeys[i])>0)break; sKeys[i+1]=sKeys[i]; pKeys[i+1]=pKeys[i]; } sKeys[i+1]=strdup(skey); pKeys[i+1]=strdup(pkey); Numkeys++; return 1; } void sindex::search(char *key,fstream& file,index& index,char *filename,delimbuffer dbuffer) { patient d; int add,k1=0; for(int i=0;i<=Numkeys;i++) { if(strcmp(sKeys[i],key)>0)
Page 44

FILES AND INFORMATION STRUCTURES LAB. break; if(strcmp(sKeys[i],key)==0) { k1=1; char *k=pKeys[i]; add=index.search(k); file.open(filename,ios::in); file.seekg(add,ios::beg); dbuffer.read(file); if(file.fail())break; d.unpack(dbuffer); d.display(); file.close(); } } if(!k1) cout<<"\n\t\t\Record not found...."; return ; } void sindex::display() { for(i=0;i<Numkeys;i++) cout<<"\n"<<sKeys[i]<<"\t\t"<<pKeys[i]; } void main() { index index; sindex sindex; delimbuffer dbuffer; fstream file,tmpfile; patient d; long int val,inradr; char filename[10],ksearch[20],*inkey; int choice,flag=1; clrscr(); cout<<"Enter the file name to be created : "; cin>>filename; file.open(filename,ios::out); file.close(); while(flag) { clrscr(); cout<<"\nMENU\n1:Insert\n2:Display sec index\n3:Search\n"; cout<<"4:Display contents\n5:Exit\nEnter your choice\n "; cin>>choice; switch(choice) { case 1: clrscr(); file.open(filename,ios::app); file.seekp(0,ios::end);
Page 45

FILES AND INFORMATION STRUCTURES LAB. cout<<"\nEnter details:\n"; d.read(); val=index.search(d.num); if(val>=0) cout<<"\nDuplicate\n"; else { inradr=file.tellp(); index.insert(d.num,inradr); sindex.insert(d.name,d.num); d.pack(dbuffer); dbuffer.write(file); } file.close(); break; case 2: clrscr(); if(!index.numkeys()) { cout<<"\nSecondary index is Empty."; break; } cout<<"\nThe contents of Secondary Index are :\nSKey\t\tPkey"; sindex.display(); getch(); break; case 3: clrscr(); cout<<"\nEnter the Secondary key to be searched\n"; cin>>ksearch; sindex.search(ksearch,file,index,filename,dbuffer); getch(); break; case 4: clrscr(); file.open(filename,ios::in); cout<<"\npno\tpname\tdisease\n"; while(1) { dbuffer.read(file); if(file.fail()) break; d.unpack(dbuffer); d.display(); } file.close(); getch(); break; case 5: flag=0;break; default :cout<<"invalid menu choice.\n";getch(); } } index.clear(); getch(); }
Page 46

FILES AND INFORMATION STRUCTURES LAB.

OUTPUT: Enter the file name to be created : s.dat ----------MENU---------1:Insert records 2:Display secondary index 3:Searching record using secondary index 4:Display records 5:Exit Enter your choice: 1 Enter patient no: 1 Enter patient name: aaa Enter disease: fever ----------MENU---------1:Insert records 2:Display secondary index 3:Searching record using secondary index 4:Display records 5:Exit Enter your choice: 1 Enter patient no: 2 Enter patient name: bbb Enter disease: cold
Page 47

FILES AND INFORMATION STRUCTURES LAB.

----------MENU---------1:Insert records 2:Display secondary index 3:Searching record using secondary index 4:Display records 5:Exit Enter your choice: 1 Enter patient no: 3 Enter patient name: aaa Enter disease: tb ----------MENU---------1:Insert records 2:Display secondary index 3:Searching record using secondary index 4:Display records 5:Exit Enter your choice: 2 The contents of Secondary Index are : SKey(patientname) Pkey(patientno) ------------------------------------aaa 3 aaa 1 bbb 2 ----------MENU---------1:Insert records 2:Display secondary index 3:Searching record using secondary index 4:Display records 5:Exit Enter your choice: 3 Enter the Secondary key to be searched aaa pno pname disease ------------------------3 aaa tb 1 aaa fever ----------MENU---------1:Insert records 2:Display secondary index 3:Searching record using secondary index 4:Display records 5:Exit Enter your choice: 4
Page 48

FILES AND INFORMATION STRUCTURES LAB.

pno pname disease -------------------------1 aaa fever 2 bbb cold 3 aaa tb

ASSIGNMENT NO. 9
Program to search a record using inverted list Write a c++ program to create file with each record having the structure shown below. Book number:integer Title of the book:10 characters Author:10 characters Publication:10 characters Develop an secondary index for the above file with the above author as the secondary key.

Page 49

FILES AND INFORMATION STRUCTURES LAB.

Program: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> #include<string.h> #include<process.h> #include "c:\tc\bin\delimbuf.h" #include "c:\tc\bin\prind1.h" #include "c:\tc\bin\sind.h" static int i=0; void main() { index index; sindex sindex; delimbuffer dbuffer; fstream file,tmpfile; book d; long int val,inradr; char filename[10],ksearch[20],*inkey; int choice,flag=1; clrscr(); cout<<"Enter the file name to be created : "; cin>>filename; file.open(filename,ios::out); file.close(); while(flag) { clrscr();
Page 50

FILES AND INFORMATION STRUCTURES LAB. cout<<"\nMENU\n1.Insert\n2.Display contents of inverted list\n3.Search using inverted list\n"; cout<<"4.Display contents of file\n5.Exit\nEnter your choice : "; cin>>choice; switch(choice) { case 1:clrscr(); file.open(filename,ios::app); file.seekp(0,ios::end); cout<<"Enter the records\n"; d.read(); val=index.search(d.bno); if(val>=0) cout<<"Duplicate\n"; else { inradr=file.tellp(); index.insert(d.bno,inradr); sindex.insert(d.author,d.bno); d.pack(dbuffer); dbuffer.write(file); } file.close(); getch(); break; case 2:clrscr(); cout<<"The contents of Secondary Index are...:\nSKey\tPkey"; sindex.display(); getch(); break; case 3:clrscr(); cout<<"\nEnter the keyfield of the record you want to search for Secondary :"; cin>>ksearch; sindex.search(ksearch,file,index,filename,dbuffer); getch(); break; case 4:clrscr(); file.open(filename,ios::in); while(1) { dbuffer.read(file); if(file.fail()) break; d.unpack(dbuffer); d.display(); } file.close(); getch(); break; case 5:flag=0;break; }
Page 51

FILES AND INFORMATION STRUCTURES LAB. } index.clear(); getch(); }

OUTPUT: Enter the file name to be created : a.dat MENU 1.Insert 2.Display contents of Inverted list 3.Search using inverted list 4.Display contents of file 5.Exit Enter your choice : 1 Enter the records enter the book no :12 enter the title of the book :fis enter the book's author :folk enter the name of the publication :tmh MENU 1.Insert 2.Display contents of Inverted list 3.Search using inveted list 4.Display contents of file 5.Exit Enter your choice : 1 Enter the records enter the book no :13 enter the title of the book :co enter the book's author :hamacher enter the name of the publication :tmh MENU 1.Insert 2.Display contents of Inverted list 3.Search using inveted list 4.Display contents of file 5.Exit
Page 52

FILES AND INFORMATION STRUCTURES LAB. Enter your choice : 1 Enter the records enter the book no :15 enter the title of the book :ada enter the book's author :folk enter the name of the publication :sapna MENU 1.Insert 2.Display contents of Inverted list 3.Search using inveted list 4.Display contents of file 5.Exit Enter your choice : 2 The contents of Secondary Index are...: SKey Pkey ---------------------------------folk ->12->15 hamacher ->13 MENU 1.Insert 2.Display contents of Inverted list 3.Search using inveted list 4.Display contents of file 5.Exit Enter your choice : 3 Enter the keyfield of the record you want to search for Secondary :folk Bk.no Tit. Author Pub. ---------------------------------------------12 fis folk tmh 15 ada folk sapna MENU 1.Insert 2.Display contents of Inverted list 3.Search using inveted list 4.Display contents of file 5.Exit Enter your choice : 4 Bk.no Tit. Author Pub. ---------------------------------------------12 fis folk tmh 0 co hamacher tmh 15 ada folk sapna MENU 1.Insert 2.Display contents of Inverted list 3.Search using inveted list 4.Display contents of file
Page 53

FILES AND INFORMATION STRUCTURES LAB. 5.Exit Enter your choice :5

ASSIGNMENT NO. 10
Program to search a record using combination of secondary keys Write a c++ program to create file with each record having the structure shown below. Record label: 5 characters Record Id: Integer Title: 10 characters Composer: 10 characters Develop a composer & title index for the above file.

Page 54

FILES AND INFORMATION STRUCTURES LAB.

Program: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> #include<string.h> #include<process.h> #include "c:\tc\bin\delimbuf.h" #include "c:\tc\bin\prind1.h" static int i=0; class rec { public:int id; char label[5],title[10],composer[10]; rec() { label[0]=title[0]=composer[0]=0; } void read() { cout<<"\nEnter the rec label: "; cin>>label; cout<<"Enter the rec ID no: "; cin>>id; cout<<"Enter the rec title : "; cin>>title; cout<<"Enter the composer : "; cin>>composer; } void display() { cout<<"\n"<<label<<"\t"<<id<<"\t"<<title<<"\t"<<composer<<"\n"; } int pack(delimbuffer &)const; int unpack(delimbuffer &); }; int rec::pack(delimbuffer &Buffer)const { int res;
Page 55

FILES AND INFORMATION STRUCTURES LAB. Buffer.clear(); res=Buffer.pack(label); res=res&&Buffer.pack((char*)&id); res=res&&Buffer.pack(title); res=res&&Buffer.pack(composer); return res; } int rec::unpack(delimbuffer &Buffer) { int res; res=Buffer.unpack(label); res=res&&Buffer.unpack((char*)&id); res=Buffer.unpack(title); res=res&&Buffer.unpack(composer); return res; } class sindex { public: sindex(int maxkeys=100); ~sindex(); void search(char *key1,char *key2,index &index,sindex &csindex,fstream &file,char *filename,delimbuffer &dbuffer); int insert(char *skey,int pkey); void clear(); void display(); int init(int maxkeys); int numkeys(); protected : int Maxkeys,Numkeys; char **sKeys; int *pKeys; }; sindex::sindex(int maxkeys):Numkeys(0),sKeys(0),pKeys(0) { init(maxkeys); } sindex::~sindex() { delete sKeys; delete pKeys; } void sindex::clear() { delete sKeys; delete pKeys; Numkeys=0; init(Maxkeys); } int sindex::numkeys()
Page 56

FILES AND INFORMATION STRUCTURES LAB. { return Numkeys; } int sindex::init(int maxkeys) { if(maxkeys<=0) { Maxkeys=0; return 0; } Maxkeys=maxkeys; sKeys=new char*[maxkeys]; pKeys=new int[maxkeys]; return 1; } int sindex::insert(char *skey,int pkey) { if(Numkeys==Maxkeys) return 0; for(int i=Numkeys-1;i>=0;i--) { if(strcmp(skey,sKeys[i])>0) break; sKeys[i+1]=sKeys[i]; pKeys[i+1]=pKeys[i]; } sKeys[i+1]=strdup(skey); pKeys[i+1]=pkey; Numkeys++; return 1; } void sindex::search(char *tkey,char *ckey,index &index,sindex &csindex,fstream &file,char *filename,delimbuffer &dbuffer) { rec d; int ra,m=1; for(int i=0;i<=numkeys();i++) { if(strcmp(sKeys[i],tkey)>0) break; if(strcmp(sKeys[i],tkey)==0) for(int j=0;j<=numkeys();j++) { if(strcmp(csindex.sKeys[j],ckey)>0) break; if(strcmp(csindex.sKeys[j],ckey)==0) { int k=pKeys[i]; int l=csindex.pKeys[j];
Page 57

FILES AND INFORMATION STRUCTURES LAB. if(l==k) { m=0; ra=index.search(k); file.open(filename,ios::in); file.seekg(ra,ios::beg); dbuffer.read(file); d.unpack(dbuffer); d.display(); file.close(); break; } } } } if(m) cout<<"Record not found..."; return; } void sindex::display() { for(i=0;i<Numkeys;i++) cout<<"\n"<<sKeys[i]<<"\t"<<pKeys[i]; } void main() { index index; sindex tsindex; sindex csindex; delimbuffer dbuffer; fstream file,tmpfile; rec d; long int val,inradr; char filename[10],tsearch[20],csearch[20],*inkey; int choice,flag=1; clrscr(); cout<<"Enter the file title to be created : "; cin>>filename; file.open(filename,ios::out); file.close(); while(flag) { cout<<"\nMENU\n1.Insert\n2.Display Titleindex\n3.Display Composerindex\n"; cout<<"4.Search using combination of keys\n5.Display contents of file\n6.Exit\n; cout<<enter ur choice:; cin>>choice; switch(choice) {
Page 58

FILES AND INFORMATION STRUCTURES LAB. case 1: clrscr(); file.open(filename,ios::app); file.seekp(0,ios::end); cout<<"Enter the records\n"; d.read(); val=index.search(d.id); if(val>=0) cout<<"Duplicate\n"; else { inradr=file.tellp(); index.insert(d.id,inradr); tsindex.insert(d.title,d.id); csindex.insert(d.composer,d.id); d.pack(dbuffer); dbuffer.write(file); } file.close(); break; case 2: clrscr(); cout<<"The contents of Title Index are...:\nSKey\tPkey"; tsindex.display(); getch(); break; case 3: clrscr(); cout<<"\nThe contents of Composer Index are...:\nSKey\tPkey"; csindex.display(); getch(); break; case 4: clrscr(); cout<<"\nEnter the Title & Composer of the ; cout<<record u want to search:; cin>>tsearch; cin>>csearch; tsindex.search(tsearch,csearch,index,csindex,file,filename,dbuffer); getch(); break; case 5: clrscr(); file.open(filename,ios::in); while(1) { dbuffer.read(file); if(file.fail()) break; d.unpack(dbuffer); d.display(); } file.close(); getch(); break; case 6: flag=0;break; } } index.clear();
Page 59

FILES AND INFORMATION STRUCTURES LAB. getch(); } OUTPUT: ----------MENU---------1.Insert 2.Display Titleindex 3.Display Composerindex 4.Search using combination of keys 5.Display contents of file 6.Exit Enter your choice : 1 Enter the record Enter the rec label: a Enter the rec ID no: 1 Enter the rec title : zara Enter the composer : xyz ----------MENU---------1.Insert 2.Display Titleindex 3.Display Composerindex 4.Search using combination of keys 5.Display contents of file 6.Exit Enter your choice : 1 Enter the record Enter the rec label: b Enter the rec ID no: 2 Enter the rec title : jai-ho Enter the composer : abc ----------MENU---------1.Insert 2.Display Titleindex 3.Display Composerindex 4.Search using combination of keys 5.Display contents of file 6.Exit Enter your choice : 1 Enter the record Enter the rec label: c Enter the rec ID no: 3 Enter the rec title : zara Enter the composer : xyz ----------MENU---------1.Insert 2.Display Titleindex 3.Display Composerindex 4.Search using combination of keys 5.Display contents of file 6.Exit Enter your choice : 2
Page 60

FILES AND INFORMATION STRUCTURES LAB. SKey(title) Pkey -----------------------jai-ho 2 zara 1 zara 3 ----------MENU---------1.Insert 2.Display Titleindex 3.Display Composerindex 4.Search using combination of keys 5.Display contents of file 6.Exit Enter your choice : 3 SKey(composer) Pkey ----------------------------abc 2 xyz 1 xyz 3 ----------MENU---------1.Insert 2.Display Titleindex 3.Display Composerindex 4.Search using combination of keys 5.Display contents of file 6.Exit Enter your choice : 4 Enter the Title & Composer of the record you want to search for : zara xyz Record-id Label Title Composer --------------------------------------1 a zara xyz 3 c zara xyz ----------MENU---------1.Insert 2.Display Titleindex 3.Display Composerindex 4.Search using combination of keys 5.Display contents of file 6.Exit Enter your choice : 5 Recordid Label Title Composer ------------------------------------------a 1 zara xyz b 2 jai-ho abc c 3 zara xyz

Page 61

FILES AND INFORMATION STRUCTURES LAB.

ASSIGNMENT NO. 11
Program on co-sequential processing(matching) Write a c++ program to create file with each record having the structure shown below. Student Usn:10 characters Student name:15 characters Student address:10 characters Student course:05 characters Student branch:05 characters Develop master transaction process for each student record(master) to print the student. Information & list of all credits (transaction) taken by the student Using matching operation

Program: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h>


Page 62

FILES AND INFORMATION STRUCTURES LAB. #include<string.h> #include<process.h> #include "c:\tc\bin\delimbuf.h" #include "c:\tc\bin\prind.h" static int i=0; class mast { public:char usn[10],name[15],adr[10],cr[5],branch[5];int recadr; void read(); void display(); int pack(delimbuffer &)const; int unpack(delimbuffer &); mast() { usn[0]=name[0]=adr[0]=cr[0]=branch[0]=0; } }; void mast::read() { cout<<"Enter usn: "; cin>>usn; cout<<"enter name: "; cin>>name; cout<<"Enter address: "; cin>>adr; cout<<"Enter course: "; cin>>cr; cout<<"Enter branch: "; cin>>branch; } void mast::display() { cout<<usn<<"\t"<<name<<"\t"<<adr<<"\t"<<cr<<"\t"<<branch<<endl; } int mast::pack(delimbuffer &dbuf)const { int res; dbuf.clear(); res=dbuf.pack(usn); res=res&&dbuf.pack(name); res=res&&dbuf.pack(adr); res=res&&dbuf.pack(cr); res=res&&dbuf.pack(branch); return res; } int mast::unpack(delimbuffer &dbuf) { int res; res=dbuf.unpack(usn); res=res&&dbuf.unpack(name); res=res&&dbuf.unpack(adr);
Page 63

FILES AND INFORMATION STRUCTURES LAB. res=res&&dbuf.unpack(cr); res=res&&dbuf.unpack(branch); return res; } class trans { public: char usn[10],sub[10],cred[5]; int recadr; void read(); void display(); int pack(delimbuffer &)const; int unpack(delimbuffer &); trans() { usn[0]=sub[0]=cred[0]=0; } }; void trans::read() { cout<<"Enter usn: "; cin>>usn; cout<<"Enter subject: "; cin>>sub; cout<<"Enter credits: "; cin>>cred; } void trans::display() { cout<<usn<<"\t"<<sub<<"\t"<<cred<<endl; } int trans::pack(delimbuffer &dbuf)const { int res; dbuf.clear(); res=dbuf.pack(usn); res=res&&dbuf.pack(sub); res=res&&dbuf.pack(cred); return res; } int trans::unpack(delimbuffer &dbuf) { int res; res=dbuf.unpack(usn); res=res&&dbuf.unpack(sub); res=res&&dbuf.unpack(cred); return res; } void main() { index i1,i2; fstream mfile,tfile;
Page 64

FILES AND INFORMATION STRUCTURES LAB. delimbuffer dbuf; trans t; mast m; long int val; char file1[10]="mast.dat",file2[10]="trans.dat"; int ch,i,j,n,recadr,mrec=0,trec=0; clrscr(); mfile.open(file1,ios::out); mfile.close(); tfile.open(file2,ios::out); tfile.close(); while(ch!=6) { cout<<"\nMENU\n1.Insert masterrecord\n2.Display master\n3.Insert transrecord; cout<<"4.Display trans\n5.Trans Master Process\n6.Exit"; cout<<"\nEnter ur choice: "; cin>>ch; switch(ch) { case 1: mfile.open(file1,ios::app); mfile.seekp(0,ios::end); cout<<"Enter the records\n"; m.read(); val=i1.search(m.usn); if(val>=0) cout<<"Duplicate\n"; else { recadr=mfile.tellp(); i1.insert(m.usn,recadr); m.pack(dbuf); dbuf.write(mfile); mrec++; } cout<<"\nMaster index\n"; i1.display(); mfile.close(); break; case 2: mfile.open(file1,ios::in); while(1) { dbuf.read(mfile); if(mfile.fail()) break; m.unpack(dbuf); m.display(); } mfile.close(); break; case 3: tfile.open(file2,ios::app); tfile.seekg(0,ios::end);
Page 65

FILES AND INFORMATION STRUCTURES LAB. cout<<"Enter the records\n"; t.read(); val=i2.search(t.usn); if(val>=0) cout<<"Duplicate\n"; else { recadr=tfile.tellp(); i2.insert(t.usn,recadr); t.pack(dbuf); dbuf.write(tfile); trec++; } cout<<"\nTransaction index\n"; i2.display(); tfile.close(); break; case 4: tfile.open(file2,ios::in); while(1) { dbuf.read(tfile); if(tfile.fail()) break; t.unpack(dbuf); t.display(); } tfile.close(); break; case 5: mfile.open(file1,ios::in); tfile.open(file2,ios::in); i=0;j=0; int flag=0; while(i<mrec &&j<trec) { if(strcmp(i1.Keys[i],i2.Keys[j])<0) i++; else if(strcmp(i1.Keys[i],i2.Keys[j])>0) j++; else if(strcmp(i1.Keys[i],i2.Keys[j])==0) { flag=1; int ra1=i1.Recadr[i]; mfile.seekg(ra1,ios::beg); dbuf.read(mfile); m.unpack(dbuf); cout<<m.usn<<"\t"<<m.name<<"\t"; cout<<m.adr<<"\t"<<m.cr<<"\t"<<m.branch<<"\t"; int ra2=i2.Recadr[j]; tfile.seekg(ra2,ios::beg); dbuf.read(tfile); t.unpack(dbuf);
Page 66

FILES AND INFORMATION STRUCTURES LAB. cout<<t.sub<<"\t"<<t.cred<<endl; i++; j++; } } if (flag==0) cout<<"No match found"; mfile.close(); tfile.close(); break; case 6: break; } } getch(); }

OUTPUT: ----------MENU---------1.Insert master record 2.Display master file 3.Insert transaction record 4.Display tranaction file 5.Display Master-Transaction Process 6.Exit Enter ur choice: 1 Enter usn: 01 enter name: arun Enter address: xyz Enter course: be Enter branch: ise
Page 67

FILES AND INFORMATION STRUCTURES LAB.

Master index : pkey recadr --------------01 0 ----------MENU---------1.Insert master record 2.Display master file 3.Insert transaction record 4.Display tranaction file 5.Display Master-Transaction Process 6.Exit Enter ur choice: 1 Enter usn: 02 enter name: rajesh Enter address: abx Enter course: be Enter branch: ise Master index pkey recadr -------------01 0 02 20 ----------MENU---------1.Insert master record 2.Display master file 3.Insert transaction record 4.Display tranaction file 5.Display Master-Transaction Process 6.Exit Enter ur choice: 2 Usn name addr course branch ---------------------------------------01 arun xyz be ise 02 rajesh abx be ise ----------MENU---------1.Insert master record 2.Display master file 3.Insert transaction record 4.Display tranaction file 5.Display Master-Transaction Process 6.Exit Enter ur choice: 3 Enter usn: 01 Enter subject: fis
Page 68

FILES AND INFORMATION STRUCTURES LAB. Enter credits: 50 Transaction index pkey recadr ----------------01 0 ----------MENU---------1.Insert master record 2.Display master file 3.Insert transaction record 4.Display tranaction file 5.Display Master-Transaction Process 6.Exit Enter ur choice: 4 Usn sub credits ---------------------------01 fis 50 ----------MENU---------1.Insert master record 2.Display master file 3.Insert transaction record 4.Display tranaction file 5.Display Master-Transaction Process 6.Exit Enter ur choice:5 Master-transaction proscess: Usn name addr course branch sub credits --------------------------------------------------------------------01 arun xyz be ise fis

ASSIGNMENT NO. 15
Page 69

FILES AND INFORMATION STRUCTURES LAB. Program on Hashing Write a C++ program to create file with each record having the structure shown below. Mobile model Number:10 characters Mobile phone Number: Integer Mobile holder name: 10 characters Develop a hashed index of the above file with Mobile model Number as a primary key. Write the methods to read & write the records in the file & to search record using hashed index.

PROGRAM: #include <iostream.h> #include <conio.h> #include <stdio.h> #include <fstream.h> #include <string.h> #include <process.h> #include "c:\tc\bin\delimbuf.h" #include "c:\tc\bin\prind.h" static int i=0; class mobile { public:char mno[10]; int pno; char name[10]; mobile() { name[0]=pno=mno[0]=0; } void read() { cout<<"\nEnter the mobile model number : ";
Page 70

FILES AND INFORMATION STRUCTURES LAB. cin>>mno; cout<<"Enter the phone number : "; cin>>pno; cout<<"Enter the mobile holder name : "; cin>>name; } void display() { cout<<"\n"<<mno<<"\t"<<pno<<"\t"<<name; } int pack(delimbuffer &)const; int unpack(delimbuffer &); }; int mobile::pack(delimbuffer &Buffer)const { int res; Buffer.clear(); res=res&&Buffer.pack(mno); res=res&&Buffer.pack((char *)&pno); res=res&&Buffer.pack(name); return res; } int mobile::unpack(delimbuffer &Buffer) { int res; res=Buffer.unpack(mno); res=res&&Buffer.unpack((char *)&pno); res=res&&Buffer.unpack(name); return res; } class hash:public index { public: hash(int maxkeys=100); ~hash(); int hashfunction(char* key); void display(); int rasearch(int); int rec(int); }; hash::hash(int maxkeys) {init(maxkeys);} hash::~hash() {delete Keys;delete Recadr;} int hash::rasearch(int recadr) { for(int i=0;i<=Numkeys;i++)
Page 71

FILES AND INFORMATION STRUCTURES LAB. if(Recadr[i]==recadr) return 1; return -1; } int hash::rec(int i) { return Recadr[i]; } void hash::display() { cout<<"Prim.key\tHome addr"; for(i=0;i<Numkeys;i++) cout<<"\n"<<Keys[i]<<"\t\t"<<Recadr[i]; } int hash::hashfunction(char* key) { int sum=0; int len=strlen(key); if(len%2==1) len++; for(int j=0;j<len;j+=2) sum=(sum+key[j]*100+key[j+1])%19937; return sum%101; } void main() { hash hash; delimbuffer dbuffer; fstream file,tmpfile; mobile d; int val,inradr,address; char filename[10],ksearch[20],*inkey,che='y'; int choice,flag=1; clrscr(); cout<<"Enter the file name to be created : "; cin>>filename; file.open(filename,ios::out); file.close(); while(flag) { cout<<"\nMENU\n1.Create the fie\n2.Display contents of hash table\n3.To Search an record\n4.Display records of file\n5.Exit\nEnter your choice : "; cin>>choice; switch(choice) { case 1: file.open(filename,ios::out); while(che=='y') {
Page 72

FILES AND INFORMATION STRUCTURES LAB. cout<<"Enter the records\n"; d.read(); val=hash.search(d.mno); if(val>=0) cout<<"Duplicate\n"; else { address=hash.hashfunction(d.mno); val=hash.rasearch(address); if(val==1) { cout<<"Collision occuring...... at address : "<<address<<"\n "; hash.display(); break; } file.seekp(address,ios::beg); hash.insert(d.mno,address); d.pack(dbuffer); dbuffer.write(file); hash.display(); } cout<<"\nDo you want to continue !!!!!!! (y/n) : "; cin>>che; } file.close(); break; case 2: cout<<"The contents of Hashed Index are...:\n"; hash.display(); break; case 3: cout<<"Enter the keyfield of the record you want to search for :"; cin>>inkey; address=hash.search(inkey); if(address<0) { cout<<"Record not found...."; break; } else { file.open(filename,ios::in); file.seekg(address,ios::beg); dbuffer.read(file); if(file.fail()) break; d.unpack(dbuffer); d.display(); file.close(); } break; case 4: int k; file.open(filename,ios::in); for(i=0;i<hash.numkeys();i++) {
Page 73

FILES AND INFORMATION STRUCTURES LAB. address=hash.rec(i); file.seekg(address,ios::beg); dbuffer.read(file); if(file.fail()) break; d.unpack(dbuffer); d.display(); } k=hash.numkeys(); if(k<=0) cout<<"No records in the file !\n"; file.close(); break; case 5: flag=0;break; } } hash.clear(); getch(); }

OUTPUT: Enter the file name to be created : a.dat MENU 1.Create the file 2.Display contents of hash table 3.To Search an record 4.Display records of file 5.Exit Enter your choice : 1 Enter the records Enter the mobile model number : 12 Enter the phone number : 1234 Enter the mobile holder name : arun Prim.key Home addr -------------------------------------12 1 Do you want to continue !!!!!!! (y/n) : y Enter the records Enter the mobile model number : 4 Enter the phone number : 5678 Enter the mobile holder name : rajesh Prim.key Home addr -------------------------------------12 1 4 49 Do you want to continue !!!!!!! (y/n) : y Enter the records
Page 74

FILES AND INFORMATION STRUCTURES LAB. Enter the mobile model number : 56 Enter the phone number : 8901 Enter the mobile holder name : ajay Collision occuring...... at address : 1 Prim.key Home addr -------------------------------------12 1 4 49 MENU 1.Create the file 2.Display contents of hash table 3.To Search an record 4.Display records of file 5.Exit Enter your choice : 2 The contents of Hashed Index are...: Prim.key Home addr -------------------------------------12 1 4 49 MENU 1.Create the file 2.Display contents of hash table 3.To Search an record 4.Display records of file 5.Exit Enter your choice : 3 Enter the keyfield of the record you want to search for :12 Model-no. Ph.no. H.name -------------------------------------12 1234 arun MENU 1.Create the file 2.Display contents of hash table 3.To Search an record 4.Display records of file 5.Exit Enter your choice : 4 Model-no. Ph.no. H.name -------------------------------------12 1234 arun 4 5678 rajesh

Page 75

FILES AND INFORMATION STRUCTURES LAB.

APPENDIX
1.Fixed length Buffer class defition & methods(fixlenbu.h) (Used in Assignment no.5)
class fixedlenbuffer { public: fixedlenbuffer(int recordsize=100); void clear(); int read(istream&); int write(ostream&)const; protected: int initialize,Maxbyte,buffersize,nextbyte,packing; char *buffer; }; fixedlenbuffer::fixedlenbuffer(int recordsize) { initialize=0; if(recordsize<0) recordsize=0; Maxbyte=recordsize; buffer=new char[Maxbyte]; clear(); } void fixedlenbuffer::clear() { nextbyte=0; packing=1; buffer[0]=0; } int fixedlenbuffer::read(istream& stream) {
Page 76

FILES AND INFORMATION STRUCTURES LAB. int record=stream.tellg(); clear(); packing=0; stream.read(buffer,buffersize); if(!stream.good())return 0; return record; } int fixedlenbuffer::write(ostream& stream)const { int record=stream.tellp(); stream.write(buffer,buffersize); if(!stream.good()) return -1; return record; }

2.Delimbuffer class definition(delimbuf.h)


class delimbuffer { public: delimbuffer(char delim='|',int maxbyte=100); void clear(); int pack(const char*,int size=-1); int write(ostream &)const; int read(istream &); int unpack(char*); protected: char *Buffer,Delim,delimstr[2]; int Buffersize,Nextbyte,Maxbyte; }; delimbuffer::delimbuffer(char delim,int maxbyte) { Delim=delim; delimstr[0]=Delim; delimstr[1]=0; if(maxbyte<0) maxbyte=0; Maxbyte=maxbyte; Buffer=new char[Maxbyte]; Buffersize=0; } void delimbuffer::clear() { Nextbyte=0;Buffersize=0;Buffer[0]=0; } int delimbuffer::pack(const char *str,int size) { short len; if(size>=0) len=size;
Page 77

FILES AND INFORMATION STRUCTURES LAB. else len=strlen(str); if(len>strlen(str)) return 0; int start=Nextbyte; Nextbyte+=len+1; if(Nextbyte>Maxbyte) return 0; memcpy(&Buffer[start],str,len); Buffer[start+len]=Delim; Buffersize=Nextbyte; return 1; } int delimbuffer::write(ostream &file)const { int recadr=file.tellp(); file.write(Buffer,Buffersize); file.write("#",1); if(file.good()) return recadr; return -1; } int delimbuffer::read(istream &file) { clear(); file.getline(Buffer,Maxbyte,'#'); Buffersize=strlen(Buffer); if(file.good())return 1; return -1; } int delimbuffer::unpack(char *str) { int len=-1; int start=Nextbyte; for(int i=start;i<Buffersize;i++) { if(Buffer[i]==Delim) { len=i-start; break; } } if(len==-1)return 0; Nextbyte+=len+1; if(Nextbyte>Buffersize) return 0; strncpy(str,&Buffer[start],len); str[len]=0; return 1; }

Page 78

FILES AND INFORMATION STRUCTURES LAB.

3.Primary index class definition for character type primary key (prind.h)
class index { public: index(int maxkeys=100,int unique=1); ~index(); int search(char *key); int insert(char *key,int recadr); int remove(char* key); void clear(); void display(); int init(int maxkeys,int unique); int numkeys(); protected : int Maxkeys,Numkeys,Unique; char **Keys; int *Recadr; int find(char *key); }; index::index(int maxkeys,int unique):Numkeys(0),Keys(0),Recadr(0) {init(maxkeys,unique);} index::~index() {delete Keys;delete Recadr;} void index::clear() { delete Keys; delete Recadr; Numkeys=0; init(Maxkeys,Unique); } int index::numkeys() {return Numkeys;} int index::init(int maxkeys,int unique) { Unique=unique!=0; if(maxkeys<=0)
Page 79

FILES AND INFORMATION STRUCTURES LAB. { Maxkeys=0; return 0; } Maxkeys=maxkeys; Keys=new char*[maxkeys]; Recadr=new int[maxkeys]; return 1; } int index::insert(char *key,int recadr) { if(Numkeys==Maxkeys) return 0; for(int i=Numkeys-1;i>=0;i--) { if(strcmp(key,Keys[i])>0)break; Keys[i+1]=Keys[i]; Recadr[i+1]=Recadr[i]; } Keys[i+1]=strdup(key); Recadr[i+1]=recadr; Numkeys++; return 1; } int index::remove(char* key) { int indx=find(key); if(indx<0)return 0; for(int i=indx;i<Numkeys;i++) { Keys[i]=Keys[i+1]; Recadr[i]=Recadr[i+1]; } Numkeys--; return 1; } int index::find(char *key) { for(int i=0;i<=Numkeys;i++) { if(strcmp(Keys[i],key)==0) return i; else if(strcmp(Keys[i],key)>0) return -1; } return -1; } int index::search(char *key) { int found=find(key); if(found<0)return -1; else return Recadr[found];
Page 80

FILES AND INFORMATION STRUCTURES LAB. } void index::display() { cout<<"\n"; for(int i=0;i<Numkeys;i++) cout<<"\n"<<Keys[i]<<"\t"<<Recadr[i];

4.Primary index class definition for interger type primary key (prind1.h)
class index { public: index(int maxkeys=100,int unique=1); ~index(); int search(int key); int insert(int key,int recadr); void clear(); int init(int maxkeys,int unique); protected : int Maxkeys,Numkeys,Unique; int *Keys; int *Recadr; int find(int key); }; index::index(int maxkeys,int unique):Numkeys(0),Keys(0),Recadr(0) {init(maxkeys,unique);} index::~index() {delete Keys;delete Recadr;} void index::clear() { delete Keys; delete Recadr; Numkeys=0; init(Maxkeys,Unique); } int index::init(int maxkeys,int unique) { Unique=unique!=0; if(maxkeys<=0) { Maxkeys=0; return 0; } Maxkeys=maxkeys; Keys=new int[maxkeys]; Recadr=new int[maxkeys]; return 1; }
Page 81

FILES AND INFORMATION STRUCTURES LAB. int index::insert(int key,int recadr) { if(Numkeys==Maxkeys) return 0; for(int i=Numkeys-1;i>=0;i--) { if(key>Keys[i])break; Keys[i+1]=Keys[i]; Recadr[i+1]=Recadr[i]; } Keys[i+1]=key; Recadr[i+1]=recadr; Numkeys++; return 1; } int index::find(int key) { for(int i=0;i<=Numkeys;i++) { if(Keys[i]==key) return i; else if(Keys[i]>key) return -1; } return -1; } int index::search(int key) { int found=find(key); if(found<0)return -1; else return Recadr[found]; }

Page 82

FILES AND INFORMATION STRUCTURES LAB.

5.Secondary index class definition for character type secondary key (sind.h)
#include "c:\tc\bin\book.h" #include "c:\tc\bin\node.h" class sindex { public: sindex(int maxkeys=100); ~sindex(); void search(char *key,fstream &file,index &index,char *filename,delimbuffer &dbuffer); int insert(char *skey,int pkey); void clear(); void display(); int init(int maxkeys); int numkeys(); protected : int Maxkeys,Numkeys,Unique; char **sKeys; node **head; }; sindex::sindex(int maxkeys):Numkeys(0),sKeys(0),head(0) {init(maxkeys);} sindex::~sindex() {delete sKeys;delete head;} void sindex::clear() { delete sKeys; delete head; Numkeys=0; init(Maxkeys); } int sindex::numkeys() {return Numkeys;} int sindex::init(int maxkeys) { if(maxkeys<=0) { Maxkeys=0; return 0; } Maxkeys=maxkeys; sKeys=new char*[maxkeys]; head=new node*[maxkeys]; for(int i=0;i<=maxkeys;i++) head[i]=0; return 1;
Page 83

FILES AND INFORMATION STRUCTURES LAB. } /*int sindex::insert(char *skey,int pkey) { if(Numkeys==Maxkeys) return 0; for(int i=0;i<=Numkeys;i++) if(strcmp(skey,sKeys[i])==0) { head[i]=head[i]->insert(head[i],pkey); return 1; } for(i=Numkeys-1;i>=0;i--) { if(strcmp(skey,sKeys[i])>0)break; sKeys[i+1]=sKeys[i]; head[i+1]=head[i]; } sKeys[i+1]=strdup(skey); head[i+1]=head[i]->insert(0,pkey); Numkeys++; return 1; } void sindex::search(char *key,fstream &file,index &index,char *filename,delimbuffer &dbuffer) { book d; node *head1; int ra,m=1; for(int i=0;i<=Numkeys;i++) { if(strcmp(sKeys[i],key)>0) break; if(strcmp(sKeys[i],key)==0) { m=0; for(head1=head[i];head1!=0;head1=head1->next) { int k=head1->pkey; ra=index.search(k); file.open(filename,ios::in); file.seekg(ra,ios::beg); dbuffer.read(file); d.unpack(dbuffer); d.display(); file.close(); } } } if(m) cout<<"Record not found...";
Page 84

FILES AND INFORMATION STRUCTURES LAB. return; } */ void sindex::display() { for(int i=0;i<Numkeys;i++) { cout<<"\n"<<sKeys[i]<<"\t"; head[i]->ndisplay(head[i]); } }

6.Book class definition & necessary methods (book.h) (Used only for Assignment no.9)
Page 85

FILES AND INFORMATION STRUCTURES LAB. class book { public: char title[10],author[10],pub[10]; int bno; book() {bno=0;title[0]=author[0]=pub[0];} void read() { cout<<"enter the book no :"; cin>>bno; cout<<"enter the title of the book :"; cin>>title; cout<<"enter the book's author :"; cin>>author; cout<<"enter the name of the publication :"; cin>>pub; } void display() { cout<<"\n"<<bno<<"\t"<<title<<"\t"<<author<<"\t"<<pub; } int pack(delimbuffer &)const; int unpack(delimbuffer &); }; int book::pack(delimbuffer &buffer)const { int res; buffer.clear(); res=buffer.pack((char*)&bno); res=res&&buffer.pack(title); res=res&&buffer.pack(author); res=res&&buffer.pack(pub); return res; } int book::unpack(delimbuffer &buffer) { int res; res=buffer.unpack((char*)&bno); res=res&&buffer.unpack(title); res=res&&buffer.unpack(author); res=res&&buffer.unpack(pub); return res; }

7.Node class definition & necessary methods (book.h) (Used only for Assignment no.9)
class node {
Page 86

FILES AND INFORMATION STRUCTURES LAB. public: int pkey; node *next; node() {pkey=0;next=0;} node* insert(node *head,int pkey); void ndisplay(node *head); }; node *node::insert(node *head,int key) { node *temp,*temp1,*temp2=head; temp=new node; temp->pkey=key; temp->next=0; if(temp2==0) return temp; if(temp2->pkey>=key) { temp->next=temp2; return temp; } while(temp2!=0&&temp2->pkey<=key) { temp1=temp2; temp2=temp2->next; } temp->next=temp2; temp1->next=temp; return head; } void node::ndisplay(node *head) { cout<<"pkeys"; while(head!=0) { cout<<"->"<<head->pkey; head=head->next; } }

Page 87

S-ar putea să vă placă și