M programs/program2/parse_csv.c => programs/program2/parse_csv.c +2 -2
@@ 16,10 16,10 @@ int parse_csv(const char filename[], course_t courses[MAX_COURSES]) {
strcpy(courses[i].code, strtok(buf, ","));
strcpy(courses[i].name, strtok(NULL, ","));
- // A magical mystery
+ // The so-called "CRN"
courses[i].crn = atoi(strtok(NULL, ","));
- // Seats information
+ // Seat information
courses[i].seats_total = atoi(strtok(NULL, ","));
courses[i].seats_taken = atoi(strtok(NULL, ","));
courses[i].seats_open = atoi(strtok(NULL, ","));
M programs/program2/print_course.c => programs/program2/print_course.c +3 -1
@@ 1,6 1,7 @@
#include "course.h"
#include <stdio.h>
+// Print individual course information
void print_course(course_t course) {
// Numbers are from max size of element + alignment factor
printf("%-36s %9s\n", course.name, course.code);
@@ 8,7 9,8 @@ void print_course(course_t course) {
course.time, course.professor);
}
-void print_courses(unsigned int max_course, course_t courses[MAX_COURSES]) {
+// Cycle through array of courses and print all of them
+void print_courses(unsigned int max_course, course_t courses[]) {
for (unsigned int i = 0; i < max_course; i++) {
print_course(courses[i]);
}
M programs/program2/program2.c => programs/program2/program2.c +7 -8
@@ 31,15 31,15 @@ int main(void) {
pchoice:
switch (choice) {
case 'a': {
- printf("\n");
+ printf("\n"); // Formatting
print_courses(max_course, courses);
break;
}
case 'n': {
char scode[10];
printf("Course code: ");
- scanf(" %[^\n]", scode);
- printf("\n");
+ scanf(" %[^\n]", scode); // scanf code reads until newline
+ printf("\n"); // Formatting
search_course_code(max_course, courses, scode);
break;
}
@@ 47,7 47,7 @@ int main(void) {
char sdays[8];
printf("Weekday combo: ");
scanf(" %[^\n]", sdays);
- printf("\n");
+ printf("\n"); // Formatting
search_course_days(max_course, courses, sdays);
break;
}
@@ 55,18 55,17 @@ int main(void) {
unsigned int sseats;
printf("Max seats: ");
scanf(" %u", &sseats);
- printf("\n");
+ printf("\n"); // Formatting
search_course_upper_bound_seats(max_course, courses, sseats);
break;
}
// HACK Shoddy way of handling newlines
- // Also handles first pass using labels, which work just like
- // assembly
+ // Also handles first pass using labels and manual jumping
case '\n':
case 26: {
choice = getchar(); // Read more input; last input was bogus
if (choice == 'q' || choice == 255) {
- // Exit the program
+ // Exit the program
goto exit;
}
// Process the new input without printing the help message again
M programs/program2/search_course.c => programs/program2/search_course.c +10 -4
@@ 2,16 2,21 @@
#include "print_course.h"
#include <string.h>
-void search_course_code(unsigned int max_course, course_t courses[MAX_COURSES],
+// Search for a course by its code
+void search_course_code(unsigned int max_course, course_t courses[],
char scode[10]) {
+ // Cycle through each course
for (unsigned int i = 0; i < max_course; i++) {
+ // Test if equals with `strcmp`
if (strcmp(courses[i].code, scode) == 0) {
print_course(courses[i]);
}
}
}
-void search_course_days(unsigned int max_course, course_t courses[MAX_COURSES],
+// Search for a course by its weekday occurrences
+// Works the same as `search_course_code(...)`
+void search_course_days(unsigned int max_course, course_t courses[],
char sdays[8]) {
for (unsigned int i = 0; i < max_course; i++) {
if (strcmp(courses[i].weekdays, sdays) == 0) {
@@ 20,9 25,10 @@ void search_course_days(unsigned int max_course, course_t courses[MAX_COURSES],
}
}
+// Search for a course with less than or equal to the provided number
+// of total seats
void search_course_upper_bound_seats(unsigned int max_course,
- course_t courses[MAX_COURSES],
- unsigned int sseats) {
+ course_t courses[], unsigned int sseats) {
for (unsigned int i = 0; i < max_course; i++) {
if (courses[i].seats_total <= sseats) {
print_course(courses[i]);