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
|
#include "Course.h"
#include <cctype>
#include <cstring>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::ios;
#include <iomanip>
using std::setprecision;
using std::setw;
int myStricmp(const char* dst, const char* src)
{
int f, l;
do
{
if ((tolower(f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z')) f -= 'A' - 'a';
if ((tolower(l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z')) l -= 'A' - 'a';
}
while (tolower(f && (f == l)));
return(f - l);
}
int myStrnicmp(const char* dst, const char* src, int count)
{
int f, l;
do
{
if ((tolower(f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z')) f -= 'A' - 'a';
if ((tolower(l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z')) l -= 'A' - 'a';
}
while (--count && f && (f == l));
return (f - l);
}
void Course::setName(char enteredName[])
{
strcpy(name, enteredName);
}
void Course::setTerm(char enteredTerm[])
{
strcpy(term, enteredTerm);
}
void Course::setUnits(int enteredUnits)
{
units = enteredUnits;
}
void Course::setGrade(char enteredGrade)
{
grade = enteredGrade;
}
void Course::printCourse() const
{
cout.setf(ios::left, ios::adjustfield);
cout << setw(12) << name << setw(10) << term << setw(7) << units << grade;
cout << endl;
}
bool Course::compareNames(Course a, Course b)
{
bool result = false;
if ( strcmp( a.name , b.name ) < 0 ) result = true;
return result;
}
bool Course::compareTerms(Course a, Course b)
{
bool result = false;
if ( strcmp( a.term , b.term ) > 0) result = true;
return result;
}
bool Course::compareUnits(Course a, Course b)
{
bool result = false;
if ( a.units > b.units ) result = true;
return result;
}
bool Course::compareGrades(Course a, Course b)
{
bool result = false;
if ( tolower(a.grade) < tolower(b.grade) ) result = true;
return result;
}
bool Course::CourseCmp ( const Course a, const Course b)
{
// validate string length
if ( ( strlen( a.term ) != 6) || ( strlen( b.term ) != 6 ) )
return (myStricmp( a.name, b.name ));
// handle ties
if ( myStricmp( a.term, b.term ) == 0 )
return ( myStricmp( a.name, b.name ));
// compare the years
int yearA = atoi( a.term + 2);
int yearB = atoi (b.term + 2);
if( yearA < yearB )
return true; // termA comes first
if( yearA > yearB )
return false; // termB comes first
// compare semesters in case of same year
if( myStrnicmp( a.term, "SP", 2 ) == 0 )
return true;
if(myStrnicmp( a.term, "SU", 2 ) == 0 )
return myStrnicmp( b.term, "SP", 2 ) ? true : false;
return false;
}
|