Assignment 4 – Strings and Structures
60-141 – Introduction to Programming II Winter, 2017
(Saturday, March 25, 2017: 23:59:59)
Write a C program that uses string processing functions to properly format a Comma Separated Values (csv) file of course Information and store them into an array of structures. Once all data have been formatted and stored, your program should be able to:
a. Add new course
b. Search for course information using either course name or course number as the search key
c. Display all or any course data.
The content of the input file (input.csv) is read by the program using input redirection technique that you already have used in the earlier assignments.
Assume that the CourseInfo structure has been defined as follows:
struct CourseInfo{
int courseID;
char courseName[50];
char courseCode[12];
char Term [6];
};
typedef struct CourseInfo courseInfo; //optional
Example:
Fields in the input file are comma separated as follows: CourseName,Faculty,Subject,Level,Section,Semester,Year
Content of a sample input file:
programming,03,60,141,01,W,2015
programming,03,60,141,30,W,2015
Algebra,03,62,102,02,S,2013
Religion,08,98,938, 20,F,2014
Corresponding field values in the array of structures (after formatting):
Hints:
1- For formatting data, the following points are to be considered:
a. Course ID should be unique sequential integers starting from 1 (not from 0).
b. Course name must be all capital letters (upper case).
c. Course Code should also be unique and store in the format as xxxx-xxx-xx. The first xxxx part
indicates faculty code (xx) and subject code (xx). The second xxx part indicates level and the
last xx part indicates section number. For example 0360-141-30
d. Term should be one character indicating semester (‘S’, ‘F’,’W’) following by 4 digits indicating
a year. For example S2010, W2014.
2- Your program should implement at least the following functions:
a) loadCourseInfo() : To read all data from input file and format and store them in an array of
structures.
b) displayCourseInfo(), To print a table indicating the course Information. For example:
c) addCourseInfo(): To add a new course. Note that:
– CouseID should be unique and generated automatically by your program (last
courseID+1).
– CourseCode is also unique and you cannot have two courses with the same
courseCode. So, before adding a course, search for the courseCode to be sure that
you have not had it previously.
d) searchCourseInfo(): To search for a course information using courseID or course Name and
print the course information if it exists.
Requirements:
– Write and document a complete C program that is capable of satisfying the requirements of this
assignment problem.
Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | /* Title: Assignment#4: Strings and Structures Objective: This program uses string processing functions to properly format a comma Separated Values(csv) file of course information and store them into an array of structures. */ //C-Preprocessor Directives # include <stdio.h> # include <string.h> # include <ctype.h> # include <stdlib.h> struct CourseInfo{ int CourseID; char courseName[50]; char courseCode[12]; char Term[6]; }; typedef struct CourseInfo Course; Course courses[100]; int numCourse = 0; //Function Prototypes //To Read all the data from input file and format and store them in an array of structures void loadCourseInfo(); void displayCourseInfo(); //Prints a table indicating the course information void addCourseInfo(char *, char *, char *); //This function is used to add a new course //This function is used to search for a course information using courseID // or course Name and print the course information if it exists void searchCourseInfo(int); void addNewCourse(); //This function is used to add new course information and store in structure int main (){ int select; loadCourseInfo(); int loop=1; while (loop) { //display the menu options printf( "Please select an option\n" "1 - Add a new course\n" "2 - Search for a course information using course name\n" "3 - Search for a course information using course number\n" "4 - Display all or any course data\n" "0 - Quit\n" ); scanf( "%d" , &select); switch (select){ case 0: loop = 0; break ; case 1: addNewCourse(); break ; case 2: searchCourseInfo(1); break ; case 3: searchCourseInfo(2); break ; case 4: displayCourseInfo(); break ; default : printf( "Invalid selection, please choose a number between 1 and 4, or enter 0 to Quit\n" ); } } return 0; } /* Objective: To Read all the data from input file and format and store them in an array of structures. Input:There are no parameters Output: There is no output */ void loadCourseInfo(){ char *stringToken,row[100],courseCode[12],courseName[50],term[6]; FILE *fPointer; int k = 0; fPointer = fopen ( "input.csv" , "r" ); while ( fscanf (fPointer, "%s\n" , row) != EOF){ // Use "," as the delimiter, and store them in an array of structures stringToken = strtok (row, "," ); while (stringToken != NULL){ switch (k){ case 0: strcpy(courseName, stringToken); break ; case 1: strcpy(courseCode,stringToken); break ; case 2: strcat(courseCode,stringToken); strcat(courseCode, "-" ); break ; case 3: strcat(courseCode,stringToken); strcat(courseCode, "-" ); break ; case 4: strcat(courseCode,stringToken); break ; case 5: strcpy(term, stringToken); break ; case 6: strcat(term, stringToken); break ; } k++; stringToken = strtok (NULL, "," ); } k=0; addCourseInfo(courseName, courseCode, term); } displayCourseInfo(); fclose(fPointer); } /* Objective: Prints a table indicating the course information. Input: no parameters for this function Output: no return for this function */ void displayCourseInfo(){ int i; printf( "\n Content of input.csv file \n\n" ); printf( "\nID Name Code Term \n" ); printf( "_______________________________________ \n" ); for (i = 0; i < numCourse; i++){ //Add 1 to make sure we start from 1 not 0 printf( "%-5d" , (courses[i].CourseID)+1); printf( "%-16s" , courses[i].courseName); printf( "%-12s" , courses[i].courseCode); printf( "%-5s" , courses[i].Term); printf( "\n" ); } printf( "\n\n" ); } /* Objective: This function is used to add a new course Input: cName, cCode and term arrays Output: no return for this function */ void addCourseInfo(char cName[50], char cCode[12], char term[6]){ int j; for (j = 0; j < strlen (cName); j++){ cName[j] = toupper(cName[j]); } strcpy(courses[numCourse].courseName, cName); strcpy(courses[numCourse].courseCode, cCode); strcpy(courses[numCourse].Term, term); numCourse++; courses[numCourse].CourseID = numCourse; } /* Objective: This function is used to search for a course information using courseID or course Name and print the course information if it exists. Input: cName, cCode and term arrays Output: no return for this function */ void searchCourseInfo(int select){ int i, j, m; int searchID; char searchName[50]; int isThere = 0; // Using the course name to find if this course exists if (select == 1) { printf( "\nPlease enter the course name you want to search for\n " ); scanf( "%s" , searchName); for (j = 0; j < strlen (searchName); j++) searchName[j] = toupper(searchName[j]); printf( "\nResults:\n" ); for (i = 0; i < numCourse; i ++){ if ( strcmp (courses[i].courseName, searchName) == 0){ printf( "%-8d" , (courses[i].CourseID)+1); for (m = 0; m < strlen (courses[m].courseName); m++){ courses[i].courseName[m] = toupper(courses[i].courseName[m]); } printf( "%-16s" , courses[i].courseName); printf( "%-16s" , courses[i].courseCode); printf( "%s" , courses[i].Term); printf( "\n" ); isThere++; } } if (isThere == 0){ printf( "%s course was not found.\n" , searchName); } } // Using the course ID to find if this course exists else { printf( "\nPlease enter the course ID you want to look for\n " ); scanf( "%d" , &searchID); printf( "\nResults:\n" ); if (searchID <= numCourse) { printf( "%-8d" , searchID); for (j = 0; j < strlen (courses[searchID-1].courseName); j++){ courses[searchID-1].courseName[j] = toupper(courses[searchID-1].courseName[j]); } printf( "%-16s" , courses[searchID-1].courseName); printf( "%-16s" , courses[searchID-1].courseCode); printf( "%s\n\n" , courses[searchID-1].Term); } else { printf( "Can not find Course ID %d \n" , searchID); } } } /* Objective: This function is used to add new course information and store in structure Input: course code,course Name,term,faculty Code,subject Code,level,section Number,semester and year. Output: no return for this function */ void addNewCourse(){ char courseCode[12],courseName[50],term[6],fCode[2],sCode[2],level[3],sectionNum[2],semester[1],year[4]; int i, duplicate=0; // Accept input from user printf( "\nPlease enter the Course Name: " ); scanf( "%s" , courseName); getchar(); printf( "Please enter the Course code \"xx\": " ); scanf( "%s" ,fCode); getchar(); printf( "Please enter the Course Subject code \"xx\": " ); scanf( "%s" ,sCode); getchar(); printf( "Please enter the Course Level code \"xxx\": " ); scanf( "%s" ,level); getchar(); printf( "Please enter the Course Section code \"xx\": " ); scanf( "%s" ,sectionNum); getchar(); // create course code strcpy(courseCode,fCode); strcat(courseCode,sCode); strcat(courseCode, "-" ); strcat(courseCode,level); strcat(courseCode, "-" ); strcat(courseCode,sectionNum); // Make sure the course code should be unique. for (i = 0; i <numCourse; i++){ if ( strcmp (courses[i].courseCode, courseCode) == 0) duplicate++; } if (duplicate > 0){ printf( "You have a duplicate Course Code \n" ); return ; } printf( "Please enter \"S, F or W\" for the Course Semester : " ); scanf( "%s" , semester); getchar(); semester[0] = toupper(semester[0]); printf( "Please enter the Course Year \"yyyy\": " ); scanf( "%s" , year); getchar(); strcpy(term, semester); strcat(term, year); addCourseInfo(courseName,courseCode,term); } |
Leave a reply