A programs/program2/course.h => programs/program2/course.h +16 -0
@@ 0,0 1,16 @@
+#ifndef course_h
+#define course_h
+
+#define MAX_COURSES 30
+
+typedef struct Courses {
+ char code[10];
+ char name[35];
+ unsigned int crn;
+ unsigned int seats_total, seats_taken, seats_open;
+ char professor[25];
+ char weekdays[8];
+ char time[10];
+} course_t;
+
+#endif
A programs/program2/parse_csv.c => programs/program2/parse_csv.c +41 -0
@@ 0,0 1,41 @@
+#include "course.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+// Construct the array of course structs by parsing the csv file
+int parse_csv(const char filename[], course_t courses[MAX_COURSES]) {
+ FILE *csv = fopen(filename, "r");
+ int max_index; // Maximum course index (i.e. # of courses - 1)
+ char buf[150];
+
+ {
+ int i = 0; // Maximum index tracker
+ while (fgets(buf, 150, csv) != NULL) {
+ // Basic information
+ strcpy(courses[i].code, strtok(buf, ","));
+ strcpy(courses[i].name, strtok(NULL, ","));
+
+ // A magical mystery
+ courses[i].crn = atoi(strtok(NULL, ","));
+
+ // Seats information
+ courses[i].seats_total = atoi(strtok(NULL, ","));
+ courses[i].seats_taken = atoi(strtok(NULL, ","));
+ courses[i].seats_open = atoi(strtok(NULL, ","));
+
+ // Misc
+ strcpy(courses[i].professor, strtok(NULL, ","));
+
+ // Parse the schedule descriptor
+ strcpy(courses[i].weekdays, strtok(NULL, " "));
+ strcpy(courses[i].time, strtok(NULL, "\n"));
+
+ i += 1;
+ }
+ max_index = i;
+ }
+
+ fclose(csv);
+ return (max_index);
+}
A programs/program2/parse_csv.h => programs/program2/parse_csv.h +8 -0
@@ 0,0 1,8 @@
+#ifndef parse_csv_h
+#define parse_csv_h
+
+#include "course.h"
+
+int parse_csv(const char filename[], course_t courses[MAX_COURSES]);
+
+#endif
A programs/program2/print_course.c => programs/program2/print_course.c +15 -0
@@ 0,0 1,15 @@
+#include "course.h"
+#include <stdio.h>
+
+void print_course(course_t course) {
+ // Numbers are from max size of element + alignment factor
+ printf("%-36s %9s\n", course.name, course.code);
+ printf("%-3i %-7s %-9s %24s\n\n", course.seats_open, course.weekdays,
+ course.time, course.professor);
+}
+
+void print_courses(unsigned int max_course, course_t courses[MAX_COURSES]) {
+ for (unsigned int i = 0; i < max_course; i++) {
+ print_course(courses[i]);
+ }
+}
A programs/program2/print_course.h => programs/program2/print_course.h +9 -0
@@ 0,0 1,9 @@
+#ifndef print_course_h
+#define print_course_h
+
+#include "course.h"
+
+void print_course(course_t course);
+void print_courses(unsigned int max_course, course_t courses[MAX_COURSES]);
+
+#endif
M programs/program2/program2.c => programs/program2/program2.c +27 -92
@@ 1,109 1,25 @@
+#include "course.h"
+#include "parse_csv.h"
+#include "print_course.h"
+#include "search_course.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#define MAX_COURSES 30
-
/*
* William Culhane
* Program 2, CSCI 112
* Sun May 30 10:27:49 PM MDT 2021
*/
-typedef struct Courses {
- char code[10];
- char name[35];
- unsigned int mystery; // TODO What is this? An ID?
- unsigned int seats_total, seats_taken, seats_open;
- char professor[25];
- char weekdays[8];
- char time[10];
-} course_t;
-
-// Construct the array of course structs by parsing the csv file
-int parse_csv(const char filename[], course_t courses[MAX_COURSES]) {
- FILE *csv = fopen(filename, "r");
- int max_index; // Maximum course index (i.e. # of courses - 1)
- char buf[150];
-
- {
- int i = 0; // Maximum index tracker
- while (fgets(buf, 150, csv) != NULL) {
- // Basic information
- strcpy(courses[i].code, strtok(buf, ","));
- strcpy(courses[i].name, strtok(NULL, ","));
-
- // A magical mystery
- courses[i].mystery = atoi(strtok(NULL, ","));
-
- // Seats information
- courses[i].seats_total = atoi(strtok(NULL, ","));
- courses[i].seats_taken = atoi(strtok(NULL, ","));
- courses[i].seats_open = atoi(strtok(NULL, ","));
-
- // Misc
- strcpy(courses[i].professor, strtok(NULL, ","));
-
- // Parse the schedule descriptor
- strcpy(courses[i].weekdays, strtok(NULL, " "));
- strcpy(courses[i].time, strtok(NULL, "\n"));
-
- i += 1;
- }
- max_index = i;
- }
-
- fclose(csv);
- return (max_index);
-}
-
-void print_course(course_t course) {
- // Numbers are from max size of element + alignment factor
- printf("%-36s %9s\n", course.name, course.code);
- printf("%-3i %-7s %-9s %24s\n\n", course.seats_open, course.weekdays,
- course.time, course.professor);
-}
-
-void print_courses(unsigned int max_course, course_t courses[MAX_COURSES]) {
- for (unsigned int i = 0; i < max_course; i++) {
- print_course(courses[i]);
- }
-}
-
-void search_course_code(unsigned int max_course, course_t courses[MAX_COURSES],
- char scode[10]) {
- for (unsigned int i = 0; i < max_course; i++) {
- if (strcmp(courses[i].code, scode) == 0) {
- print_course(courses[i]);
- }
- }
-}
-
-void search_course_days(unsigned int max_course, course_t courses[MAX_COURSES],
- char sdays[8]) {
- for (unsigned int i = 0; i < max_course; i++) {
- if (strcmp(courses[i].weekdays, sdays) == 0) {
- print_course(courses[i]);
- }
- }
-}
-
-void search_course_upper_bound_seats(unsigned int max_course, course_t courses[MAX_COURSES],
- unsigned int sseats) {
- for (unsigned int i = 0; i < max_course; i++) {
- if (courses[i].seats_total <= sseats) {
- print_course(courses[i]);
- }
- }
-}
-
int main(void) {
course_t courses[MAX_COURSES];
- // Highest course index
+ // Parse csv and find highest course index
unsigned int max_course = parse_csv("classes.csv", courses);
- unsigned char choice = 0;
+ // Initialize choice to signal character
+ unsigned char choice = 26;
do {
printf("Choices:\n"
"a - print all available classes\n"
@@ 112,6 28,7 @@ int main(void) {
"s - print all classes under certain # seats\n"
"q - quit\n");
+ pchoice:
switch (choice) {
case 'a': {
printf("\n");
@@ 142,10 59,28 @@ int main(void) {
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
+ case '\n':
+ case 26: {
+ choice = getchar(); // Read more input; last input was bogus
+ if (choice == 'q' || choice == 255) {
+ // Exit the program
+ goto exit;
+ }
+ // Process the new input without printing the help message again
+ goto pchoice;
+ }
+ default: {
+ fprintf(stderr, "Unrecognized choice\n");
+ }
}
choice = getchar();
- } while (choice != 'q');
+ } while (choice != 'q' && choice != 255);
+ // 255 -> EOF
+exit:
return (0);
}
A programs/program2/search_course.c => programs/program2/search_course.c +31 -0
@@ 0,0 1,31 @@
+#include "course.h"
+#include "print_course.h"
+#include <string.h>
+
+void search_course_code(unsigned int max_course, course_t courses[MAX_COURSES],
+ char scode[10]) {
+ for (unsigned int i = 0; i < max_course; i++) {
+ if (strcmp(courses[i].code, scode) == 0) {
+ print_course(courses[i]);
+ }
+ }
+}
+
+void search_course_days(unsigned int max_course, course_t courses[MAX_COURSES],
+ char sdays[8]) {
+ for (unsigned int i = 0; i < max_course; i++) {
+ if (strcmp(courses[i].weekdays, sdays) == 0) {
+ print_course(courses[i]);
+ }
+ }
+}
+
+void search_course_upper_bound_seats(unsigned int max_course,
+ course_t courses[MAX_COURSES],
+ unsigned int sseats) {
+ for (unsigned int i = 0; i < max_course; i++) {
+ if (courses[i].seats_total <= sseats) {
+ print_course(courses[i]);
+ }
+ }
+}
A programs/program2/search_course.h => programs/program2/search_course.h +16 -0
@@ 0,0 1,16 @@
+#ifndef search_course_h
+#define search_course_h
+
+#include "course.h"
+
+void search_course_code(unsigned int max_course, course_t courses[MAX_COURSES],
+ char scode[10]);
+
+void search_course_days(unsigned int max_course, course_t courses[MAX_COURSES],
+ char sdays[8]);
+
+void search_course_upper_bound_seats(unsigned int max_course,
+ course_t courses[MAX_COURSES],
+ unsigned int sseats);
+
+#endif