Slide 8.2: Evaluation of a binary search
Slide 8.4: Book_1.h
Home

Sequential_1.cpp


The code below is the implementation of the main function of a sequential search. It is basically modified from the implementation on the Slide 6.2. This version is more object-oriented and easy-to-read. However, they are still doing the same thing: a sequential search.

 ~wenchen/public_html/cgi-bin/351/week8/Sequential_1.cpp 
#include  <fstream>
#include  <iostream>
#include  <cstring>
#include  <cstdio>
  using namespace  std;

#include  "Book_1.h"
#include  "Overload.h"
int  main( int argc, char* argv[ ] ) {
  fstream  file;
  Book     b;
  int      number, length, count = 1;
  char     title[129], buffer[129];
  strcpy( title, argv[1] );
  file.open( "books.txt", fstream::in );
  file >> number >> length;
  while ( true ) {
    file >> b;
    if ( strlen( b.GetValue( 1, buffer ) ) == 0 ) {
      printf( "<br /><h3>No such book: <em>%s</em></h3>", title );
      break;
    }
    else {
      cout << count++ << " comparisons; checking title: ";
      cout << b.GetValue( 1, buffer ) << "<br />";
      if ( strcmp( title, buffer ) == 0 ) {
        b.PutResults( );
        break;
      }
    }
  }
  file.close( );
}