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
|
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::ios;
#include <iomanip>
using std::setw;
#include <cstring>
#include <cstdlib>
//create class course
class course //create a class named course with 4 elements
{
public:
char classDes[11]; // course designation ex. COMSC-165, accomodate 10 characters
char term[7]; // term ex. FA2010
int units; //units
char grade; //grade
course* next; //prepare your class to be used in a linked list by adding a next pointer
};
//prototypes
void dispCourse(course*);
int myStricmp(const char*, const char*);
int myStrnicmp(const char*, const char*, size_t count);
int courseCmp(const course*, const course*);
int main()
{
//variables
char classPrompt;
char buf[100];
course* head, *temp = 0; //initially empty list
/*
course* temp, *head = 0; //initially empty list
head = new course;
head -> next = 0;
temp = head;
*/
while(true)
{
//Prompt user to enter class
dispCourse(head);
cout << "Would you like to add a class to the course list? (Y/N): ";
cin >> classPrompt;
cin.ignore(1000, 10);
if (classPrompt == 'n' || classPrompt == 'N') //if user input N break
{
cout << endl << "Thank you for entering your courses" << endl;
break;
}
else //if user doesn't break..
{
//prompt user
temp = new course;
cout << "Please enter the course name: ";
cin.getline(temp -> classDes, 11);
cout << "Please enter the course term: ";
cin.getline(temp -> term, 7);
cout << "Please enter # of units: ";
cin >> buf; (temp -> units) = atoi(buf);
cin.ignore(1000, 10);
cout << "Please enter your grade: ";
cin >> temp -> grade;
cin.ignore(1000, 10);
cout << endl;
//If head is empty create list
if(head == 0)
{
head = temp;
head->next = 0;
}
//compare/arange in order
else
{
course* x = head;
if (courseCmp(x, temp) == 1)
{
while (courseCmp(x, temp) == 1)
{
if (x->next != 0 && courseCmp(x->next, temp) != -1)
x = x->next;
else
break;
}
temp->next = x->next;
x->next = temp;
}
else
{
temp->next = x;
head = temp;
}
}
}
}
//deallocate memory
while (head)
{
course* n = head->next;
delete head;
head = n;
}
}
void dispCourse(course* p)
{
cout.setf(ios::right, ios::adjustfield);
cout << setw(10) << "Course" << setw(10) << "Term" << setw(10) << "Units" << setw(10) << "Grade" << endl;
cout << setw(10) << "-----" << setw(10) << "-----" << setw(10) << "-----" << setw(10) << "-----" << endl;
for(course* i = p; i;i = i->next)
{
cout.setf(ios::right, ios::adjustfield);
cout << setw(10) <<*i->classDes <<
setw(10) << i->term <<
setw(10) << i->units <<
setw(10) << i->grade <<
endl;
}
}
int courseCmp(const course* a, const course* b)
{
//validate the length of the strings
if ((strlen(a->term) != 6) || (strlen(b->term) != 6)) //...in cstring
return myStricmp(a->classDes, b->classDes);
//handle ties here
if (myStricmp(a->term, b->term) == 0)
return myStricmp(a->classDes, b->classDes);
//compare the year
int yearA = atoi(a->term + 2); //atoi is found...
int yearB = atoi(b->term + 2); //...in cstdlib
if (yearA < yearB)
return -1; //termA comes first
if (yearA > yearB)
return 1; //termB comes first
//compare semester in case of same year
if (myStrnicmp(a->term, "SP", 2) == 0)
return -1; // termA comes first
if (myStrnicmp(a->term, "SU", 2) == 0)
return myStrnicmp(b->term, "SP", 2) ? -1 : 1;
return 1;
}
int myStricmp(const char* dst, const char* src)
{
int f, l;
do
{
if (((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z')) f -= 'A' - 'a';
if (((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z')) l -= 'A' - 'a';
} while (f && (f == l));
return(f - l);
}
int myStrnicmp(const char* dst, const char* src, size_t count)
{
int f, l;
do
{
if (((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z')) f -= 'A' - 'a';
if (((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z')) l -= 'A' - 'a';
} while (--count && f && (f == l));
return (f - l);
}
|