/** * student-experiments * Some experiments with students. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include // For printf, stdout, and such #include "student.h" // Duh. #include "student-sort.h" // For our sorting routine #include "student-compare.h" // For our comparators // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * Print out a horizontal rule of a particular length. */ static void hrule (int len) { int i; for (i = 0; i < len; i++) putchar ('-'); putchar ('\n'); } // hrule /** * Print out an array of students */ static void print_students (struct student *students[], int num_students) { int i; for (i = 0; i < num_students; i++) { printf ("%2d: ", i); student_print (students[i], stdout); printf ("\n"); } // for each student } // print_students // +------+----------------------------------------------------------- // | Main | // +------+ int main () { // Set up a group of sample students. struct student *class[] = { student_new ("John", "Doe", "00001234", 3.4, 0), student_new ("John", "Ode", "10203040", 2.0, 100000), student_new ("Jane", "Doe", "00001233", 3.9, 1005000), student_new ("Jack", "Doe", "00112233", 3.2, 234012), student_new ("Jan", "Edo", "20199999", 2.7, 123450), student_new ("Sam", "Are", "20199998", 1.5, 50), student_new ("Sam", "IAm", "20199908", 3.5, 543210), student_new ("Jane", "IAm", "20199908", 3.5, 543210) }; int num_students = sizeof (class) / sizeof (struct student *); // Print them out. hrule (80); print_students (class, num_students); // Sort them by name sort_students (class, num_students, compare_students_by_name); hrule (80); printf ("SORTED BY NAME\n"); printf ("--------------\n"); print_students (class, num_students); // Sort them by student id // Sort them by GPA // Sort them by debt // And we're done. return 0; } // main