#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
typedef struct student{
char name[20];
int id;
char dormitory[20];
char qq[20];
char phone[20];
int cet4_score;
} Student;
#define MAX_STUDENT_NUM 100
Student students[MAX_STUDENT_NUM];
int student_num = 0;
int cmp_by_name(const void *a, const void *b) {
return strcmp(((Student*)a)->name, ((Student*)b)->name);
}
int cmp_by_phone(const void *a, const void *b) {
return strcmp(((Student*)a)->phone, ((Student*)b)->phone);
}
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("Error: 学生数量已达到上限\n");
return;
}
Student new_student;
printf("请输入学生姓名:");
scanf("%s", new_student.name);
printf("请输入学生学号:");
scanf("%d", &new_student.id);
printf("请输入学生宿舍:");
scanf("%s", new_student.dormitory);
printf("请输入学生QQ号:");
scanf("%s", new_student.qq);
printf("请输入学生电话:");
scanf("%s", new_student.phone);
printf("请输入学生CET4成绩:");
scanf("%d", &new_student.cet4_score);
students[student_num++] = new_student;
printf("已添加学生 %s 的信息\n", new_student.name);
}
void delete_student() {
printf("请输入要删除的学生姓名:");
char name_to_delete[20];
scanf("%s", name_to_delete);
int deleted_num = 0;
for (int i = 0; i < student_num; i++) {
if (strcmp(name_to_delete, students[i].name) == 0) {
deleted_num++;
if (i != student_num-1) {
memcpy(&students[i], &students[i+1], sizeof(Student)*(student_num-i-1));
}
}
}
student_num -= deleted_num;
printf("已删除 %d 名学生的信息\n", deleted_num);
}
void sort_and_print_by_name() {
qsort(students, student_num, sizeof(Student), cmp_by_name);
for (int i = 0; i < student_num; i++) {
printf("姓名:%s\n", students[i].name);
printf("学号:%d\n", students[i].id);
printf("宿舍:%s\n", students[i].dormitory);
printf("QQ号:%s\n", students[i].qq);
printf("电话:%s\n", students[i].phone);
printf("CET4 成绩:%d\n", students[i].cet4_score);
printf("\n");
}
}
void sort_and_print_by_phone() {
qsort(students, student_num, sizeof(Student), cmp_by_phone);
for (int i = 0; i < student_num; i++) {
printf("姓名:%s\n", students[i].name);
printf("学号:%d\n", students[i].id);
printf("宿舍:%s\n", students[i].dormitory);
printf("QQ号:%s\n", students[i].qq);
printf("电话:%s\n", students[i].phone);
printf("CET4 成绩:%d\n", students[i].cet4_score);
printf("\n");
}
}
void find_phone_by_name() {
printf("请输入要查询的学生姓名:");
char name_to_find[20];
scanf("%s", name_to_find);
int found_num = 0;
for (int i = 0; i < student_num; i++) {
if (strcmp(name_to_find, students[i].name) == 0) {
printf("%s 的电话是 %s\n", name_to_find, students[i].phone);
found_num++;
}
}
if (found_num == 0) {
printf("没有找到姓名为 %s 的学生信息\n", name_to_find);
}
}
void find_name_by_phone() {
printf("请输入要查询的学生电话:");
char phone_to_find[20];
scanf("%s", phone_to_find);
int found_num = 0;
for (int i = 0; i < student_num; i++) {
if (strcmp(phone_to_find, students[i].phone) == 0) {
printf("%s 的姓名是 %s\n", phone_to_find, students[i].name);
found_num++;
}
}
if (found_num == 0) {
printf("没有找到电话为 %s 的学生信息\n", phone_to_find);
}
}
void cet_statistics() {
double sum = 0;
int pass_count = 0;
for (int i = 0; i < student_num; i++) {
sum += students[i].cet4_score;
if (students[i].cet4_score >= 425) {
pass_count++;
}
}
double average = sum / student_num;
double variance = 0;
for (int i = 0; i < student_num; i++) {
variance += (students[i].cet4_score - average) * (students[i].cet4_score - average);
}
variance /= student_num;
printf("平均成绩:%.2lf\n", average);
printf("方差:%.2lf\n", variance);
printf("及格率:%.2lf%%\n", ((double)pass_count / student_num) * 100);
}
void print_menu() {
printf("请选择操作:\n");
printf("1. 添加学生信息\n");
printf("2. 删除学生信息\n");
printf("3. 打印所有学生信息并按照姓名排序\n");
printf("4. 打印所有学生信息并按照电话号码排序\n");
printf("5. 按姓名查找电话号码\n");
printf("6. 按电话号码查找姓名\n");
printf("7. 分析四级成绩\n");
}
int main() {
while (1) {
print_menu();
int option;
scanf("%d", &option);
switch (option) {
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
sort_and_print_by_name();
break;
case 4:
sort_and_print_by_phone();
break;
case 5:
find_phone_by_name();
break;
case 6:
find_name_by_phone();
break;
case 7:
cet_statistics();
break;
}
}
}