pickzy.com

C  |  C++  |  Objective-C  |  VC++  |  Win32  |  MFC  |  Java  |  Php  |  Delphi  |  Visual Basic  |  .Net  |  Networking  |  General  |  Games  |  Jobs  |  Javascript  |  




Menu

pickSourcecode.com


        

 




 

Cpp > Programs

 

cpp BTree example

#include "btree.hpp"
#include <Windows.h>
#include <iostream.h>
#include <stdlib.h>

//  static  definitions
DiskManager BTree::theDiskManager;

DataFile BTree::theDataFile;

WNJFile BTree::theWNJFile;


int WNJFile::myCount =      0L;
int Page::gPage =           1;
int BTree::NodeIndexCtr =   0;
int BTree::LeafIndexCtr =   0;
int BTree::NodePageCtr =    0;
int BTree::LeafPageCtr =    0;
int BTree::NodeIndexPerPage[Order+1];
int BTree::LeafIndexPerPage[Order+1];

//  prototypes
void parseCommandLines(char *buffer,int argc,char **argv);
void ShowMenu(long*);
void DoFind(char*, BTree&);
void ParseFile(BTree&);

//  driver  program
int main()
{

   BTree myTree;


   
for (int i = 0; i < Order +1; i++)
   {

      BTree::NodeIndexPerPage[i] = 0;

      BTree::LeafIndexPerPage[i] = 0;

   }


   
char buffer[PageSize+1];

   bool fQuit = false;


   while ( !fQuit )

   {

       cout << "?: ";

       cin.getline(buffer,PageSize);


       
if ( buffer[0] == '-' )
       {

           switch (buffer[1])

           {

             case '?':

                DoFind(buffer+2,myTree);

                break;


             case '!':

                   myTree.PrintTree();

                   break;


             case 'F':

             case 'f':

                   ParseFile(myTree);

                   break;

             case '0':

                 fQuit = true;

                 break;

          }

       }

       else

       {

          
if ( myTree.Insert(buffer) )
            cout << "Inserted.\n";

          buffer[0] = '\0';

       }

   }

       return 0;

}


//  having  found  matches,  show  the  menu  of  choices
//  each  entry  is  numbered  and  dated
void ShowMenu(int *list)
{

  
int j=0;
  
char buffer[PageSize+1];
  time_t theTime;

  
int len;
  
char listBuff[256];
  struct tm * ts;

  
int dispSize;

  while (list[j] && j < 20)

  {

     BTree::theDataFile.GetRecord(list[j],buffer,len, theTime);

  dispSize = __min(len,32);

     strncpy(listBuff,buffer,dispSize);

     
if (dispSize == 32)
        {

        listBuff[29] = '.';

        listBuff[30] = '.';

        listBuff[31] = '.';

        }

     listBuff[dispSize]='\0';

     ts = localtime(&theTime);

     cout << "[" << (j+1) << "] ";

     cout << ts->tm_mon << "/";

     cout << ts->tm_mday << "/";

     cout << ts->tm_year << " ";

     cout <<  listBuff << endl;

     j++;

  }

}


//  handle  -?  command
//  find  matches,  show  the  menu,  request  choice
//  display  record  and  redisplay  menu
void DoFind(char * searchString, BTree& myTree)
{


    
//  create  an  array  of  total  set  of  WNJ
    
//  offsets.  This  will  be  used  to  display
    
//  choices  and  to  find  actual  text
    
int  list[PageSize];

    
//  initialize  the  array  to  all  zeros
    
for (int i = 0; i<PageSize; i++)
    list[i] = 0;


    
int k = 0;

    
char * p1 = searchString;
    while (p1[0] == ' ')

        p1++;


    
int offset = myTree.Find(p1);
    
if (offset)
    {

        
//  get  the  array  of  offsets  from  WNJFile
        
int *found  =  BTree::theWNJFile.Find(offset);
        
int j = 0;

        
//  add  any  you  don't  already  have
        
for (;k < PageSize && found[j];j++,k++)
        {

            
for (int l = 0; l < k; l++)
            {

                
if (list[l] == found[j])
                    continue;

            }

            list[k] = found [j];

        }

        delete [] found;

    }


    cout << "\n";


    
if (!list[0])
    {

        cout << "Nothing found.\n";

        return;

    }


    ShowMenu(list);


    
int choice;
    
char buffer[PageSize];
    
int len;
    time_t theTime;


    
for (;;)
    {

        cout << "Choice (0 to stop): " ;

        cin >> choice;

        cin.ignore(PageSize,'\n');

        
if ( choice < 1 )
            break;


        BTree::theDataFile.GetRecord(list[choice-1],buffer,len, theTime);

        cout << "\n>> ";

        cout << buffer;

        cout << "\n\n";

        ShowMenu(list);

    }

}


//  open  a  file  and  create  a  new  note  for  each  line
//  index  every  word  in  the  line
void ParseFile( BTree& myTree)
{


    
char fileName[256];
    cout << "FileName: ";

    cin.getline(fileName,PageSize);


    
char buffer[PageSize];
    
char theString[PageSize];

    ifstream theFile(fileName,ios::in );

    
if (!theFile)
    {

        cout << "Error opening input file " << fileName << endl;

        return;

    }


    
int offset = 0;
    
for (;;)
    {

        theFile.read(theString,PageSize);

        
int len = theFile.gcount();
        
if (!len)
           break;

        theString[len]='\0';

        
char *p1, *p2, *p0;
        p0 = p1 = p2 = theString;


        while (p1[0] && (p1[0] == '\n' || p1[0] == '\r'))

           p1++;


        p2 = p1;


        while (p2[0] && p2[0] != '\n' && p2[0] != '\r')

           p2++;


        
int bufferLen = p2 - p1;
        
int totalLen = p2 - p0;

        
if (!bufferLen)
           continue;


        
//  lstrcpyn(buffer,p1,bufferLen);
        strncpy(buffer,p1,bufferLen);

        buffer[bufferLen]='\0';


        
//  for  (int  i  =  0;  i<  PageSize;  i++)
           cout << "\r";

        cout << "Parsing " << buffer;

        myTree.Insert(buffer);

        offset += totalLen;

        theFile.clear();

        theFile.seekg(offset,ios::beg);

    }

    cout << "\n\nCompleted parsing " << fileName << endl;

}


 
Privacy Policy | About Us