Enter your email address & Win:

File Structure ( FS ) Lab Program 6 ( 6th Semester Information Science) – Alternative 2

// June 13th, 2009 // Educational, Engineering, File Structure (F.S.) Lab // Written by sandeephegde

-->

Program 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

This includes tow files

1.

//scindex.h
class SCIndex
{
public:

int  Insert (const char  *skey,const char  *pkey);
int  Remove (const char * key);
int Search (const char * key) const;
void Print (ostream &);
void Write (ostream &);
void Read(istream &);
void Init (int maxKeys);
private:
int MaxKeys;
int NumKeys;
char  **sKeys;
char  **pKeys;
int Find (const char * key) const;

};

int SCIndex :: Insert (const char *skey,const char *pkey)
{
int i=0,j=0;
if (NumKeys == MaxKeys) return 0; //no room for another key

for (i = NumKeys-1; i >=0; i–)
{

if(strcmp(skey,sKeys[i]) > 0)
break; // insert into location i+1
else if(strcmp(skey,sKeys[i]) == 0)
break; // insert into location i+1

else
{
sKeys[i+1] = sKeys[i];
pKeys[i+1] = pKeys[i];
}
}
j=i;

for(j=i; j>=0; j–)

//   while(strcmp(skey,sKeys[j]) == 0)
{
if(strcmp(pkey,pKeys[j]) > 0)
break;
sKeys[j+1] = sKeys[j];
pKeys[j+1] = pKeys[j];

}
sKeys[j+1] = strdup(skey);
pKeys[j+1] = strdup(pkey);
NumKeys ++;
return 1;
}

void SCIndex::Write(ostream &stream)
{    int i=0;
stream<<NumKeys<<”|”;
for (i = 0; i <NumKeys; i++)
{

stream<<sKeys[i];
stream<<”|”;
stream<<pKeys[i];
stream<<”|”;

}
}

int SCIndex :: Remove (const char * key)
{
int index = Find (key);
if (index < 0) return 0; // key not in index
for (int i = index; i < NumKeys; i++)
{
sKeys[i] = sKeys[i+1];
pKeys[i] = pKeys[i+1];
}
NumKeys –;
return 1;
}

int  SCIndex :: Search (const char * key) const
{
int i=0,fnd=0;
for(i=0;i<NumKeys;i++)
{
if (strcmp(sKeys[i], key)==0)
{
fnd=1;
cout<<endl<<sKeys[i]<<”             “<<pKeys[i];
}
}
if (fnd==0)
return -1;
return 1;

}

void SCIndex :: Print (ostream & stream)
{
stream << “Secondary Index :  Max keys = “<<MaxKeys
<<”    Number of  keys = “<<NumKeys<<endl;
for (int i = 0; i<NumKeys; i++)
stream <<”\tKey["<<i<<"] “<<sKeys[i]
<<”  : Primary key =”<<pKeys[i]<<endl;
}

int SCIndex :: Find (const char * key) const
{
for (int i = 0; i < NumKeys; i++)
if (strcmp(sKeys[i], key)==0)
return i;// key found
else if (strcmp(sKeys[i], key)>0) return -1;// not found
return -1;// not found
}

void SCIndex :: Init (int maxKeys)
{

if (maxKeys <= 0)
{
MaxKeys = 0;
cout<<endl<<”No Records : “<<endl;
}
MaxKeys = maxKeys;
sKeys =new char *[maxKeys];
pKeys = new char *[maxKeys];
NumKeys=0;

}

void SCIndex::Read(istream &stream)
{
int i=0;
/*
stream.read(NumKeys,4);
for (i = 0; i <NumKeys; i++)
{

stream.read(sKeys[i],10);
stream.read(pKeys[i],10);
}
*/
}

—————————————————————————————————————–

2. //lab6.cpp

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include “student.h”
#include “varbuf.h”
#include “index.h”
#include “scindex.h”

main()
{

//char buffer[500];

student s;
VariableLengthBuffer b;
TextIndex ind;
SCIndex scind;
fstream datafile,indexp,indexs;
int choice=0,k=0,k1=0,n=0;
int recaddr,i;
char delkey[20],skey[20],srchp[10];
datafile.open(“student.txt”,ios::out|ios::in);
indexp.open(“pstudent.ind”, ios::out);
indexs.open(“sstudent.ind”, ios::out);

b.Init(65);
ind.Init(50);
scind.Init(50);
b.Clear();
cout<<endl<<”Enter number of students : “;
cin>>n;

for(i=1;i<=n;i++)
{
cout<<endl<<” Record : “<<i;
s.clear();
s.readdata();
b.Clear();
b.Pack(s.usn);
b.Pack(s.name);
b.Pack(s.address);
b.Pack(s.sem);
recaddr=datafile.tellg();
k=ind.Insert(s.usn,recaddr);
if( k != 1)
cout<<endl<<” Duplicate Primary key : Record Not Inserted “;
else
{
b.Write(datafile);
k=scind.Insert(s.name,s.usn);
}
}

do
{
cout<<endl;
cout<<endl<<”1.Insert Record “;
cout<<endl<<”2.Delete Record “;
cout<<endl<<”3.Search Record “;
cout<<endl<<”4.Display Primary Index”;
cout<<endl<<”5.Display Secondary Index”;
cout<<endl<<”6.Exit “;
cout<<endl;

cout<<” Enter your choice “;
cin>>choice;
switch(choice)
{
case 1:
cout<<endl;
s.readdata();
datafile.seekg(0,ios::end);
recaddr=datafile.tellg();
k=ind.Insert(s.usn,recaddr);
if(k!=1)
cout<<endl<<”Record not inserted : Duplicate Primary Key “;

else
{
k=scind.Insert(s.name,s.usn);
b.Clear();
b.Pack(s.usn);
b.Pack(s.name);
b.Pack(s.address);
b.Pack(s.sem);
b.Write(datafile);
cout<<endl<<”Record Inserted “;
}
break;

case 2:
cout<<endl;
cout<<” Enter Name of the Record to be deleted : ” ;
cin>>delkey;
cout<<endl<<”Following Record/s Found for : “<<delkey<<endl;
cout<<endl<<”Secondary Key   Primary Key”;
cout<<endl<<”————–  ————-”;
k1=scind.Search(delkey);
if(k1 !=1)
{
cout<<endl<<” Record not found “;
break;
}
else
{
cout<<endl<<” Enter USN : “;
cin>>srchp;

k=scind.Remove(delkey);
k1=ind.Remove(srchp);
if(k!=1 || k1!=1)
cout<<endl<<”Record not Found “;
else
cout<<endl<<”Record of “<<delkey<<” USN “<<srchp<<” Deleted”;

}
break;

case 3:
cout<<endl;
cout<<” Enter name of the student to be Searched : ” ;
cin>>skey;
cout<<endl<<”Following Record/s Found for : “<<skey<<endl;
cout<<endl<<”Secondary Key    Primary Key”;
cout<<endl<<”————–   ————”;

k1=scind.Search(skey);
if(k1 !=1)
{
cout<<endl<<” Record not found “;
break;
}
else
{
cout<<endl<<” Enter USN : “;
cin>>srchp;
recaddr=ind.Search(srchp);
if(recaddr<0)
cout<<endl<<”Record does not exist”;
else
{
cout<<endl<<” Record found “;
cout<<endl<<”Record reference of USN “<<srchp<<” is “<<recaddr<<endl;
datafile.seekg(recaddr,ios::beg);
b.Clear();
b.Read(datafile);
b.bufdisplay();
s.clear();
b.Unpack(s.usn);
b.Unpack(s.name);
b.Unpack(s.address);
b.Unpack(s.sem);
s.display();
s.clear();
datafile.seekg(0,ios::end);
}
}
break;

case 4:
ind.Print(cout);
break;

case 5:
scind.Print(cout);
break;
//case 5: exit(1);
}
}   while(choice < 6);

ind.Write(indexp);
scind.Write(indexs);

indexp.close();
indexs.close();
datafile.close();

}


If you enjoyed this post, make sure you subscribe to my RSS feed!

Related posts:

  1. File Structure ( FS ) Lab Program 6 ( 6th Semester Information Science)
  2. File Structure ( FS ) Lab Program 5 ( 6th Semester Information Science)
  3. File Structure ( FS ) Lab Program 6 ( 6th Semester Information Science)- Alternative 1
  4. File Structure ( FS ) Lab Program 11 ( 6th Semester Information Science)
  5. File Structure ( FS ) Lab Program 12 ( 6th Semester Information Science)
blog comments powered by Disqus
Get Adobe Flash playerPlugin by wpburn.com wordpress themes