#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[50];
char number[20];
int mathGrade;
int englishGrade;
} Student;
typedef struct Node {
Student data;
struct Node* next;
} Node;
void addStudent(Node** head, Student newStudent) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = newStudent;
newNode->next = *head;
*head = newNode;
}
void deleteStudent(Node** head, char* number) {
Node* current = *head;
Node* previous = NULL;
while (current != NULL) {
if (strcmp(current->data.number, number) == 0) {
if (previous == NULL) {
*head = current->next;
} else {
previous->next = current->next;
}
free(current);
return;
}
previous = current;
current = current->next;
}
printf("Student with number %s not found.\n", number);
}
void editStudent(Node* head, char* number, Student newStudent) {
Node* current = head;
while (current != NULL) {
if (strcmp(current->data.number, number) == 0) {
current->data = newStudent;
return;
}
current = current->next;
}
printf("Student with number %s not found.\n", number);
}
void searchByName(Node* head, char* name) {
Node* current = head;
while (current != NULL) {
if (strcmp(current->data.name, name) == 0) {
printf("Name: %s\n", current->data.name);
printf("Number: %s\n", current->data.number);
printf("Math Grade: %d\n", current->data.mathGrade);
printf("English Grade: %d\n", current->data.englishGrade);
return;
}
current = current->next;
}
printf("Student with name %s not found.\n", name);
}
void searchByNumber(Node* head, char* number) {
Node* current = head;
while (current != NULL) {
if (strcmp(current->data.number, number) == 0) {
printf("Name: %s\n", current->data.name);
printf("Number: %s\n", current->data.number);
printf("Math Grade: %d\n", current->data.mathGrade);
printf("English Grade: %d\n", current->data.englishGrade);
return;
}
current = current->next;
}
printf("Student with number %s not found.\n", number);
}
int countStudentsInGradeRange(Node* head, int minGrade, int maxGrade) {
int count = 0;
Node* current = head;
while (current != NULL) {
if (current->data.mathGrade >= minGrade && current->data.mathGrade <= maxGrade) {
count++;
}
current = current->next;
}
return count;
}
void sortStudents(Node* head, int sortBy) {
int swapped;
Node* current = head;
Node* last = NULL;
if (head == NULL) {
return;
}
do {
swapped = 0;
current = head;
while (current->next != last) {
if (sortBy == 0 && strcmp(current->data.number, current->next->data.number) > 0) {
Student temp = current->data;
current->data = current->next->data;
current->next->data = temp;
swapped = 1;
} else if (sortBy == 1 && current->data.mathGrade > current->next->data.mathGrade) {
Student temp = current->data;
current->data = current->next->data;
current->next->data = temp;
swapped = 1;
} else if (sortBy == 2 && current->data.englishGrade > current->next->data.englishGrade) {
Student temp = current->data;
current->data = current->next->data;
current->next->data = temp;
swapped = 1;
}
current = current->next;
}
last = current;
} while (swapped);
}
void saveToFile(Node* head, const char* filename) {
FILE* file = fopen(filename, "w");
Node* current = head;
while (current != NULL) {
fprintf(file, "%s,%s,%d,%d\n", current->data.name, current->data.number,
current->data.mathGrade, current->data.englishGrade);
current = current->next;
}
fclose(file);
}
void loadFromFile(Node** head, const char* filename) {
FILE* file = fopen(filename, "r");
if (file) {
char line[100];
while (fgets(line, sizeof(line), file)) {
Student temp;
sscanf(line, "%[^,],%[^,],%d,%d", temp.name, temp.number,
&temp.mathGrade, &temp.englishGrade);
addStudent(head, temp);
}
fclose(file);
} else {
printf("Failed to open file.\n");
}
}
void printMenu() {
printf("\n-- Student Information and Grade Management System --\n");
printf("\nPlease select an option:\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Edit student\n");
printf("4. Search by name\n");
printf("5. Search by number\n");
printf("6. Grade statistics\n");
printf("7. Sort students\n");
printf("8. Save to file\n");
printf("9. Load from file\n");
printf("0. Exit\n");
printf("\nEnter your choice: ");
}
int main() {
Node* head = NULL;
int choice;
do {
printMenu();
scanf("%d", &choice);
switch (choice) {
case 1: {
Student newStudent;
printf("Enter name: ");
scanf("%s", newStudent.name);
printf("Enter number: ");
scanf("%s", newStudent.number);
printf("Enter math grade: ");
scanf("%d", &newStudent.mathGrade);
printf("Enter english grade: ");
scanf("%d", &newStudent.englishGrade);
addStudent(&head, newStudent);
break;
}
case 2: {
char number[20];
printf("Enter student number to delete: ");
scanf("%s", number);
deleteStudent(&head, number);
break;
}
case 3: {
char number[20];
Student newStudent;
printf("Enter student number to edit: ");
scanf("%s", number);
printf("Enter new name: ");
scanf("%s", newStudent.name);
printf("Enter new number: ");
scanf("%s", newStudent.number);
printf("Enter new math grade: ");
scanf("%d", &newStudent.mathGrade);
printf("Enter new english grade: ");
scanf("%d", &newStudent.englishGrade);
editStudent(head, number, newStudent);
break;
}
case 4: {
char name[50];
printf("Enter name to search: ");
scanf("%s", name);
searchByName(head, name);
break;
}
case 5: {
char number[20];
printf("Enter number to search: ");
scanf("%s", number);
searchByNumber(head, number);
break;
}
case 6: {
int minGrade, maxGrade;
printf("Enter minimum grade: ");
scanf("%d", &minGrade);
printf("Enter maximum grade: ");
scanf("%d", &maxGrade);
int count = countStudentsInGradeRange(head, minGrade, maxGrade);
printf("Number of students in grade range: %d\n", count);
break;
}
case 7: {
int sortBy;
printf("Sort by:\n");
printf("0. Number\n");
printf("1. Math grade\n");
printf("2. English grade\n");
printf("Enter your choice: ");
scanf("%d", &sortBy);
sortStudents(head, sortBy);
printf("Students sorted.\n");
break;
}
case 8: {
char filename[50];
printf("Enter filename to save: ");
scanf("%s", filename);
saveToFile(head, filename);
printf("Data saved to file.\n");
break;
}
case 9: {
char filename[50];
printf("Enter filename to load: ");
scanf("%s", filename);
loadFromFile(&head, filename);
printf("Data loaded from file.\n");
break;
}
case 0:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
printf("\n");
} while (choice != 0);
return 0;
}