home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / pascal / 5172 < prev    next >
Encoding:
Internet Message Format  |  1992-09-02  |  2.3 KB

  1. Path: sparky!uunet!news.claremont.edu!ucivax!news.service.uci.edu!nntpsrv
  2. From: answer@orion.oac.uci.edu (Reference)
  3. Subject: B-search
  4. Nntp-Posting-Host: orion.oac.uci.edu
  5. Message-ID: <2AA5120A.2223@news.service.uci.edu>
  6. Newsgroups: comp.lang.pascal
  7. Organization: University of California, Irvine
  8. Lines: 71
  9. Date: 2 Sep 92 19:12:10 GMT
  10.  
  11. Hi,
  12.  
  13. Being more or less self taught at TP, I figure I sometimes do stuff the hard
  14. way, out of ignorance.  Most of the time my way works fine, however I'm 
  15. now confronted where my way doesn't work.
  16.  
  17. What I've got here is a binary search procedure that's supposed to read a list
  18. name from a sorted binary file, and then return the e-mail address for that 
  19. name.  It works great, except for the last record, which it is unable to 
  20. read (say the last name "Zebra" for instance).  
  21.  
  22. I know that the person "Zebra" is there, I can see it when I read the binary 
  23. file, I just can't get to it when I do my binary search.
  24.  
  25. The code listed below always thinks the second to last record of the sorted 
  26. file is the last record.  I could make sure I pad the file with some dummy 
  27. name such as "ZZZZZZ", but that's pretty lame.
  28.  
  29. Any suggestions?
  30.  
  31. Tony
  32.  
  33. Anthony Toyofuku
  34. Main Library.
  35. University of California,
  36. Irvine 
  37.  
  38.  
  39. ----------------------------CUT HERE------------------------------
  40.  
  41.  
  42.  
  43.  
  44. type
  45.    string15 = string[15];
  46.    string40 = string[40];
  47.    AddRecord = record                     
  48.      LastName  : string15;
  49.      FirstName : string15;
  50.      Add   : string40;
  51.    end;
  52.    FileBin = file of AddRecord;
  53.  
  54. Procedure BSearch(WantedAddress : string;
  55.                   var Person : AddRecord; var Found : boolean);
  56.  
  57. var
  58.   First, Last, Middle : byte;
  59.   File1               : Filebin;
  60.  
  61. begin
  62.   Assign(File1, FILENAME);            { FILENAME is declared as a  }
  63.   reset(File1);                       { constant "address.bin".    }
  64.   First := 1;
  65.   Last := FileSize(File1);
  66.   Found := false;
  67.   while (First <= Last) AND (NOT Found) do
  68.     begin
  69.       Middle := (First + Last) div 2;
  70.       seek(File1, Middle - 1);        { I think my problems lie here } 
  71.       read(File1, Person);
  72.       if WantedAddress = Person.LastName then
  73.         Found := true
  74.       else if WantedAddress < Person.LastName then
  75.         Last := Middle - 1
  76.       else
  77.         First := Middle + 1;
  78.     end;
  79.   close(File1);
  80. end;
  81.  
  82.