parent
2d1f258cdf
commit
b26017fa52
@ -0,0 +1,506 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define MAX_STUDENTS 100
|
||||
#define MAX_NAME_LEN 50
|
||||
#define MAX_ID_LEN 20
|
||||
#define MAX_MAJOR_LEN 50
|
||||
#define MAX_AWARDS_LEN 200
|
||||
#define DATA_FILE "students.dat"
|
||||
|
||||
typedef struct {
|
||||
char id[MAX_ID_LEN];
|
||||
char name[MAX_NAME_LEN];
|
||||
char gender; // 'M' 或 'F'
|
||||
int age;
|
||||
char major[MAX_MAJOR_LEN];
|
||||
float gpa;
|
||||
char awards[MAX_AWARDS_LEN];
|
||||
} Student;
|
||||
|
||||
Student students[MAX_STUDENTS];
|
||||
int student_count = 0;
|
||||
|
||||
// 函数声明
|
||||
void load_data();
|
||||
void save_data();
|
||||
void add_student();
|
||||
void delete_student();
|
||||
void update_student();
|
||||
void search_student();
|
||||
void display_student(Student s);
|
||||
void list_all_students();
|
||||
void sort_students();
|
||||
void evaluate_students();
|
||||
void statistics();
|
||||
void show_menu();
|
||||
void clear_input_buffer();
|
||||
|
||||
int main() {
|
||||
load_data();
|
||||
|
||||
while (1) {
|
||||
show_menu();
|
||||
printf("请选择操作(1-9): ");
|
||||
char choice;
|
||||
scanf(" %c", &choice);
|
||||
clear_input_buffer();
|
||||
|
||||
switch (choice) {
|
||||
case '1': add_student(); break;
|
||||
case '2': delete_student(); break;
|
||||
case '3': update_student(); break;
|
||||
case '4': search_student(); break;
|
||||
case '5': list_all_students(); break;
|
||||
case '6': sort_students(); break;
|
||||
case '7': evaluate_students(); break;
|
||||
case '8': statistics(); break;
|
||||
case '9':
|
||||
save_data();
|
||||
printf("感谢使用,再见!\n");
|
||||
exit(0);
|
||||
default:
|
||||
printf("无效的选择,请重新输入!\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void load_data() {
|
||||
FILE *file = fopen(DATA_FILE, "rb");
|
||||
if (file == NULL) {
|
||||
return; // 文件不存在,无需加载
|
||||
}
|
||||
|
||||
student_count = fread(students, sizeof(Student), MAX_STUDENTS, file);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void save_data() {
|
||||
FILE *file = fopen(DATA_FILE, "wb");
|
||||
if (file == NULL) {
|
||||
printf("无法保存数据!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
fwrite(students, sizeof(Student), student_count, file);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void add_student() {
|
||||
if (student_count >= MAX_STUDENTS) {
|
||||
printf("学生数量已达上限,无法添加!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("\n--- 添加学生信息 ---\n");
|
||||
Student s;
|
||||
|
||||
printf("请输入学号: ");
|
||||
fgets(s.id, MAX_ID_LEN, stdin);
|
||||
s.id[strcspn(s.id, "\n")] = '\0'; // 移除换行符
|
||||
|
||||
// 检查学号是否已存在
|
||||
for (int i = 0; i < student_count; i++) {
|
||||
if (strcmp(students[i].id, s.id) == 0) {
|
||||
printf("错误: 该学号已存在!\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
printf("请输入姓名: ");
|
||||
fgets(s.name, MAX_NAME_LEN, stdin);
|
||||
s.name[strcspn(s.name, "\n")] = '\0';
|
||||
|
||||
printf("请输入性别(M/F): ");
|
||||
scanf(" %c", &s.gender);
|
||||
clear_input_buffer();
|
||||
s.gender = toupper(s.gender);
|
||||
|
||||
printf("请输入年龄: ");
|
||||
scanf("%d", &s.age);
|
||||
clear_input_buffer();
|
||||
|
||||
printf("请输入专业: ");
|
||||
fgets(s.major, MAX_MAJOR_LEN, stdin);
|
||||
s.major[strcspn(s.major, "\n")] = '\0';
|
||||
|
||||
printf("请输入GPA(0-4): ");
|
||||
scanf("%f", &s.gpa);
|
||||
clear_input_buffer();
|
||||
|
||||
printf("请输入获奖情况: ");
|
||||
fgets(s.awards, MAX_AWARDS_LEN, stdin);
|
||||
s.awards[strcspn(s.awards, "\n")] = '\0';
|
||||
|
||||
students[student_count++] = s;
|
||||
save_data();
|
||||
printf("学生信息添加成功!\n");
|
||||
}
|
||||
|
||||
void delete_student() {
|
||||
printf("\n--- 删除学生信息 ---\n");
|
||||
char id[MAX_ID_LEN];
|
||||
printf("请输入要删除的学生学号: ");
|
||||
fgets(id, MAX_ID_LEN, stdin);
|
||||
id[strcspn(id, "\n")] = '\0';
|
||||
|
||||
for (int i = 0; i < student_count; i++) {
|
||||
if (strcmp(students[i].id, id) == 0) {
|
||||
// 将最后一个学生移到当前位置
|
||||
if (i < student_count - 1) {
|
||||
students[i] = students[student_count - 1];
|
||||
}
|
||||
student_count--;
|
||||
save_data();
|
||||
printf("学生信息删除成功!\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
printf("错误: 未找到该学号的学生!\n");
|
||||
}
|
||||
|
||||
void update_student() {
|
||||
printf("\n--- 修改学生信息 ---\n");
|
||||
char id[MAX_ID_LEN];
|
||||
printf("请输入要修改的学生学号: ");
|
||||
fgets(id, MAX_ID_LEN, stdin);
|
||||
id[strcspn(id, "\n")] = '\0';
|
||||
|
||||
for (int i = 0; i < student_count; i++) {
|
||||
if (strcmp(students[i].id, id) == 0) {
|
||||
printf("当前学生信息:\n");
|
||||
display_student(students[i]);
|
||||
|
||||
printf("\n请输入新的信息(留空则保持不变):\n");
|
||||
|
||||
char input[MAX_NAME_LEN];
|
||||
|
||||
printf("姓名[%s]: ", students[i].name);
|
||||
fgets(input, MAX_NAME_LEN, stdin);
|
||||
input[strcspn(input, "\n")] = '\0';
|
||||
if (strlen(input) > 0) {
|
||||
strcpy(students[i].name, input);
|
||||
}
|
||||
|
||||
printf("性别[%c]: ", students[i].gender);
|
||||
char gender;
|
||||
scanf(" %c", &gender);
|
||||
clear_input_buffer();
|
||||
if (gender != '\n') {
|
||||
students[i].gender = toupper(gender);
|
||||
}
|
||||
|
||||
printf("年龄[%d]: ", students[i].age);
|
||||
fgets(input, MAX_NAME_LEN, stdin);
|
||||
input[strcspn(input, "\n")] = '\0';
|
||||
if (strlen(input) > 0) {
|
||||
students[i].age = atoi(input);
|
||||
}
|
||||
|
||||
printf("专业[%s]: ", students[i].major);
|
||||
fgets(input, MAX_MAJOR_LEN, stdin);
|
||||
input[strcspn(input, "\n")] = '\0';
|
||||
if (strlen(input) > 0) {
|
||||
strcpy(students[i].major, input);
|
||||
}
|
||||
|
||||
printf("GPA[%.2f]: ", students[i].gpa);
|
||||
fgets(input, MAX_NAME_LEN, stdin);
|
||||
input[strcspn(input, "\n")] = '\0';
|
||||
if (strlen(input) > 0) {
|
||||
students[i].gpa = atof(input);
|
||||
}
|
||||
|
||||
printf("获奖情况[%s]: ", students[i].awards);
|
||||
fgets(input, MAX_AWARDS_LEN, stdin);
|
||||
input[strcspn(input, "\n")] = '\0';
|
||||
if (strlen(input) > 0) {
|
||||
strcpy(students[i].awards, input);
|
||||
}
|
||||
|
||||
save_data();
|
||||
printf("学生信息修改成功!\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
printf("错误: 未找到该学号的学生!\n");
|
||||
}
|
||||
|
||||
void search_student() {
|
||||
printf("\n--- 查询学生信息 ---\n");
|
||||
printf("1. 按学号查询\n");
|
||||
printf("2. 按姓名查询\n");
|
||||
printf("3. 按专业查询\n");
|
||||
printf("请选择查询方式(1-3): ");
|
||||
|
||||
char choice;
|
||||
scanf(" %c", &choice);
|
||||
clear_input_buffer();
|
||||
|
||||
char query[MAX_NAME_LEN];
|
||||
int found = 0;
|
||||
|
||||
switch (choice) {
|
||||
case '1':
|
||||
printf("请输入学号: ");
|
||||
fgets(query, MAX_ID_LEN, stdin);
|
||||
query[strcspn(query, "\n")] = '\0';
|
||||
|
||||
printf("\n查询结果:\n");
|
||||
for (int i = 0; i < student_count; i++) {
|
||||
if (strcmp(students[i].id, query) == 0) {
|
||||
display_student(students[i]);
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case '2':
|
||||
printf("请输入姓名: ");
|
||||
fgets(query, MAX_NAME_LEN, stdin);
|
||||
query[strcspn(query, "\n")] = '\0';
|
||||
|
||||
printf("\n查询结果:\n");
|
||||
for (int i = 0; i < student_count; i++) {
|
||||
if (strstr(students[i].name, query) != NULL) {
|
||||
display_student(students[i]);
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case '3':
|
||||
printf("请输入专业: ");
|
||||
fgets(query, MAX_MAJOR_LEN, stdin);
|
||||
query[strcspn(query, "\n")] = '\0';
|
||||
|
||||
printf("\n查询结果:\n");
|
||||
for (int i = 0; i < student_count; i++) {
|
||||
if (strstr(students[i].major, query) != NULL) {
|
||||
display_student(students[i]);
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("无效的选择!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
printf("未找到符合条件的学生!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void display_student(Student s) {
|
||||
printf("\n学号: %s\n", s.id);
|
||||
printf("姓名: %s\n", s.name);
|
||||
printf("性别: %c\n", s.gender);
|
||||
printf("年龄: %d\n", s.age);
|
||||
printf("专业: %s\n", s.major);
|
||||
printf("GPA: %.2f\n", s.gpa);
|
||||
printf("获奖情况: %s\n", s.awards);
|
||||
printf("------------------------------\n");
|
||||
}
|
||||
|
||||
void list_all_students() {
|
||||
printf("\n--- 所有学生信息 ---\n");
|
||||
if (student_count == 0) {
|
||||
printf("没有学生信息!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < student_count; i++) {
|
||||
display_student(students[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void sort_students() {
|
||||
printf("\n--- 排序学生信息 ---\n");
|
||||
printf("1. 按学号排序\n");
|
||||
printf("2. 按姓名排序\n");
|
||||
printf("3. 按GPA排序\n");
|
||||
printf("4. 按年龄排序\n");
|
||||
printf("请选择排序方式(1-4): ");
|
||||
|
||||
char choice;
|
||||
scanf(" %c", &choice);
|
||||
clear_input_buffer();
|
||||
|
||||
// 简单的冒泡排序
|
||||
for (int i = 0; i < student_count - 1; i++) {
|
||||
for (int j = 0; j < student_count - i - 1; j++) {
|
||||
int swap = 0;
|
||||
|
||||
switch (choice) {
|
||||
case '1':
|
||||
if (strcmp(students[j].id, students[j+1].id) > 0) {
|
||||
swap = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case '2':
|
||||
if (strcmp(students[j].name, students[j+1].name) > 0) {
|
||||
swap = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case '3':
|
||||
if (students[j].gpa < students[j+1].gpa) {
|
||||
swap = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case '4':
|
||||
if (students[j].age > students[j+1].age) {
|
||||
swap = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("无效的选择!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (swap) {
|
||||
Student temp = students[j];
|
||||
students[j] = students[j+1];
|
||||
students[j+1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
save_data();
|
||||
printf("排序完成!\n");
|
||||
list_all_students();
|
||||
}
|
||||
|
||||
void evaluate_students() {
|
||||
printf("\n--- 学生评优评奖 ---\n");
|
||||
printf("1. 显示GPA前3名的学生\n");
|
||||
printf("2. 显示有获奖记录的学生\n");
|
||||
printf("请选择评优方式(1-2): ");
|
||||
|
||||
char choice;
|
||||
scanf(" %c", &choice);
|
||||
clear_input_buffer();
|
||||
|
||||
switch (choice) {
|
||||
case '1': {
|
||||
// 先按GPA排序
|
||||
for (int i = 0; i < student_count - 1; i++) {
|
||||
for (int j = 0; j < student_count - i - 1; j++) {
|
||||
if (students[j].gpa < students[j+1].gpa) {
|
||||
Student temp = students[j];
|
||||
students[j] = students[j+1];
|
||||
students[j+1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int limit = student_count < 3 ? student_count : 3;
|
||||
printf("\nGPA前%d名的学生:\n", limit);
|
||||
for (int i = 0; i < limit; i++) {
|
||||
display_student(students[i]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case '2': {
|
||||
int found = 0;
|
||||
printf("\n有获奖记录的学生:\n");
|
||||
for (int i = 0; i < student_count; i++) {
|
||||
if (strlen(students[i].awards) > 0) {
|
||||
display_student(students[i]);
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
printf("没有学生有获奖记录!\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
printf("无效的选择!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void statistics() {
|
||||
printf("\n--- 统计信息 ---\n");
|
||||
printf("总学生数: %d\n", student_count);
|
||||
|
||||
if (student_count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 按性别统计
|
||||
int male_count = 0, female_count = 0;
|
||||
for (int i = 0; i < student_count; i++) {
|
||||
if (toupper(students[i].gender) == 'M') {
|
||||
male_count++;
|
||||
} else {
|
||||
female_count++;
|
||||
}
|
||||
}
|
||||
printf("男生人数: %d, 女生人数: %d\n", male_count, female_count);
|
||||
|
||||
// 平均GPA
|
||||
float total_gpa = 0;
|
||||
for (int i = 0; i < student_count; i++) {
|
||||
total_gpa += students[i].gpa;
|
||||
}
|
||||
printf("平均GPA: %.2f\n", total_gpa / student_count);
|
||||
|
||||
// 按专业统计
|
||||
printf("\n各专业人数:\n");
|
||||
char majors[MAX_STUDENTS][MAX_MAJOR_LEN];
|
||||
int counts[MAX_STUDENTS] = {0};
|
||||
int major_count = 0;
|
||||
|
||||
for (int i = 0; i < student_count; i++) {
|
||||
int found = 0;
|
||||
for (int j = 0; j < major_count; j++) {
|
||||
if (strcmp(majors[j], students[i].major) == 0) {
|
||||
counts[j]++;
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
strcpy(majors[major_count], students[i].major);
|
||||
counts[major_count] = 1;
|
||||
major_count++;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < major_count; i++) {
|
||||
printf("%s: %d人\n", majors[i], counts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void show_menu() {
|
||||
printf("\n=== 学生信息管理系统 ===\n");
|
||||
printf("1. 添加学生信息\n");
|
||||
printf("2. 删除学生信息\n");
|
||||
printf("3. 修改学生信息\n");
|
||||
printf("4. 查询学生信息\n");
|
||||
printf("5. 显示所有学生\n");
|
||||
printf("6. 排序学生信息\n");
|
||||
printf("7. 学生评优评奖\n");
|
||||
printf("8. 统计信息\n");
|
||||
printf("9. 退出系统\n");
|
||||
}
|
||||
|
||||
void clear_input_buffer() {
|
||||
while (getchar() != '\n');
|
||||
}
|
Loading…
Reference in new issue