#include "CRUD.h"
#include <string.h>
extern char *studentFile;
extern char *courseFile;
extern char *scoreFile;
extern Student *students;
extern Course *courses;
extern Score *scores;
extern int studentCount;
extern int courseCount;
extern int scoreCount;
void addStudent()
{
printf("Add Student\n");
Student student;
printf("Enter Student ID: ");
scanf("%d", &student.id);
printf("Enter Student Name: ");
scanf("%s", student.name);
printf("Enter Student Gender(M/F):");
getchar();
char c;
scanf("%c", &c);
student.gender = isMale(c);
printf("Enter Student Birthday(dd/mm/yyyy): ");
scanf("%d/%d/%d", &student.birthday.day, &student.birthday.month,
&student.birthday.year);
// memory
students = (Student *)realloc(students, sizeof(Student) * (studentCount + 1));
students[studentCount++] = student;
// disk
FILE *fp = fopen(studentFile, "a");
fprintf(fp, "%d,%s,%d,%d/%d/%d", student.id, student.name, student.gender,
student.birthday.day, student.birthday.month, student.birthday.year);
fclose(fp);
}
void editStudent()
{
int id;
printf("Enter Student ID: ");
scanf("%d", &id);
int index = getStudentIndexByID(id);
if (index == -1)
{
printf("Student Not Found\n");
return;
}
printf("Edit Student\n");
Student student;
printf("What do you want to edit?\n");
printf("1. Name\n");
printf("2. Gender\n");
printf("3. Birthday\n");
int choice;
scanf("%d", &choice);
char c;
switch (choice)
{
case 1:
printf("Enter Student Name: ");
scanf("%s", student.name);
break;
case 2:
printf("Enter Gender(M/F): ");
getchar();
scanf("%c", &c);
student.gender = isMale(c);
break;
case 3:
printf("Enter Student Birthday(dd/mm/yyyy): ");
scanf("%d/%d/%d", &student.birthday.day, &student.birthday.month,
&student.birthday.year);
break;
default:
printf("Invalid Choice\n");
return;
}
// memory
students[index] = student;
// disk
saveStudent();
}
void deleteStudent()
{
int id;
printf("Enter Student ID: ");
scanf("%d", &id);
int index = getStudentIndexByID(id);
if (index == -1)
{
printf("Student Not Found\n");
return;
}
// memory
for (int i = index; i < studentCount - 1; ++i)
students[i] = students[i + 1];
students = (Student *)realloc(students, sizeof(Student) * (studentCount - 1));
studentCount--;
// disk
saveStudent();
}
void searchStudent()
{
Student studentInput;
Student *studentOutput;
int outputCount = 0;
printf("You want to search a student by...\n");
printf("1. ID\n");
printf("2. Name\n");
printf("3. Gender\n");
printf("4. Birthday\n");
int choice;
scanf("%d", &choice);
switch (choice)
{
case 1:
studentOutput = searchStudentByID(&studentInput);
outputCount = 1;
break;
case 2:
studentOutput = searchStudentByName(&studentInput, &outputCount);
break;
case 3:
studentOutput = searchStudentByGender(&studentInput, &outputCount);
break;
case 4:
studentOutput = searchStudentByBirthday(&studentInput, &outputCount);
break;
default:
printf("Invalid Choice\n");
return;
}
if (studentOutput == NULL)
{
printf("Student Not Found\n");
return;
}
printf("Student Found\n");
for (int i = 0; i < outputCount; ++i)
printStudent(&(studentOutput[i]));
}
int getStudentIndexByID(int id)
{
for (int i = 0; i < studentCount; i++)
if (students[i].id == id)
return i;
return -1;
}
Student *searchStudentByID(Student *student)
{
int id = student->id;
int index = getStudentIndexByID(id);
if (index == -1)
return NULL;
return &(students[index]);
}
Student *searchStudentByName(Student *student, int *outputCount)
{
char *name = student->name;
Student *output;
for (int i = 0; i < studentCount; ++i)
if (strcmp(students[i].name, student->name) == 0)
{
output = (Student *)realloc(output, sizeof(Student) * (*outputCount + 1));
output[(*outputCount)++] = students[i];
}
return output;
}
Student *searchStudentByGender(Student *student, int *outputCount)
{
bool gender = student->gender;
Student *output;
for (int i = 0; i < studentCount; ++i)
{
if (students[i].gender == gender)
{
output = (Student *)realloc(output, sizeof(Student) * (*outputCount + 1));
output[(*outputCount)++] = students[i];
}
}
return output;
}
Student *searchStudentByBirthday(Student *student, int *outputCount)
{
Birthday birthday = student->birthday;
Student *output;
for (int i = 0; i < studentCount; ++i)
{
if (students[i].birthday.day == birthday.day &&
students[i].birthday.month == birthday.month &&
students[i].birthday.year == birthday.year)
{
output = (Student *)realloc(output, sizeof(Student) * (*outputCount + 1));
output[(*outputCount)++] = students[i];
}
}
return output;
}
bool isMale(char c)
{
if (c == 'M' || c == 'm')
return true;
else if (c == 'F' || c == 'f')
return false;
else
{
printf("Invalid Gender\n");
return false;
}
}
void loadStudent()
{ // load student from file
FILE *fp = fopen(studentFile, "r");
if (fp == NULL)
return;
Student student;
char buf[1024];
char *res;
char *b;
while (fscanf(fp, "%s", buf) != EOF)
{
res = strtok(buf, ",");
student.id = atoi(res);
res = strtok(NULL, ",");
strcpy(student.name, res);
res = strtok(NULL, ",");
if (strcmp(res, "1") == 0)
student.gender = true;
else
student.gender = false;
res = strtok(NULL, ",");
b = strtok(res, "/");
student.birthday.day = atoi(b);
b = strtok(NULL, "/");
student.birthday.month = atoi(b);
b = strtok(NULL, "/");
student.birthday.year = atoi(b);
students = (Student *)realloc(students, sizeof(Student) * (studentCount + 1));
students[studentCount++] = student;
}
}
void saveStudent()
{
FILE *fp = fopen(studentFile, "w");
Student student;
for (int i = 0; i < studentCount; ++i)
{
student = students[i];
fprintf(fp, "%d,%s,%d,%d/%d/%d", student.id, student.name, student.gender,
student.birthday.day, student.birthday.month, student.birthday.year);
if (i != studentCount - 1)
fprintf(fp, "\n");
}
fclose(fp);
}
void printStudent(Student *student)
{
printf("ID: %d\n", student->id);
printf("Name: %s\n", student->name);
printf("Gender: ");
if (student->gender)
printf("Male\n");
else
printf("Female\n");
printf("Birthday: %d/%d/%d\n", student->birthday.day, student->birthday.month, student->birthday.year);
}
void printStudentList()
{
for (int i = 0; i < studentCount; ++i)
printStudent(&(students[i]));
}