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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
static const string DEFAULT_NAME="Some Name";
static const int DEFAULT_POINTS = 0;
static const int MAX_POINTS = 1000;
int Student::sortKey = Student::SORT_BY_LAST; //I get an error here
class Student
{
private:
string lastName;
string firstName;
int totalPoints;
static int sortKey;
public:
static const int SORT_BY_FIRST = 88;
static const int SORT_BY_LAST = 98;
static const int SORT_BY_POINTS = 108;
static bool setSortKey(int key);
static int getSortKey();
Student::Student( string last, string first, int points); //And I get an error here
Student(void);
~Student(void);
string getLastName();
string getFirstName();
int getTotalPoints();
bool setLastName(string last);
bool setFirstName(string first);
bool setPoints(int pts);
static int compareTwoStudents(Student firstStud, Student secondStud);
string toString();
};
class StudentArrayUtilities
{
private:
static bool swapStudents(Student students[], int top);
static void swap(Student &student1, Student &student2);
public:
StudentArrayUtilities(void);
~StudentArrayUtilities(void);
static string toString(string title, Student students[], int numStudents);
static void arraySort(Student students[], int numStudents);
static double getMedianDestructive(Student students[], int numStudents);
};
Student::Student(void)
{
lastName = DEFAULT_NAME;
firstName = DEFAULT_NAME;
totalPoints = DEFAULT_POINTS;
}
Student::~Student(void)
{
}
Student::Student( string last, string first, int points)
{
if (!setLastName(last))
lastName = DEFAULT_NAME;
if (!setFirstName(first))
firstName = DEFAULT_NAME;
if (!setPoints(points))
totalPoints = DEFAULT_POINTS;
}
bool Student::setLastName(string last)
{
if (last.length() > 0 && isalpha(last[0]))
{
lastName = last;
return true;
}
return false;
}
bool Student::setFirstName(string first)
{
if (first.length() > 0 && isalpha(first[0]))
{
firstName = first;
return true;
}
return false;
}
bool Student::setPoints(int pts)
{
if (pts >= 0 && pts <= MAX_POINTS)
{
totalPoints = pts;
return true;
}
return false;
}
int Student::getSortKey()
{
return sortKey;
}
string Student::getLastName()
{
return this->lastName;
}
string Student::getFirstName()
{
return this->firstName;
}
int Student::getTotalPoints()
{
return this->totalPoints;
}
string Student::toString()
{
ostringstream strFirst, strLast, strPoints;
strFirst << firstName;
strLast << lastName;
strPoints << totalPoints;
return " "+ strLast.str()+ ", " + strFirst.str()+ " points: " + strPoints.str()+ "\n";
}
bool Student::setSortKey(int key)
{
if(key==SORT_BY_FIRST || key==SORT_BY_LAST || key==SORT_BY_POINTS)
{
sortKey = key;
return true;
}
return false;
}
int Student::compareTwoStudents(Student firstStudent, Student secondStudent)
{
switch (sortKey)
{
case SORT_BY_FIRST:
return firstStudent.firstName.compare(secondStudent.firstName);
case SORT_BY_LAST:
return firstStudent.lastName.compare(secondStudent.lastName);
case SORT_BY_POINTS:
return firstStudent.totalPoints - secondStudent.totalPoints;
default:
return 0;
}
}
StudentArrayUtilities::StudentArrayUtilities(void)
{
}
StudentArrayUtilities::~StudentArrayUtilities(void)
{
}
string StudentArrayUtilities::toString(string title, Student students[],int numStudents)
{
string outputString = title + "\n";
for (int i = 0; i < numStudents; i++)
{
outputString += " "+ students[i].toString();
}
return outputString + "\n";
}
void StudentArrayUtilities::arraySort(Student students[], int numStudents)
{
for (int i = 0; i < numStudents; i++)
{
if (!swapStudents(students, numStudents-1-i))
{
return;
}
}
}
bool StudentArrayUtilities::swapStudents(Student students[], int top)
{
bool isSwap = false;
for (int i =0; i < top; i++)
{
if (Student::compareTwoStudents(students[i], students[i+1]) > 0 )
{
swap(students[i], students[i+1]);
isSwap = true;
}
}
return isSwap;
}
void StudentArrayUtilities::swap(Student &student1, Student &student2)
{
Student temp("", "", 0);
temp = student1;
student1 = student2;
student2 = temp;
}
double StudentArrayUtilities::getMedianDestructive(Student students[],int numStudents)
{
if (numStudents < 0)
{
return 0.0;
}
if (numStudents == 1)
{
return students[0].getTotalPoints();
}
int clientSortKey = Student::getSortKey();
Student::setSortKey(Student::SORT_BY_POINTS);
arraySort(students, numStudents);
Student::setSortKey(clientSortKey);
if (numStudents%2 == 0)
{
int middle = numStudents/2;
return (students[middle].getTotalPoints()+ students[middle -1].getTotalPoints()) / 2.0;
}
return students[numStudents/2].getTotalPoints();
}
int main()
{
Student studentClass[] =
{
Student("smith","fred", 95),
Student("bauer","jack",123),
Student("jacobs","carrie", 195),
Student("renquist","abe",148),
Student("zz-error","trevor", 108),
Student("perry","fred",225),
Student("loceff","fred", 44),
Student("stollings","pamela",452),
Student("charters","rodney", 295),
Student("cassar","john",321),
Student("clark","mary",600),
Student("allen","helen",350),
Student("young","donna",450),
Student("parker","deborah",430),
Student("turner","ruth",221)
};
Student studentClass2[] =
{
Student("smith","fred", 95),
Student("bauer","jack",123),
Student("jacobs","carrie", 195),
Student("renquist","abe",148),
Student("zz-error","trevor", 108),
Student("perry","fred",225),
Student("loceff","fred", 44),
Student("stollings","pamela",452),
Student("charters","rodney", 295),
Student("cassar","john",321),
Student("clark","mary",600),
Student("allen","helen",350),
Student("young","donna",450),
Student("parker","deborah",430),
Student("turner","ruth",221),
Student("phillips","kimberly",150)
};
Student smallClass[] =
{
Student("smith","fred", 95)
};
cout << StudentArrayUtilities::toString("Before:", studentClass, 15);
StudentArrayUtilities::arraySort(studentClass, 15);
cout << StudentArrayUtilities::toString("After default sort:",studentClass, 15);
Student::setSortKey(Student::SORT_BY_FIRST);
StudentArrayUtilities::arraySort(studentClass, 15);
cout << StudentArrayUtilities::toString("After sort by first:",studentClass, 15);
Student::setSortKey(Student::SORT_BY_POINTS);
StudentArrayUtilities::arraySort(studentClass, 15);
cout << StudentArrayUtilities::toString("After sort by points:",studentClass, 15);
Student::setSortKey(Student::SORT_BY_FIRST);
int sortKey = Student::getSortKey();
cout << "Median of evenClass: "<< StudentArrayUtilities::getMedianDestructive(studentClass2, 16)<< endl;
if(Student::getSortKey() == sortKey)
{
cout << "Successfully preserved sort key." << endl;
}else
{
cout << "Sort key is changed"<< endl;
}
cout << "Median of oddClass: "<< StudentArrayUtilities::getMedianDestructive(studentClass, 15)<< endl;
cout << "Median of smallClass: "<< StudentArrayUtilities::getMedianDestructive(smallClass, 1)<< endl;
system("pause");
}
|