Sunday, 18 November 2012

FS lab program for secondary indexing


#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
class secondary_index
{
  public:
      string Name_list[100];
      int Address_list[100];
      int count;

      void create_index();
      void insert();
      void remove(string);
      void delete_from_file(int);
      void search(string);
      int search_index(string);
      void read_from_file(int);
      string extract_Name(string);
      void sort_index();
     
};

void secondary_index::create_index()
{
      fstream file;
      int pos;
      string buffer,Name;
      count=-1;
      file.open("1.txt",ios::in);
      while (!file.eof())
      {
            pos=file.tellg();      
            buffer.erase();        
            getline(file,buffer);
            if (buffer.empty()) break; //imp since it reads the last \n and goes to new line
            Name=extract_Name(buffer);
            Name_list[++count]=Name;
            Address_list[count]=pos;     
      }
      file.close();    
      sort_index();          
}

string secondary_index::extract_Name(string buffer)
{
      string USN,Name;
      int i=0;
      USN.erase();     
      while (buffer[i]!='|')
        USN+=buffer[i++];

      Name.erase();
      i++;
      while (buffer[i]!='|')
        Name+=buffer[i++];

      return Name;
}
void secondary_index::sort_index()
{
      int i,j,temp_Address;
      string temp_Name;
      for (int i=0;i<=count;i++)
      {
        for (int j=i+1;j<=count;j++)
        {
            if (Name_list[i]>Name_list[j])
            {
              temp_Name=Name_list[i];
              Name_list[i]=Name_list[j];
              Name_list[j]=temp_Name;
              
              temp_Address=Address_list[i];
              Address_list[i]=Address_list[j];
              Address_list[j]=temp_Address;
      }}}
     
}
void secondary_index::insert()
{
      string USN, Name, Branch, sem, buffer;
      int Semester,pos;
      fstream file;    
      cout<<"\nUsn:";
      cin>>USN;
      cout<<"\nName:";
      cin>>Name;
      cout<<"\nBranch:";
      cin>>Branch;
      cout<<"\nSemster:";
      cin>>Semester;
      stringstream out;
      out << Semester;
      sem = out.str(); 
      buffer.erase();
      buffer=USN+'|'+Name+'|'+Branch+'|'+sem+'$'+'\n';
      file.open("1.txt",ios::out|ios::app);
      pos=file.tellp();
      file<<buffer;
      file.close();
      Name_list[++count]=Name;
      Address_list[count]=pos;
      sort_index();          
}

int secondary_index::search_index(string key)
{
      int low=0,high=count,mid=0,flag=0,pos;
      while (low<=high)
      {
            mid=(low+high)/2;
            if (Name_list[mid]==key) {flag=1; break;}
            if (Name_list[mid]>key) high=mid-1;
            if (Name_list[mid]<key) low=mid+1;
      }
      if (flag) return mid;
      else return 0;
}

void secondary_index::search(string key)
{
      int pos=0,t;
      string buffer;
     
      buffer.erase();  
      pos=search_index(key);
      if (pos){
            read_from_file(pos);
            t=pos;
            while (Name_list[++t]==key) read_from_file(t);
            t=pos;
            while (Name_list[--t]==key) read_from_file(t); 
            }
      else cout<<"\nNot Found";
}

void secondary_index::read_from_file(int pos)
{
      int address;     
      string buffer;
      fstream file;
      file.open("1.txt");
      address=Address_list[pos];
      file.seekp(address,ios::beg);
      getline(file,buffer);
      cout<<"\nFound the record:  "<<buffer;   
      file.close();
}

void secondary_index::remove(string key)
{
     
      int pos=0,t,choice;
      string buffer;
      buffer.erase();  
      pos=search_index(key);
      if (pos){
            read_from_file(pos);
            cout<<"\n Delete?";
            cin>>choice;
            if (choice) delete_from_file(pos);
            t=pos;
            while (Name_list[++t]==key){
                  read_from_file(t);
                  cout<<"\n Delete?";
                  cin>>choice;
                  if (choice) delete_from_file(t);
                  }
            t=pos;
            while (Name_list[--t]==key){
                  read_from_file(t);
                  cout<<"\n Delete?";
                  cin>>choice;
                  if (choice) delete_from_file(t);               
                  }
            }
      else cout<<"\n Not Found";
}

void secondary_index::delete_from_file(int pos)
{
      char del_ch='*';
      int i, address;
      fstream file;
      file.open("1.txt");
      address=Address_list[pos];
      file.seekp(address,ios::beg);
      file.put(del_ch);
      cout<<"\nRecord Deleted:  ";
     
      for (i=pos;i<count;i++)
            {
                  Name_list[i]=Name_list[i+1];
                  Address_list[i]=Address_list[i+1];
            }
      count--;
}
     
int main()
{
      int choice;
      string key;
      secondary_index i1;
     
      i1.create_index();
     
      while(1){
      cout <<"\nMain Menu\n------------\n1.Add  \n2.Search \n3.Delete \n4.EXIT\n---------------\nEnter the choice:";
      cin>>choice;     
      switch (choice)
      {
            case 1: cout<<"Data\n";
                  i1.insert();
                  break;
            case 2: cout <<"\n\nEnter the name";
                  cin>>key;
                  i1.search(key);
                  break;
            case 3:cout <<"\n\nEnter the name";
                  cin>>key;
                  i1.remove(key);
                  break;

            case 4: return 0;
            default:cout<<"\n\nWrong Choice";
                  break;
      }}
}
     
     
           

No comments:

Post a Comment