Write a C program that uses string processing functions to properly format a Com
ID: 3868921 • Letter: W
Question
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. The content of the input file (input.csv) is read by the program using input redirection. Once all data have been formatted and stored, the 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.
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):
CourseID
1
2
3
4
CourseName
PROGRAMMING
PROGRAMMING
ALGEBRA
RELIGION
CourseCode
0360-141-01
0360-141-30
0362-102-02
0898-938-20
Term
W2015
W2015
S2013
F2014
**The program must implement at least these following functions:
loadCourseInfo(): To read all data from input file and format and store them in an array of structures.
displayCourseInfo(): To print a table indicating the course Information. For example:
ID
Name
Code
Term
1
PROGRAMMING
0360-141-01
W2015
2
PROGRAMMING
0360-141-30
W2015
3
ALGEBRA
0362-102-02
S2013
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
searchCourseInfo(): To search for a course information using courseID or course Name and print the course information if it exists.
Note:
* Course ID should be unique sequential integers starting from 1 (not from 0).
* Course name must be all capital letters (upper case).
* 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 0450-231-40
* Term should be one character indicating semester (‘S’, ‘F’,’W’) following by 4 digits indicating a year. For example W2012, F2017
CourseID
1
2
3
4
CourseName
PROGRAMMING
PROGRAMMING
ALGEBRA
RELIGION
CourseCode
0360-141-01
0360-141-30
0362-102-02
0898-938-20
Term
W2015
W2015
S2013
F2014
Explanation / Answer
#include<stdio.h>
#include<string.h>
struct CourseInfo{
int courseID;
char courseName[50];
char courseCode[12];
char Term [6];
};
typedef struct CourseInfo courseInfo; //optional
courseInfo infos[10];
int size = 0;
void loadCourseInfo() {
FILE *fp = fopen("Data.txt","r");
char tmp0[10], tmp1[10], tmp2[10], tmp3[10];
while (!feof(fp)) {
infos[size].courseID = size + 1;
fscanf(fp,"%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^ ] ",infos[size].courseName, infos[size].courseCode, tmp0, tmp1, tmp2, infos[size].Term, tmp3);
strcat(infos[size].courseCode, tmp0);
strcat(infos[size].courseCode, "-");
strcat(infos[size].courseCode, tmp1);
strcat(infos[size].courseCode, "-");
strcat(infos[size].courseCode, tmp2);
strcat(infos[size].Term, tmp3);
printf("%s ", infos[size].courseCode);
size++;
}
}
void displayCourseInfo() {
int i;
for (i = 0; i < size; i++) {
printf("%d %s %s %s ", infos[i].courseID, infos[i].courseName, infos[i].courseCode, infos[i].Term);
}
}
void addCourseInfo() {
int i;
courseInfo tmp;
printf("Enter course name: ");
scanf("%[^ ] ", tmp.courseName);
printf("Enter course Code: ");
scanf("%[^ ] ", tmp.courseCode);
printf("Enter course Term: ");
scanf("%[^ ] ", tmp.Term);
tmp.courseID = size + 1;
for (i = 0; i < size; i++) {
if (strcmp(infos[i].courseCode,tmp.courseCode) == 0) {
printf("Duplicate course code");
return;
}
}
infos[size++] = tmp;
}
void searchCourseInfo() {
int choice, i, ID;
char name[10];
printf(1. Search by course ID 2. Search by course name ")
scanf("%d ", &choice);
switch(choice) {
case 1:
printf("Enrer ID: ");
scanf("%d ", &ID);
for (i = 0; i < size; i++)
if (infos[i].courseID == ID)
printf("%d %s %s %s ", infos[i].courseID, infos[i].courseName, infos[i].courseCode, infos[i].Term);
break;
case 2:
printf("Enter name: ");
scanf("%[^ ] ",name);
for (i = 0; i < size; i++)
if (strcmp(infos[i].courseName, name) == 0)
printf("%d %s %s %s ", infos[i].courseID, infos[i].courseName, infos[i].courseCode, infos[i].Term);
break;
}
}
int main() {
loadCourseInfo();
printf("print ");
displayCourseInfo();
}
Do you need more features?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.