Saturday, 17 November 2012

Fs lab program for primary indexing


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

      void create_primary_index();
      void insert();
      void remove(string);
      void search(string);
      int search_primary_index(string);
      string extract_USN(string);
      void sort_primary_index();
     
};

void primary_index::create_primary_index()
{
      fstream file;
      int pos;
      string buffer,USN;
      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
            USN=extract_USN(buffer);
            USN_list[++count]=USN;
            Address_list[count]=pos;     
      }
      file.close();    
      sort_primary_index();        
}

string primary_index::extract_USN(string buffer)
{
      string USN;
      int i=0;
      USN.erase();     
      while (buffer[i]!='|')
        USN+=buffer[i++];
      return USN;
}
void primary_index::sort_primary_index()
{
      int i,j,temp_Address;
      string temp_USN;
      for (int i=0;i<=count;i++)
      {
        for (int j=i+1;j<=count;j++)
        {
            if (USN_list[i]>USN_list[j])
            {
              temp_USN=USN_list[i];
              USN_list[i]=USN_list[j];
              USN_list[j]=temp_USN;
              
              temp_Address=Address_list[i];
              Address_list[i]=Address_list[j];
              Address_list[j]=temp_Address;
      }}}
     
}
void primary_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();
      USN_list[++count]=USN;
      Address_list[count]=pos;
      sort_primary_index();        
}

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

void primary_index::search(string key)
{
      int pos=0,address;
      string buffer;
      fstream file;
      buffer.erase();  
      pos=search_primary_index(key);
      if (pos){
            file.open("1.txt");
            address=Address_list[pos];
            file.seekp(address,ios::beg);
            getline(file,buffer);
            cout<<"\nFound the record:  "<<buffer;   
            file.close();    
            }
      else cout<<"\nNot Found";
}

void primary_index::remove(string key)
{
      int pos=0,address,i;
      char del_ch='*';
      fstream file;
      pos=search_primary_index(key);
      if (pos){
            file.open("1.txt");
            address=Address_list[pos];
            file.seekp(address,ios::beg);
            file.put(del_ch);
            cout<<"\nRecord Deleted:  "; 
            file.close();    

            for (i=pos;i<count;i++)
            {
                  USN_list[i]=USN_list[i+1];
                  Address_list[i]=Address_list[i+1];
            }
            count--;
      }
      else cout<<"\nNot Found";
}
     
int main()
{
      int choice;
      string key;
      primary_index i1;
     
      i1.create_primary_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 USN to search";
                  cin>>key;
                  i1.search(key);
                  break;
            case 3:cout <<"\n\nEnter the USN to delete";
                  cin>>key;
                  i1.remove(key);
                  break;
            case 4: return 0;
            default:cout<<"\n\nWrong Choice";
                  break;
      }}
}
     
     
           
 

No comments:

Post a Comment