The class should also have a function named compareTo which compares the Employee object to an Employee object passed to it and returns 0 if they are equal, <0 if the current object is less than the passed object, and >0 if the current object is greater than the passed object. Use the strcmp function to compare the strings. The comparison should compare last names first, then first names (if the last names are equal), and finally ID# (if both names are equal).
Read all the records from the empdata.txt file into the array. Display how many records were read in, then sort the records using your own modified version of the Quicksort algorithm or the Heapsort algorithm.
questions: I'm still having issues with the strmcpy. Typically I have problems with someone and then I figure it out later on. Right now, I'm wondering if I should use what I have for strmcpy or if I should use the variable names instead. (int ln = strcmp(lname, r.lname); int fn = strcmp(fname, r.fname); int idd = strcmp(id, r.id); )
another question: While reading the information in to the arrays from the file, in the while loop, should I include the compareTo function and the quickSort function or one or the other or both? A simple "yes compareTo in loop" or "no compareTo in loop" would suffice. I figured that both would work in the loop due to wanting to compare and quicksort while reading from the file.
#include <iostream>
#include <fstream>
#include <cstring>
using std::cout;
using std::ifstream;
using std::endl;
using std::strcpy;
using std::strcmp;
class Record {
public:
int id;
char fname[20];
char lname[20];
int compareTo(const Record& r) const { //2nd const wont change member variables
if (strcmp(lname, r.lname) == 0)
return r.lname;
if(strcmp(fname, r.fname) == 0)
return r.fname;
if(strcmp(id, r.id) == 0)
return r.id;
if (strcmp(lname, r.lname) <0 )
return 0;
if (strcmp(lname, r.lname) >0)
return 0;
}
};
void quickSort(Record a[], int left, int right);
int main(void) {
Record recs[120000];
long numrecs = 0;
char linein[60];
char* ptr;
ifstream ifile("/examples/empdata.txt");
if (!ifile) {
cout << "Could not open input file\n\n";
return 1;
}
ifile >> linein;
while (ifile) {
//grab first field
ptr = strtok(linein, ":");
if (ptr==NULL) { return 2; }
recs[numrecs].id = atoi(ptr);
//grab second field
ptr = strtok(NULL, ":");
if (ptr==NULL) { return 2; }
strcpy(recs[numrecs].fname, ptr);
//grab third field
ptr = strtok(NULL, ":");
if (ptr==NULL) { return 2; }
strcpy(recs[numrecs].lname, ptr);
//see if any more fields exist
ptr = strtok(NULL, ":");
if (ptr!=NULL) { return 3; }
numrecs++;
ifile >> linein;
// compareTo();
// quickSort(work, 0, ARRAY_SIZE-1);
}
cout << "Total records: " << endl; //recs[numrecs] << endl;
cout << " ID# Name" << endl;
cout <<"------ ---------------" << endl;
for (int i=0; i<15; i++) {
cout << recs[i].id << ": " << recs[i].lname << ", " << recs[i].fname << endl;
}
ifile.close();
return 0;
}
void quickSort(Record a[], int left, int right) {
int pivotNdx, l_hold = left, r_hold = right;
Record pivot = a;
while (left < right) {
while ((a.compareTo(pivot)>=0) && (left < right))
right--;
if (left != right) a[left++] = a;
while ((a.compareTo(pivot)<=0) && (left <
right)) left++;
if (left != right) a[right--] = a;
}
a = pivot;
pivotNdx = left;
left = l_hold;
right = r_hold;
if (left < pivotNdx) quickSort(a, left, pivotNdx-1);
if (right > pivotNdx) quickSort(a, pivotNdx+1, right);
}