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.

157 lines
5.0 KiB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct Student {
char *id;
char *clas;
char *name;
double score1;
double score2;
double score3;
double totalScore;
};
struct Student students[MAX_STUDENTS];
int studentCount = 0;
// 函数声明
void menu();
void inputStudent();
void deleteStudent();
void selectStudent();
void orderStudents();
void outputStudents();
void freeMemory();
// 主函数
int main() {
int option;
do {
menu();
printf("please input your option: \n");
scanf("%d", &option);
switch(option) {
case 1: inputStudent(); break;
case 2: deleteStudent(); break;
case 3: selectStudent(); break;
case 4: orderStudents(); break;
case 5: outputStudents(); break;
case 6: freeMemory(); return 0; // 清理内存并退出
}
} while (option != 6);
return 0;
}
// 菜单函数
void menu() {
printf("1.input\n2.delete\n3.select\n4.order\n5.output\n6.quit\n");
}
// 输入学生信息
void inputStudent() {
char continueInput[4];
do {
students[studentCount].id = (char *)malloc(20 * sizeof(char));
students[studentCount].clas = (char *)malloc(20 * sizeof(char));
students[studentCount].name = (char *)malloc(20 * sizeof(char));
printf("Id ");
scanf("%s", students[studentCount].id);
printf("class ");
scanf("%s", students[studentCount].clas);
printf("name ");
scanf("%s", students[studentCount].name);
printf("score1 ");
scanf("%lf", &students[studentCount].score1);
printf("score2 ");
scanf("%lf", &students[studentCount].score2);
printf("score3 ");
scanf("%lf", &students[studentCount].score3);
students[studentCount].totalScore = students[studentCount].score1 + students[studentCount].score2 + students[studentCount].score3;
studentCount++;
printf("continue? \n");
scanf("%s", continueInput);
} while (strcmp(continueInput, "yes") == 0);
}
// 删除学生信息
void deleteStudent() {
char target[20];
char continueDelete[4];
int found = 0;
do {
found = 0;
scanf("%s", target);
int i1;
for (i1 = 0; i1 < studentCount; i1++) {
if (strcmp(students[i1].id, target) == 0 || strcmp(students[i1].name, target) == 0) {
free(students[i1].id);
free(students[i1].clas);
free(students[i1].name);
memmove(&students[i1], &students[i1+1], (studentCount - i1 - 1) * sizeof(struct Student));
studentCount--;
found = 1;
int l;
for(l =0;l<studentCount;l++){
printf("%s,%s,%s,%.1f,%.1f,%.1f,%.1f\n",students[l].id,students[l].clas,students[l].name,students[l].score1,students[l].score2,students[l].score3,students[l].totalScore);
}
break; }
}
if (!found) {
printf("there is no eligible student.\n");
}
printf("continue? \n");
scanf("%s", continueDelete);
} while (strcmp(continueDelete, "yes") == 0);
}
// 查询学生信息
void selectStudent() {
char target[20];
char continueSelect[4];
int found = 0;
do {
found = 0;
scanf("%s", target);
int i2;
for (i2 = 0; i2 < studentCount; i2++) {
if (strcmp(students[i2].id, target) == 0 || strcmp(students[i2].clas, target) == 0) {
printf("%s,%s,%s,%.1f,%.1f,%.1f,%.1f\n", students[i2].id, students[i2].clas, students[i2].name, students[i2].score1, students[i2].score2, students[i2].score3, students[i2].totalScore);
found = 1;
}
}
if (!found) {
printf("there is no eligible student.\n");
}
printf("continue? \n");
scanf("%s", continueSelect);
} while (strcmp(continueSelect, "yes") == 0);
}
// 排序学生信息
void orderStudents() {
int i3;
for (i3 = 0; i3 < studentCount - 1; i3++) {
int j;
for (j = 0; j < studentCount - 1 - i3; j++) {
if (strcmp(students[j].clas, students[j + 1].clas) > 0 ||
(strcmp(students[j].clas, students[j + 1].clas) == 0 && students[j].totalScore < students[j + 1].totalScore)) {
struct Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
outputStudents();
}
void outputStudents() {
int i4;
for (i4 = 0; i4 < studentCount; i4++) {
printf("%s,%s,%s,%.1f,%.1f,%.1f,%.1f\n", students[i4].id, students[i4].clas, students[i4].name, students[i4].score1, students[i4].score2, students[i4].score3, students[i4].totalScore);
}
}
void freeMemory(){
int i;
for(i = 0;i<studentCount;i++){
free(students[i].id);
free(students[i].clas);
free(students[i].name);
}
}