CS 250 Computer Programming and Problem Solving - FALL 1998 

[ Home | Syllabus | Notes | Glossary | CS250 WEB Version Home Page ]


Searching a List

 


//*********************************
// PersonArray: adapted from p248.txt - LongArray Class Definition
// features overload to file I/O for list objects
// Also make GrowBy a private function (from second test)
//
// Programmer: Chris Wild
//********************************
#include <iostream.h>
#include <fstream.h>
#include "person.h"

class PersonArray {
   friend ofstream & operator <<(ofstream & outFile, const PersonArray & theArray);
   friend ifstream & operator >>(ifstream & iStream, PersonArray & theArray);
public:
  PersonArray( int sz = 0);
  // Construct an array of size sz

  PersonArray( const PersonArray & L );
  // Copy constructor.

  ~PersonArray();
  // Destructor.

  int GetSize() const;
  // Return the current allocation size.

  Person Get( int i ) const;
  // Retrieve element at index position i.

  void Put( int i, Person elt );
  // Insert element at index position i.
  // grow array if necessary

private:
  Person * data;   // ptr to array containing elements
  int size; // current allocation size
  void GrowBy( int n );
  // Increase the allocation size by n elements.
};


PersonArray::PersonArray( int sz)
{
  size = sz;
  data = new Person[size];
}

ofstream & operator <<(ofstream & outFile, const PersonArray & theArray)
{
   outFile << theArray.size << endl; // first record how many object will follow in the file
   for(int i = 0; i < theArray.size; i++) {
      outFile << theArray.data[i];
   }
   return outFile;
}
 

ifstream & operator >>(ifstream & inFile, PersonArray & theArray)
{
   inFile >> theArray.size;
   inFile.ignore(1);
   for(int i = 0; i < theArray.size; i++) {
      inFile >> theArray.data[i];
   }
   return inFile;
}

//*******************************
// Test program to demonstrate the following:
//    1) overload of file I/O for Lists
//    2) Search for a element in a list by overloading
//       "==" operator in various ways.
//       I show several overloads to show it can be done
//       They are encapulated in the different FindByXXX functions
//    3) shows how to use the same file for output and input
//    4) return value from main program (can be tested by shell script)
// Uses an array (which you can't do for your project
// but the principles are the same
//
// Programmer: Chris Wild
//********************************

#include <fstream.h>
#include <iostream.h>
#include "Person.h"
#include "PersonArray.h"

const int N_PERSONS = 2;

// prototypes
bool operator ==(int age, const Person & thePerson);
bool operator ==(const FixedString & SSN, const Person & thePerson);
int main()
{
        Person someBody;
        PersonArray people(N_PERSONS); // create a list of peopls
        ifstream inFile;
        ofstream outFile;
        int i;

        // First create a bunch of people
        outFile.open("people.txt");
        if(!outFile) {
                cerr << " could not open file people.txt for output\n";
                return 1;
        }
        for(i = 0 ; i < N_PERSONS; i++) {
                cin >> someBody; // get somebody
                people.Put(i,someBody);
        }

        // Then write list out to file.
        outFile << people; // write list out to file.
        outFile.close();

        // Now create a new list from this file
        PersonArray people2(N_PERSONS);
        inFile.open("people.txt");
        inFile >> people2;
        for(i=0; i< people2.GetSize(); i++) {
                 someBody = people2.Get(i);
                 cout << i << ": " << someBody;
                 }

        // now search - first for age
        // then by SSN
        FixedString testSSN("000-00-0000");
        for(i=0; i< people2.GetSize(); i++) {
                 someBody = people2.Get(i);
                 if(51 == someBody)
                       cout << "matched age: " << someBody;
                 if(testSSN == someBody)
                            cout << "matched SSN:" << someBody;
        }
        
        return 0;
}


bool operator ==(int age, const Person & thePerson)
{
   return (age == thePerson.GetAge());
}
bool operator ==(const FixedString & SSN, const Person & thePerson)
{
   return (SSN == thePerson.GetSSN());
}


// Source Code here

Copyright chris wild 1998.
For problems or questions regarding this website contact [Chris Wild (e-mail:wild@cs.odu.edu].
Last updated: November 17, 1998.