You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
6.2 KiB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char id[20];
char clas[20];
char name[20];
double score1, score2, score3, total, average;
} Student;
void displayMenu();
void inputStudents(Student students[], int *count);
void outputStudents(Student students[], int count);
void sortStudents(Student students[], int count);
void insertStudent(Student students[], int *count);
void deleteStudent(Student students[], int *count);
void modifyStudent(Student students[], int count);
void queryStudent(Student students[], int count);
int main() {
Student students[100];
int count = 0;
char option;
while (1) {
displayMenu();
printf("Please input your option: ");
scanf(" %c", &option);
switch (option) {
case '1':
inputStudents(students, &count);
break;
case '2':
deleteStudent(students, &count);
break;
case '3':
queryStudent(students, count);
break;
case '4':
sortStudents(students, count);
outputStudents(students, count);
break;
case '5':
outputStudents(students, count);
break;
case '6':
printf("Exiting system...\n");
return 0;
default:
printf("Invalid option. Try again.\n");
}
}
return 0;
}
void displayMenu() {
printf("\n1. Input\n");
printf("2. Delete\n");
printf("3. Select\n");
printf("4. Order\n");
printf("5. Output\n");
printf("6. Quit\n");
}
void inputStudents(Student students[], int *count) {
char cont = 'y';
while (cont == 'y') {
printf("ID: ");
scanf("%s", students[*count].id);
printf("Class: ");
scanf("%s", students[*count].clas);
printf("Name: ");
scanf("%s", students[*count].name);
printf("Score1: ");
scanf("%lf", &students[*count].score1);
printf("Score2: ");
scanf("%lf", &students[*count].score2);
printf("Score3: ");
scanf("%lf", &students[*count].score3);
students[*count].total = students[*count].score1 + students[*count].score2 + students[*count].score3;
students[*count].average = students[*count].total / 3.0;
(*count)++;
printf("Continue? (y/n): ");
scanf(" %c", &cont);
}
}
void outputStudents(Student students[], int count) {
for (int i = 0; i < count; i++) {
printf("%s %s %s %.1f %.1f %.1f %.1f\n",
students[i].id, students[i].clas, students[i].name,
students[i].score1, students[i].score2, students[i].score3,
students[i].total);
}
}
void sortStudents(Student students[], int count) {
for (int i = 0; i < count - 1; i++) {
for (int j = i + 1; j < count; j++) {
if (strcmp(students[i].clas, students[j].clas) > 0 ||
(strcmp(students[i].clas, students[j].clas) == 0 && students[i].total < students[j].total)) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void insertStudent(Student students[], int *count) {
Student newStudent;
printf("ID: ");
scanf("%s", newStudent.id);
printf("Class: ");
scanf("%s", newStudent.clas);
printf("Name: ");
scanf("%s", newStudent.name);
printf("Score1: ");
scanf("%lf", &newStudent.score1);
printf("Score2: ");
scanf("%lf", &newStudent.score2);
printf("Score3: ");
scanf("%lf", &newStudent.score3);
newStudent.total = newStudent.score1 + newStudent.score2 + newStudent.score3;
newStudent.average = newStudent.total / 3.0;
for (int i = *count; i > 0; i--) {
if (strcmp(students[i - 1].clas, newStudent.clas) < 0 ||
(strcmp(students[i - 1].clas, newStudent.clas) == 0 && students[i - 1].total > newStudent.total)) {
students[i] = students[i - 1];
} else {
students[i] = newStudent;
(*count)++;
return;
}
}
students[0] = newStudent;
(*count)++;
}
void deleteStudent(Student students[], int *count) {
char idOrName[20];
printf("Enter ID or Name to delete: ");
scanf("%s", idOrName);
for (int i = 0; i < *count; i++) {
if (strcmp(students[i].id, idOrName) == 0 || strcmp(students[i].name, idOrName) == 0) {
for (int j = i; j < *count - 1; j++) {
students[j] = students[j + 1];
}
(*count)--;
printf("Student deleted.\n");
return;
}
}
printf("Student not found.\n");
}
void modifyStudent(Student students[], int count) {
char id[20];
printf("Enter ID to modify: ");
scanf("%s", id);
for (int i = 0; i < count; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("New ID: ");
scanf("%s", students[i].id);
printf("New Class: ");
scanf("%s", students[i].clas);
printf("New Name: ");
scanf("%s", students[i].name);
printf("New Score1: ");
scanf("%lf", &students[i].score1);
printf("New Score2: ");
scanf("%lf", &students[i].score2);
printf("New Score3: ");
scanf("%lf", &students[i].score3);
students[i].total = students[i].score1 + students[i].score2 + students[i].score3;
students[i].average = students[i].total / 3.0;
printf("Student modified.\n");
return;
}
}
printf("Student not found.\n");
}
void queryStudent(Student students[], int count) {
char query[20];
printf("Enter query (ID/Class/Name): ");
scanf("%s", query);
for (int i = 0; i < count; i++) {
if (strstr(students[i].id, query) || strstr(students[i].clas, query) || strstr(students[i].name, query)) {
printf("%s %s %s %.1f %.1f %.1f %.1f\n",
students[i].id, students[i].clas, students[i].name,
students[i].score1, students[i].score2, students[i].score3,
students[i].total);
}
}
}