/* * This is an example program to introduce some of the basic features * of C, including linked list. * * CSCI 315 * Fall 2013 * * Xiannong Meng * August 2013 */ #include "common.h" /* * Print a single record. Assume both parameters are valid. * * Parameters: * course : the course to be printed */ void print_a_record(struct course_info * a_course) { printf(" course %s %s\n", a_course->course, a_course->title); printf(" professor %s\n", a_course->prof); printf(" date and time %s\n", a_course->time); printf(" loaction %s\n", a_course->room); printf(" credit %3.1f\n", a_course->credit); printf(" enrollment %d\n", a_course->enrollment); } /* * Print the entire list of the course structures. * * Parameters: * courses : a list of structures containing course information. */ void print_records(struct list * courses) { struct course_info * a_course; int i = 0; printf("--- Course information ---\n"); a_course = courses->head; while (a_course != NULL) { // traverse through the list printf("---- course %d ----\n", i++); print_a_record(a_course); a_course = a_course->next; // move onto next one } } /* * Read a single record from file into the course structure */ void read_record(FILE *f, struct course_info ** a_record) { int len; struct course_info * a_course = *(a_record); fgets(a_course->title, MAX_STRLEN, f); // read course title len = strlen(a_course->title); a_course->title[len-1] = 0; // terminate the string fgets(a_course->prof, MAX_STRLEN, f); // read professor name len = strlen(a_course->prof); a_course->prof[len-1] = 0; // terminate the string fgets(a_course->time, WRD_LENGTH, f); // read time and day of the course len = strlen(a_course->time); a_course->time[len-1] = 0; // terminate the string // read the room information fgets(a_course->room, MAX_STRLEN, f); len = strlen(a_course->room); a_course->room[len-1] = 0; // terminate the string // read course credit, note the & fscanf(f, "%f", &(a_course->credit)); // read course enrollment, note the & fscanf(f, "%d", &(a_course->enrollment)); fgetc(f); // move over the '\n' char that's left by 'scanf()' } /* * Read course information from a file and return them as a linked list * * The format of the input file is as follows * count: an integer indicating how many courses in this file, followed by * the 'count' number of course structures. Each has the following form * course: an 8-char sequence in the form of "CSCI 315" * title: a variable length string, e.g., "Operating Systems Design" * prof: a variable length string, e.g., "Professor Meng" * time: a 16-char string in the form of 'MWF 08:00-09:00' * room: a variable length string, e.g., "Breakiron 066" * credit: a floating point number for course credit, 1.0, 0.5, 0.25 etc * enrollment: an integer indicating the maximum of students * * Parameters: * char * fname: name of the file where the information is stored * Return: * an array of structures, each of which holds information for one course */ struct list * read_data(char * fname) { FILE * f = fopen(fname, "r"); // open data file for reading int count; // number of courses int i; // loop index int len; // string length struct list * course_list; // the list holding the courses struct course_info * a_course; // information for one course char * course_num = malloc(WRD_LENGTH + 1); course_list = (struct list *)malloc(sizeof(struct list)); fscanf(f, "%d", &count); // read the number of records fgetc(f); course_num = fgets(course_num, WRD_LENGTH, f); // read one course number i = 0; while (course_num != NULL || strncmp(course_num, "END", 3) == 0) { // more len = strlen(course_num); course_num[len-1] = 0; // terminate the string a_course = make_node(); // create a course sturcture read_record(f, &a_course); // read the rest of the structure strcpy(a_course->course, course_num); // copy course number just read // insert the newly read course into the course list insert(a_course, &course_list); // read next course number course_num = fgets(course_num, WRD_LENGTH, f); if (course_num == NULL) break; } return course_list; } /* * Search for a record that contains 'query' in any place. * * Parameters: * query: the query to search for * courses: the list of courses * * Returns: * The pointer to the record containing the query, if found, NULL otherwise. */ struct course_info * found(char * query, struct list * courses) { struct course_info * a_course; int found = FALSE; a_course = courses->head; while (a_course != NULL && found == FALSE) { if (strstr(a_course->course, query) != NULL || strstr(a_course->title, query) != NULL || strstr(a_course->prof, query) != NULL || strstr(a_course->room, query) != NULL || strstr(a_course->time, query) != NULL) { found = TRUE; break; } a_course = a_course->next; } if (found == TRUE) return a_course; else return NULL; } /* * Perform search on the array of the records. * * Parameters: * courses: the array of course structures * count: the number of records in the array */ void do_search(struct list * courses) { char query[MAX_STRLEN]; struct course_info * i; printf("Enter a search term as a single word ('END' to quit) : "); scanf("%s", query); while (strncmp(query, "END", 3) != 0) { i = found(query, courses); if (i != NULL) { printf("record found with match ...\n"); print_a_record(i); } else printf("record not found ...\n"); printf("Enter a search term as a single word ('END' to quit) : "); scanf("%s", query); } } int main(int argc, char* argv[]) { // declare an array of dynamic size struct list * my_courses; printf("reading data\n"); // read course information from a file my_courses = read_data("course-data.txt"); printf("printing data\n"); // print what we have read print_records(my_courses); printf("doing search\n"); // do search do_search(my_courses); return 0; }