利用C语言,设计编写一个传统的小型学生信息管理系统。 1、该系统可以用于管理一个班的学生信息。学生人数在50人左右。 2、该系统存放了有关学生的某些方面的信息:如学号、姓名、性别、出生日期、成绩、所在系等,具体包含的数据内容由设计者分析确定。 3、所有学生信息的数据结构及其存储结构由设计者分析确定。 4、所有学生的信息数据可以用文件形式存放。(可选项) 5、该系统具有相关的数据输入、输出、查询、修改、插入、删除、统计、排序等功能。(功能的多少设计者可根据需要自行调整,但必须具备输出、输出、查询、某一方面数据统计的基本功能。) 6、系统应具有友好的用户界面,并能够让用户通过菜单进行相应功能的选择。 7、上机实验前进行系统分析和设计。 8、选用适当的算法和数据结构,建立相应的程序代码。 9、上机调试通过,验证自己编写的程序的结果与预料效果之间的关系。
时间: 2023-07-10 19:39:05 浏览: 76
这是一个比较复杂的问题,需要设计和编写一个完整的小型学生信息管理系统。以下是一个简单的代码示例,供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生信息结构体
typedef struct Student {
char id[10]; // 学号
char name[20]; // 姓名
char gender[5]; // 性别
char birthdate[20]; // 出生日期
float score; // 成绩
char department[20]; // 所在系
} Student;
// 定义函数原型
void print_menu();
void add_student(Student *students, int *count);
void search_student(Student *students, int count);
void delete_student(Student *students, int *count);
void update_student(Student *students, int count);
void sort_students(Student *students, int count);
void save_to_file(Student *students, int count);
void load_from_file(Student *students, int *count);
int main() {
Student students[50]; // 学生信息数组
int count = 0; // 学生数量
int choice = 0; // 用户选择的菜单项
// 加载数据文件(如果存在)
load_from_file(students, &count);
// 显示菜单,等待用户选择
do {
print_menu();
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(students, &count);
break;
case 2:
search_student(students, count);
break;
case 3:
delete_student(students, &count);
break;
case 4:
update_student(students, count);
break;
case 5:
sort_students(students, count);
break;
case 6:
save_to_file(students, count);
break;
case 7:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != 7);
return 0;
}
// 显示菜单
void print_menu() {
printf("== Student Information Management System ==\n");
printf("1. Add student\n");
printf("2. Search student\n");
printf("3. Delete student\n");
printf("4. Update student\n");
printf("5. Sort students\n");
printf("6. Save to file\n");
printf("7. Exit\n");
printf("Please enter your choice: ");
}
// 添加学生信息
void add_student(Student *students, int *count) {
if (*count < 50) {
Student new_student;
printf("Please enter student ID: ");
scanf("%s", new_student.id);
printf("Please enter student name: ");
scanf("%s", new_student.name);
printf("Please enter student gender: ");
scanf("%s", new_student.gender);
printf("Please enter student birthdate: ");
scanf("%s", new_student.birthdate);
printf("Please enter student score: ");
scanf("%f", &new_student.score);
printf("Please enter student department: ");
scanf("%s", new_student.department);
students[*count] = new_student;
(*count)++;
printf("Student added successfully.\n");
} else {
printf("Sorry, the maximum number of students has been reached.\n");
}
}
// 搜索学生信息
void search_student(Student *students, int count) {
char id[10];
printf("Please enter student ID: ");
scanf("%s", id);
for (int i = 0; i < count; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("Student found:\n");
printf("ID: %s\n", students[i].id);
printf("Name: %s\n", students[i].name);
printf("Gender: %s\n", students[i].gender);
printf("Birthdate: %s\n", students[i].birthdate);
printf("Score: %.2f\n", students[i].score);
printf("Department: %s\n", students[i].department);
return;
}
}
printf("Student not found.\n");
}
// 删除学生信息
void delete_student(Student *students, int *count) {
char id[10];
printf("Please enter student ID: ");
scanf("%s", id);
for (int i = 0; i < *count; i++) {
if (strcmp(students[i].id, id) == 0) {
// 将最后一个学生信息移到当前位置
students[i] = students[*count - 1];
(*count)--;
printf("Student deleted successfully.\n");
return;
}
}
printf("Student not found.\n");
}
// 更新学生信息
void update_student(Student *students, int count) {
char id[10];
printf("Please enter student ID: ");
scanf("%s", id);
for (int i = 0; i < count; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("Please enter new student name (or press Enter to keep the current value \"%s\"): ", students[i].name);
char name[20];
fgets(name, 20, stdin); // 先读取回车符
fgets(name, 20, stdin); // 读取输入的字符串
name[strlen(name) - 1] = '\0'; // 去掉末尾的回车符
if (strlen(name) > 0) {
strcpy(students[i].name, name);
}
printf("Please enter new student gender (or press Enter to keep the current value \"%s\"): ", students[i].gender);
char gender[5];
fgets(gender, 5, stdin);
fgets(gender, 5, stdin);
gender[strlen(gender) - 1] = '\0';
if (strlen(gender) > 0) {
strcpy(students[i].gender, gender);
}
printf("Please enter new student birthdate (or press Enter to keep the current value \"%s\"): ", students[i].birthdate);
char birthdate[20];
fgets(birthdate, 20, stdin);
fgets(birthdate, 20, stdin);
birthdate[strlen(birthdate) - 1] = '\0';
if (strlen(birthdate) > 0) {
strcpy(students[i].birthdate, birthdate);
}
printf("Please enter new student score (or press Enter to keep the current value \"%.2f\"): ", students[i].score);
char score_str[10];
fgets(score_str, 10, stdin);
fgets(score_str, 10, stdin);
score_str[strlen(score_str) - 1] = '\0';
if (strlen(score_str) > 0) {
float score = atof(score_str);
students[i].score = score;
}
printf("Please enter new student department (or press Enter to keep the current value \"%s\"): ", students[i].department);
char department[20];
fgets(department, 20, stdin);
fgets(department, 20, stdin);
department[strlen(department) - 1] = '\0';
if (strlen(department) > 0) {
strcpy(students[i].department, department);
}
printf("Student updated successfully.\n");
return;
}
}
printf("Student not found.\n");
}
// 按成绩排序学生信息
void sort_students(Student *students, int count) {
for (int i = 0; i < count - 1; i++) {
for (int j = i + 1; j < count; j++) {
if (students[i].score < students[j].score) {
Student tmp = students[i];
students[i] = students[j];
students[j] = tmp;
}
}
}
printf("Students sorted by score in descending order.\n");
}
// 保存学生信息到文件
void save_to_file(Student *students, int count) {
FILE *fp = fopen("students.dat", "wb");
if (fp == NULL) {
printf("Failed to open file.\n");
return;
}
fwrite(students, sizeof(Student), count, fp);
fclose(fp);
printf("Student information saved to file.\n");
}
// 从文件中加载学生信息
void load_from_file(Student *students, int *count) {
FILE *fp = fopen("students.dat", "rb");
if (fp == NULL) {
return;
}
fread(students, sizeof(Student), 50, fp);
*count = fread(students, sizeof(Student), 50, fp);
fclose(fp);
}
```
这个程序实现了基本的添加、搜索、删除、更新、排序和文件读写功能。当用户选择“Add student”时,程序会要求用户输入学生信息,并将其添加到学生信息数组中。当用户选择“Search student”时,程序会要求用户输入学生ID,然后在学生信息数组中查找对应的学生信息并输出。当用户选择“Delete student”时,程序会要求用户输入学生ID,然后从学生信息数组中删除对应的学生信息。当用户选择“Update student”时,程序会要求用户输入学生ID,并允许用户修改学生信息。当用户选择“Sort students”时,程序会按照学生成绩从高到低排序学生信息。当用户选择“Save to file”时,程序将当前所有学生信息保存到文件中。当用户选择“Exit”时,程序退出。
阅读全文