/* * This is an example program to introduce some of the basic features * of C. * * CSCI 315 * Fall 2013 * * Xiannong Meng * August 2013 */ #include #include #include /* one can define 'global' data types, variables, and constants here */ #define MAX_STRLEN 80 // maximum length of an unknown string #define WRD_LENGTH 32 // per word #define MAX_COURSE 5 // number of courses per student #define FALSE 0 #define TRUE 1 /* the following is a C structure to hold course information */ struct course_info { char course[WRD_LENGTH];// in the form of 'CSCI 315', 8 chars, plus '\0' char * title; // variable length string char * prof; // professor name, variable length char time[WRD_LENGTH];// in the form of 'MWF 08:00-09:00', 16 chars, plus '\0' char * room; // classroom, variable length float credit; // 1.0, 0.5, 0.25, etc. int enrollment; // number of students allowed in class }; void read_a_record(FILE * f, struct course_info * a_course) { int len; fgets(a_course->course, WRD_LENGTH, f); // read course len = strlen(a_course->course); a_course->course[len-1] = 0; // terminate the string a_course->title = (char*)malloc(MAX_STRLEN + 1); fgets(a_course->title, MAX_STRLEN, f); // read course title len = strlen(a_course->title); a_course->title[len-1] = 0; // terminate the string a_course->prof = (char*)malloc(MAX_STRLEN + 1); 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 a_course->room = (char*)malloc(MAX_STRLEN + 1); 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 an array. * * 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 course_info * read_data(char * fname) { FILE * f = fopen(fname, "r"); // open data file for reading int count; // number of courses int i; // loop index fscanf(f, "%d", &count); // read the number of records fgetc(f); // we first allocate the memory for the array of courses struct course_info * courses = (struct course_info *)malloc((count + 1)* sizeof(struct course_info)); // we now read the courses into this array, one at a time for (i = 0; i < count; i ++) { read_a_record(f, &courses[i]); } strcpy(courses[count].course, "END"); // sentinal value for the last return courses; } /* * Print a single record. Assume both parameters are valid. * * Parameters: * courses : an array of structures containing course information. * i : the index of the array item (course) to be printed. */ void print_a_record(struct course_info * courses, int i) { printf("course %d:\n", i); printf(" course %s %s\n", courses[i].course, courses[i].title); printf(" professor %s\n", courses[i].prof); printf(" date and time %s\n", courses[i].time); printf(" loaction %s\n", courses[i].room); printf(" credit %3.1f\n", courses[i].credit); printf(" enrollment %d\n", courses[i].enrollment); } /* * Print the entire array of the course structures. * * Version 1: print an array of records (structures) with known size * * Parameters: * courses : an array of structures containing course information. * count : the total number of records in the array */ void print_records(struct course_info * courses, int count) { int i; printf("--- Course information ---\n"); for (i = 0; i < count; i ++) { print_a_record(courses, i); } } /* * Print the entire array of the course structures. * * Version 2: print an array of records (structures) without a known size * * Parameters: * courses : an array of structures containing course information. */ void print_records_v2(struct course_info * courses) { int i; // loop index i = 0; printf("--- Course information ---\n"); while (strncmp(courses[i].course, "END", 3) != 0) { // 'END', a sentenal value print_a_record(courses, i); i ++; // increment i } } /* * Search for a record that contains 'query' in any place. * * Parameters: * query: the query to search for * courses: the array of courses * size: the number of courses in the array * * Returns: * The index of the record containing the query, if found, -1 otherwise. */ int found(char * query, struct course_info * courses, int size ) { int i = 0; int found = FALSE; while (i < size && found == FALSE) { if (strstr(courses[i].course, query) != NULL || strstr(courses[i].title, query) != NULL || strstr(courses[i].prof, query) != NULL || strstr(courses[i].room, query) != NULL || strstr(courses[i].time, query) != NULL) found = TRUE; else i ++; } if (found == TRUE) return i; else return -1; } /* * 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 course_info * courses, int count) { char query[MAX_STRLEN]; int 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, count); if (i != -1) { printf("record found with match ...\n"); print_a_record(courses, 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 course_info * my_courses; // read course information from a file my_courses = read_data("course-data.txt"); // print what we have read // print_records(my_courses, 5); // print with a known size print_records_v2(my_courses); // print without a known size do_search(my_courses, 5); return 0; }