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.

155 lines
4.1 KiB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// <20><><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><E1B9B9>
struct Student {
char id[20];
char clas[20];
char name[20];
double score1;
double score2;
double score3;
double score;
};
// ȫ<><C8AB>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
struct Student* students[100];
int studentCount = 0;
// <20>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD>
void printMenu() {
printf("1. Input\n");
printf("2. Delete\n");
printf("3. Select\n");
printf("4. Order\n");
printf("5. Output\n");
printf("6. Quit\n");
printf("Please input your option: ");
}
// ѧ<><D1A7><EFBFBD><EFBFBD>Ϣ¼<CFA2><EFBFBD><EBBAAF>
void inputStudent() {
if (studentCount < 100) {
struct Student* student = (struct Student*)malloc(sizeof(struct Student));
printf("Id: ");
scanf("%s", student->id);
printf("Class: ");
scanf("%s", student->clas);
printf("Name: ");
scanf("%s", student->name);
printf("Score1: ");
scanf("%lf", &student->score1);
printf("Score2: ");
scanf("%lf", &student->score2);
printf("Score3: ");
scanf("%lf", &student->score3);
student->score = student->score1 + student->score2 + student->score3;
students[studentCount++] = student;
}
else {
printf("Student database is full!\n");
}
}
// ѧ<><D1A7><EFBFBD><EFBFBD>Ϣɾ<CFA2><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void deleteStudent() {
char deleteId[20];
printf("Enter student ID or name to delete: ");
scanf("%s", deleteId);
int deleted = 0;
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i]->id, deleteId) == 0 || strcmp(students[i]->name, deleteId) == 0) {
free(students[i]);
for (int j = i; j < studentCount - 1; j++) {
students[j] = students[j + 1];
}
studentCount--;
deleted = 1;
i--;
}
}
if (deleted == 0) {
printf("Student not found.\n");
}
}
// ѧ<><D1A7><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
void selectStudent() {
char query[20];
printf("Enter student ID or class to select: ");
scanf("%s", query);
int found = 0;
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i]->id, query) == 0 || strcmp(students[i]->clas, query) == 0) {
printf("%s, %s, %s, %.1lf, %.1lf, %.1lf, %.1lf\n", students[i]->id, students[i]->clas, students[i]->name, students[i]->score1, students[i]->score2, students[i]->score3, students[i]->score);
found = 1;
}
}
if (found == 0) {
printf("There is no eligible student.\n");
}
}
// ѧ<><D1A7><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void orderStudents() {
for (int i = 0; i < studentCount; i++) {
for (int j = i + 1; j < studentCount; j++) {
if (strcmp(students[i]->clas, students[j]->clas) > 0 || (strcmp(students[i]->clas, students[j]->clas) == 0 && students[i]->score < students[j]->score)) {
struct Student* temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
// ѧ<><D1A7><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void outputStudents() {
for (int i = 0; i < studentCount; i++) {
printf("%s, %s, %s, %.1lf, %.1lf, %.1lf, %.1lf\n", students[i]->id, students[i]->clas, students[i]->name, students[i]->score1, students[i]->score2, students[i]->score3, students[i]->score);
}
}
int main() {
int option;
do {
printMenu();
scanf("%d", &option);
switch (option) {
case 1:
inputStudent();
break;
case 2:
deleteStudent();
break;
case 3:
selectStudent();
break;
case 4:
orderStudents();
outputStudents();
break;
case 5:
outputStudents();
break;
case 6:
// <20>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD>
break;
default:
printf("Invalid option. Please try again.\n");
}
} while (option != 6);
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
for (int i = 0; i < studentCount; i++) {
free(students[i]);
}
return 0;
}