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.2 KiB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
struct Student {
char id[20];
char clas[20];
char name[20];
double score1;
double score2;
double score3;
double score;
};
// 全局学生数组
struct Student* students[100];
int studentCount = 0;
// 菜单函数
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: ");
}
// 学生信息录入函数
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");
}
}
// 学生信息删除函数
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");
}
}
// 学生信息查询函数
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");
}
}
// 学生信息排序函数
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;
}
}
}
}
// 学生信息输出函数
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:
// 退出程序
break;
default:
printf("Invalid option. Please try again.\n");
}
} while (option != 6);
// 释放内存
for (int i = 0; i < studentCount; i++) {
free(students[i]);
}
return 0;
}