1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
class Person
{
public:
string givnName; // The person's given name
string lastName; // The person's last name
string birth; // The person's date of birth
int year; // The person's birth year
int id; // The person's individual identifier
// Default constructor for a Person
Person()
{
year = -1;
id = -1;
}
// Assignment operator for a Person, copying the data in the
// right-hand side to the data of the left.
Person & operator = (const Person & rhs)
{
this->givnName = rhs.givnName;
this->lastName = rhs.lastName;
this->birth = rhs.birth;
this->year = rhs.year;
this->id = rhs.id;
return *this;
}
bool operator > (const Person & rhs) const
{
// Set up booleans to determine who has what data. If it is
// not empty, the bool will be labeled as "true".
bool gn = !givnName.empty();
bool rgn = !rhs.givnName.empty(); // Still segfaults...
bool ln = !lastName.empty();
bool rln = !rhs.lastName.empty();
bool b = !birth.empty();
bool rb = !rhs.birth.empty();
// Convert all names we are handling to have a capital first
// letter.
string tempgn;
string temprgn;
string templn;
string temprln;
if (gn == true)
{
tempgn = givnName;
tempgn = capitalize(tempgn);
}
if (rgn == true)
{
temprgn = rhs.givnName;
temprgn = capitalize(temprgn);
}
if (ln == true)
{
templn = lastName;
templn = capitalize(templn);
}
if (rln == true)
{
temprln = rhs.lastName;
temprln = capitalize(temprln);
}
// If the left person has a last name, but not the right...
if (ln == true && rln == false)
{
// The left is larger.
return true;
}
// If the left person does not have a last name, but the right
// does...
else if (ln == false && rln == true)
{
// The right is larger.
return false;
}
// If both the left person and the right person have last names,
// and they are not equivalent...
else if (ln == true && rln == true && templn != temprln)
{
// Is the last name greater than the right name? If so,
// the left is larger. Otherwise, the right is.
if (templn > temprln)
return true;
else
return false;
}
// If the left and right don't have last names or the
// last names are equal, then check given names.
// If the left has a given name but not the right...
else if (gn == true && rgn == false)
{
// The left is larger.
return true;
}
// If the left is void of a given name, but not the right...
else if (gn == false && rgn == true)
{
// The right is larger.
return false;
}
// If both people have given names, and they are not the same...
else if (gn == true && rgn == true && tempgn != temprgn)
{
// Is the left person's given name greater than the right's
// given name? If so, the left is larger. Otherwise, the right
// is larger.
if (tempgn > temprgn)
return true;
else
return false;
}
// If the left and right don't have given names or the
// given names are equal, then check dates of birth.
// If the left has a birth date but not the right...
else if (b == true && rb == false)
{
// The left is larger.
return true;
}
// If the right has a borth date but not the left...
else if (b == false && rb == true)
{
// The right is larger.
return false;
}
// If both the left and right both have borth dates and they
// are not equal to each other...
else if (b == true && rb == true && birth != rhs.birth)
{
// Check to see if the borth YEAR of the left
// is greater than than that of the right. If it is,
// then the left is larger. Otherwise, the right
// is larger.
if (year > rhs.year)
return true;
else
return false;
}
// At this point, check to see if the left's id is greater than
// the right's id. If it is...
else if (id > rhs.id)
{
// The left is larger.
return true;
}
// If it's not...
else
{
// The right is larger.
return false;
}
// If all else fails, just claim the larger is the left.
return true;
}
// Reset the Person to defaults.
void clear()
{
givnName.clear();
lastName.clear();
birth.clear();
year = -1;
id = -1;
}
}
|