//******************************************************************
// IMPLEMENTATION FILE (reclist.cpp)
// This file implements the RecordList class member functions.
// List representation: an array of pointers to PersonnelData
// structs and an integer variable giving the current length
// of the list
//******************************************************************
#include "reclist.h"
#include
#include // For NULL
using namespace std;
// Private members of class:
// PersonPtr ptrList[MAX_EMPL]; Array of pointers
// int length; Number of valid pointers
// in ptrList
void GetRecord( ifstream&, PersonPtr ); // Prototype for "helper"
// function
//******************************************************************
RecordList::RecordList()
// Default constructor
// Postcondition:
// length == 0
{
length = 0;
}
//******************************************************************
RecordList::~RecordList()
// Destructor
// Postcondition:
// Structs pointed to by ptrList[0..length-1]
// are no longer on the free store
{
int index; // Loop control variable
for (index = 0; index < length; index++)
delete ptrList[index];
}
//******************************************************************
void GetRecord( /* inout */ ifstream& inFile,
/* in */ PersonPtr aPerson )
// Reads one record from file inFile
// Precondition:
// inFile is open for input
// && aPerson points to a valid PersonnelData struct
// Postcondition:
// IF input of the lastName member failed due to end-of-file
// The contents of *aPerson are undefined
// ELSE
// All members of *aPerson are filled with the values
// for one person read from inFile
{
getline(inFile, aPerson->lastName);
if ( !inFile )
return;
getline(inFile, aPerson->firstName);
getline(inFile, aPerson->address.street);
getline(inFile, aPerson->address.city);
getline(inFile, aPerson->address.state);
getline(inFile, aPerson->workHistory);
getline(inFile, aPerson->education);
getline(inFile, aPerson->payrollData);
}
//******************************************************************
void RecordList::ReadAll( /* inout */ ifstream& inFile )
// Precondition:
// inFile has been opened for input
// && inFile contains at most MAX_EMPL records
// Postcondition:
// ptrList[0..length-1] point to dynamic structs
// containing values read from inFile
{
PersonPtr aPerson; // Pointer to newly input record
aPerson = new PersonnelData;
GetRecord(inFile, aPerson);
while (inFile)
{
ptrList[length] = aPerson; // Store pointer into array
length++;
aPerson = new PersonnelData;
GetRecord(inFile, aPerson);
}
}
//******************************************************************
void RecordList::SelSort()
// Sorts ptrList so that the pointed-to structs are in
// ascending order of last name
// Precondition:
// ptrList[0..length-1] point to valid PersonnelData structs
// Postcondition:
// ptrList contains the same values as ptrList@entry, rearranged
// so that consecutive elements of ptrList point to structs in
// ascending order of last name
{
PersonPtr tempPtr; // Used for swapping
int passCount; // Loop control variable
int searchIndx; // Loop control variable
int minIndx; // Index of minimum so far
for (passCount = 0; passCount < length - 1; passCount++)
{
minIndx = passCount;
// Find the index of the pointer to the alphabetically first
// last name remaining in ptrList[passCount..length-1]
for (searchIndx = passCount + 1; searchIndx < length;
searchIndx++)
if (ptrList[searchIndx]->lastName
< ptrList[minIndx]->lastName)
minIndx = searchIndx;
// Swap ptrList[minIndx] and ptrList[passCount]
tempPtr = ptrList[minIndx];
ptrList[minIndx] = ptrList[passCount];
ptrList[passCount] = tempPtr;
}
}
//******************************************************************
void RecordList::PrintAll()
// Precondition:
// ptrList[0..length-1] point to valid PersonnelData structs
// Postcondition:
// Contents of the structs pointed to by ptrList[0..length-1]
// have been written to standard output
{
int index; // Loop control variable
for (index = 0; index < length; index++)
{
cout << ptrList[index]->lastName << ", "
<< ptrList[index]->firstName << endl;
cout << ptrList[index]->address.street << endl;
cout << ptrList[index]->address.city << ", "
<< ptrList[index]->address.state << endl;
cout << ptrList[index]->workHistory << endl;
cout << ptrList[index]->education << endl;
cout << ptrList[index]->payrollData << endl << endl;
}
}