is this code good?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// filename global var
char studentfile[31];
char coursefile[31];
char enrollmentfile[31];
// define student structure
typedef struct {
int id;
char *firstName;
char *lastName;
int dayOfBirth;
int monthOfBirth;
int yearOfBirth;
} Student;
// course struct
typedef struct {
char id[51];
char name[51];
float credit;
} Course;
// enrollment struct
typedef struct {
char courseId[51];
int studentId;
char semester[51];
float score;
} Enrollment;
// Function declarations
Student *loadStudents(int *numStudents);
Course *loadCourses(int *numCourses);
Enrollment *loadEnrollment(int *numEnrollments);
void search_students(Student *students, int numStudents, Course *courses,
int numCourses, Enrollment *enrollments,
int numEnrollments, char *choice, char *lastName, int id);
void search_course(Course *courses, int numCourses, Enrollment *enrollments,
int numEnrollments, Student *students, int numStudents,
char *choice, char *id, char *semester);
void add_course(Course **courses, int *numCourses, char *courseId,
char *courseName, float credit);
void count_course(Enrollment *enrollments, int numEnrollments, Course *courseSSs,
int numCourses);
void count_semester(Enrollment *enrollments, int numEnrollments);
void sort_students(Student *students, int numStudents);
void free_memory(Student *students, Course *courses, Enrollment *enrollments,
int numStudents);
void count_students(Student *students, int numStudents);
void loadFiles(char *studentfile, char *coursefile, char *enrollmentfile,
Student **students, Course **courses, Enrollment **enrollments,
int *numStudents, int *numCourses, int *numEnrollments);
int printFiles(char *choice, Student *students, Course *courses,
Enrollment *enrollments, int numStudents, int numCourses,
int numEnrollments);
void runFunction(Student *students, Course *courses, Enrollment *enrollments,
int numStudents, int numCourses, int numEnrollments);
// ok so i literally just took everything from my main function, and threw it in
// 3 separate functions so I can get the 3 function credit.
int main() {
Student *students;
Course *courses;
Enrollment *enrollments;
int numStudents, numCourses, numEnrollments;
loadFiles(studentfile, coursefile, enrollmentfile, &students, &courses,
&enrollments, &numStudents, &numCourses, &numEnrollments);
runFunction(students, courses, enrollments, numStudents, numCourses,
numEnrollments);
return 0;
}
// load students from a file
Student *loadStudents(int *numStudents) {
FILE *file = fopen(studentfile, "r"); // calling global var
fscanf(file, "%d", numStudents); // read students
Student *students =
malloc(*numStudents * sizeof(Student)); // allocate mem for students
for (int i = 0; i < *numStudents; i++) {
// max string length is 50
students[i].firstName = malloc(51 * sizeof(char));
students[i].lastName = malloc(51 * sizeof(char));
// read students details
fscanf(file, "%d %s %s %d %d %d", &students[i].id, students[i].firstName,
students[i].lastName, &students[i].dayOfBirth,
&students[i].monthOfBirth, &students[i].yearOfBirth);
}
fclose(file); // close file
return students;
}
//loads the courses files
Course *loadCourses(int *numCourses) {
FILE *file = fopen(coursefile, "r"); // call global var n load from file
fscanf(file, "%d", numCourses); // read sstudents
Course *courses = malloc(*numCourses * sizeof(Course)); // allocate mem
for (int i = 0; i < *numCourses; i++) {
// read course details
fscanf(file, "%s %s %f", courses[i].id, courses[i].name,
&courses[i].credit);
}
// close file
fclose(file);
return courses;
}
//loads the enrollment files
Enrollment *loadEnrollment(int *numEnrollments) {
FILE *file = fopen(enrollmentfile, "r"); // call global var and load from file
fscanf(file, "%d", numEnrollments); // read students
Enrollment *enrollments =
malloc(*numEnrollments * sizeof(Enrollment)); // allocate mem
for (int i = 0; i < *numEnrollments; i++) {
// read enrollment details
fscanf(file, "%s %d %s %f", enrollments[i].courseId,
&enrollments[i].studentId, enrollments[i].semester,
&enrollments[i].score);
}
// close file
fclose(file);
return enrollments;
}
//Searches through the Students
void search_students(Student *students, int numStudents, Course *courses,
int numCourses, Enrollment *enrollments,
int numEnrollments, char *choice, char *lastName, int id) {
int found = 0; // flag variable
if (strcmp(choice, "lname") == 0) { // searching by last name, if a last name
// is found it'll print the details.
for (int i = 0; i < numStudents; i++) {
if (strcmp(students[i].lastName, lastName) == 0) {
printf("%d %s %s %d/%d/%d\n", students[i].id, students[i].firstName,
students[i].lastName, students[i].monthOfBirth,
students[i].dayOfBirth,
students[i].yearOfBirth); // then it loops through the
// enrollments to print out the course
// details if the student id matches.
for (int j = 0; j < numEnrollments; j++) {
if (enrollments[j].studentId == students[i].id) {
for (int k = 0; k < numCourses; k++) {
if (strcmp(enrollments[j].courseId, courses[k].id) == 0) {
printf("%s %s %s %.0f ", enrollments[j].courseId,
courses[k].name, enrollments[j].semester,
enrollments[j].score);
// grades framework (thinking of making a separate function for
// the grades)
if (enrollments[j].score >= 90) {
printf("A\n");
} else if (enrollments[j].score >= 80) {
printf("B\n");
} else if (enrollments[j].score >= 70) {
printf("C\n");
} else if (enrollments[j].score >= 60) {
printf("D\n");
} else {
printf("F\n");
}
}
}
}
}
printf("------------------\n");
found = 1;
}
}
if (found == 0) {
printf("not found\n------------------\n");
}
// same idea as the previous search, except searching by id.
} else if (strcmp(choice, "id") == 0) {
for (int i = 0; i < numStudents; i++) {
if (students[i].id == id) {
printf("%d %s %s %d/%d/%d\n", students[i].id, students[i].firstName,
students[i].lastName, students[i].monthOfBirth,
students[i].dayOfBirth, students[i].yearOfBirth);
for (int j = 0; j < numEnrollments; j++) {
if (enrollments[j].studentId == students[i].id) {
for (int k = 0; k < numCourses; k++) {
if (strcmp(enrollments[j].courseId, courses[k].id) == 0) {
printf("%s %s %s %.0f ", enrollments[j].courseId,
courses[k].name, enrollments[j].semester,
enrollments[j].score);
if (enrollments[j].score >= 90) {
printf("A\n");
} else if (enrollments[j].score >= 80) {
printf("B\n");
} else if (enrollments[j].score >= 70) {
printf("C\n");
} else if (enrollments[j].score >= 60) {
printf("D\n");
} else {
printf("F\n");
}
}
}
}
}
printf("------------------\n");
found = 1;
}
}
if (found == 0) {
printf("not found\n------------------\n");
}
}
}
//Searches through the courses (CID or Semester)
void search_course(Course *courses, int numCourses, Enrollment *enrollments,
int numEnrollments, Student *students, int numStudents,
char *choice, char *id, char *semester) {
int found = 0; // flag
// searching by course id
if (strcmp(choice, "cid") == 0) {
// looping through all courses, if the course id matches, then print
for (int i = 0; i < numCourses; i++) {
if (strcmp(courses[i].id, id) == 0) {
printf("%s %.0f\n", courses[i].name, courses[i].credit);
for (int j = 0; j < numEnrollments;
j++) { // loops through enrollment, if the course id and student id
// matches then print student details
if (strcmp(enrollments[j].courseId, id) == 0) {
for (int k = 0; k < numStudents; k++) {
if (enrollments[j].studentId == students[k].id) {
printf("%s %s %s %.0f ", students[k].firstName,
students[k].lastName, enrollments[j].semester,
enrollments[j].score);
// grades framework
if (enrollments[j].score >= 90) {
printf("A\n");
} else if (enrollments[j].score >= 80) {
printf("B\n");
} else if (enrollments[j].score >= 70) {
printf("C\n");
} else if (enrollments[j].score >= 60) {
printf("D\n");
} else {
printf("F\n");
}
}
}
}
}
printf("------------------\n");
found = 1;
}
}
if (found == 0) {
printf("not found\n------------------\n");
}
// similar logic, except semester is also a parameter, so it adds one extra
// layer of logic
} else if (strcmp(choice, "cid_semester") == 0) {
for (int i = 0; i < numCourses; i++) {
if (strcmp(courses[i].id, id) == 0) {
printf("%s %.0f\n", courses[i].name, courses[i].credit);
for (int j = 0; j < numEnrollments; j++) {
if (strcmp(enrollments[j].courseId, id) == 0 &&
strcmp(enrollments[j].semester, semester) == 0) {
for (int k = 0; k < numStudents; k++) {
if (enrollments[j].studentId == students[k].id) {
printf("%s %s %s %.0f ", students[k].firstName,
students[k].lastName, enrollments[j].semester,
enrollments[j].score);
// grades framework
if (enrollments[j].score >= 90) {
printf("A\n");
} else if (enrollments[j].score >= 80) {
printf("B\n");
} else if (enrollments[j].score >= 70) {
printf("C\n");
} else if (enrollments[j].score >= 60) {
printf("D\n");
} else {
printf("F\n");
}
}
}
}
}
printf("------------------\n");
found = 1;
}
}
if (found == 0) {
printf("not found\n------------------\n");
}
}
}
//add courses
void add_course(Course **courses, int *numCourses, char *courseId,
char *courseName, float credit) {
// Check if the course id already exists
for (int i = 0; i < *numCourses; i++) {
if (strcmp((*courses)[i].id, courseId) == 0) {
printf("Violation of course id uniqueness. Cannot add the record.\n");
return;
}
}
// Open the file in append mode and add the new course
FILE *file = fopen(coursefile, "a");
fprintf(file, "\n%s %s %.2f", courseId, courseName, credit);
fclose(file);
// Free the memory for the current courses array
free(*courses);
// Reload the file
*courses = loadCourses(numCourses);
printf("Record added\n");
}
//Counts the enrollments in each course.
void count_course(Enrollment *enrollments, int numEnrollments, Course *courses,
int numCourses) {
// loops through courzses and then enrollments, and if the enrollment courseid
// matches the current course id, then it ups the counter.
for (int i = 0; i < numCourses; i++) {
int count = 0;
for (int j = 0; j < numEnrollments; j++) {
if (strcmp(courses[i].id, enrollments[j].courseId) == 0) {
count++;
}
}
// the print of the courses
printf("%s %d\n", courses[i].id, count);
}
printf("------------------\n");
}
// i followed the psuedocode professor ahmed gave us for this (counting each unique sem, views student distro )
void count_semester(Enrollment *enrollments, int numEnrollments) {
// so i creat ea count of students for each semester, then i create a flag
// variable for the length of the list. i also have a 2d array to store the
// unique semesters.
char completed_list[numEnrollments][51];
int student_count[numEnrollments];
int tracker = 0;
int found = 0;
// if semester exists in completed list, then it increases that.
for (int i = 0; i < numEnrollments; i++) {
for (int j = 0; j < tracker; j++) {
if (strcmp(completed_list[j], enrollments[i].semester) == 0) {
student_count[j]++;
found = 1;
break;
}
}
// if not found, the print the count of students for each semester.
if (!found) {
strcpy(completed_list[tracker], enrollments[i].semester);
student_count[tracker] = 1;
tracker++;
}
found = 0;
}
// the print statement for everything.
for (int i = 0; i < tracker; i++) {
printf("%s %d\n", completed_list[i], student_count[i]);
}
printf("------------------\n");
}
// Function to sort students and print them
void sort_students(Student *students, int numStudents) {
// Bubble sort
for (int i = 0; i < numStudents - 1; i++) { // loop for each element
for (int j = 0; j < numStudents - i - 1;
j++) { // loop for the opposing elements
if (students[j].id >
students[j + 1].id) { // if the current elemnt is greater than the
// next one then itll do the swap
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
// Print the sorted students
for (int i = 0; i < numStudents; i++) {
printf("%d %s %s %d/%d/%d\n", students[i].id, students[i].firstName,
students[i].lastName, students[i].monthOfBirth,
students[i].dayOfBirth, students[i].yearOfBirth);
}
printf("------------------\n");
}
// memory freeing function
void free_memory(Student *students, Course *courses, Enrollment *enrollments,
int numStudents) {
// Free the allocated memory for students
for (int i = 0; i < numStudents; i++) {
free(students[i].firstName);
free(students[i].lastName);
}
free(students);
free(courses);
free(enrollments);
}
// literally just printing out the total students
void count_students(Student *students, int numStudents) {
printf("total students %d\n", numStudents);
printf("------------------\n");
}
// Additional Function #1 (Loading the files (students3, stuff like that))
void loadFiles(char *studentfile, char *coursefile, char *enrollmentfile,
Student **students, Course **courses, Enrollment **enrollments,
int *numStudents, int *numCourses, int *numEnrollments) {
// Inputs
scanf("%s", studentfile);
scanf("%s", coursefile);
scanf("%s", enrollmentfile);
// Load the files properly
*students = loadStudents(numStudents);
*courses = loadCourses(numCourses);
*enrollments = loadEnrollment(numEnrollments);
}
// Additional Function #2 (Scanf and Prints go here)
int printFiles(char *choice, Student *students, Course *courses,
Enrollment *enrollments, int numStudents, int numCourses,
int numEnrollments) {
char lastName[51];
int id;
char courseId[51];
char courseName[51];
float credit;
char semester[51];
// menu oriented program. Rather then how i originally had it, it will do all
// the scanf's in here, rather than in the search() functions, leaving the
// search functions with the logic. (I talked with Anna about this specific
// logic of nested if else's)
if (strcmp(choice, "search_students") == 0) {
// printf("Debug Statement");
printf("==================\n------------------\n");
scanf("%s", choice);
if (strcmp(choice, "lname") == 0) {
// printf("Debug Statement");
scanf("%s", lastName);
search_students(students, numStudents, courses, numCourses, enrollments,
numEnrollments, choice, lastName, 0);
} else if (strcmp(choice, "id") == 0) {
// printf("Debug Statement");
scanf("%d", &id);
search_students(students, numStudents, courses, numCourses, enrollments,
numEnrollments, choice, "", id);
}
} else if (strcmp(choice, "search_course") == 0) {
// printf("Debug Statement");
printf("==================\n------------------\n");
scanf("%s", choice);
if (strcmp(choice, "cid") == 0) {
// printf("Debug Statement");
scanf("%s", courseId);
search_course(courses, numCourses, enrollments, numEnrollments, students,
numStudents, choice, courseId, "");
} else if (strcmp(choice, "cid_semester") == 0) {
// printf("Debug Statement");
scanf("%s", courseId);
scanf("%s", semester);
search_course(courses, numCourses, enrollments, numEnrollments, students,
numStudents, choice, courseId, semester);
}
// rather than messing with \n characters, it looks for count, and then
// looks for the second word choice, rather than the full word.
} else if (strcmp(choice, "count") == 0) {
// printf("Debug Statement");
scanf("%s", choice);
if (strcmp(choice, "students") == 0) {
printf("==================\n------------------\n");
count_students(students, numStudents);
} else if (strcmp(choice, "students_course") == 0) {
printf("==================\n------------------\n");
count_course(enrollments, numEnrollments, courses, numCourses);
} else if (strcmp(choice, "students_semester") == 0) {
printf("==================\n------------------\n");
count_semester(enrollments, numEnrollments);
}
} else if (strcmp(choice, "add_course") == 0) {
// printf("Debug Statement");
scanf("%s %s %f", courseId, courseName, &credit);
printf("==================\n------------------\n");
add_course(&courses, &numCourses, courseId, courseName, credit);
} else if (strcmp(choice, "sort") == 0) {
// printf("Debug Statement");
printf("==================\n------------------\n");
sort_students(students, numStudents);
// printf("Debug Statement");
} else if (strcmp(choice, "exit") == 0) {
printf("==================\n------------------\nThank you!\n");
free_memory(students, courses, enrollments, numStudents);
}
}
// Additional Function #3 (While eloop for my scanf's and prints to run in)
void runFunction(Student *students, Course *courses, Enrollment *enrollments,
int numStudents, int numCourses, int numEnrollments) {
char choice[100];
while (1) {
scanf("%s", choice);
if (printFiles(choice, students, courses, enrollments, numStudents, numCourses,
numEnrollments)) {
break;
}
}
}