C语言课程设计题目六:学生信息管理系统设计
题目六:学生信息管理系统设计
学生信息包括:学号,姓名,年龄,性别,出生年月,地址,电话,E-mail等。设计学生信息管理系统,功能包括:
- 系统以菜单方式工作。
- 学生信息录入功能(学生信息用文件保存)--输入。
- 学生信息浏览功能--输出。
- 学生信息查询功能--算法。
查询方式- 按学号查询
- 按姓名查询
- 学生信息的删除与修改(可选项)。
studentInformationManagement.c
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 6031)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define STUDENTS_FILE "students.dat"
#define MAX_STUDENTS 100
typedef struct {
// 你说学号会不会有英文符号呢? 本来写的int类型,身份证号都有一个X,那学号写成char
// 学号不能带\0 警告 C6054 可能没有为字符串“id”添加字符串零终止符。
char id[20];
char name[50];
int age;
char gender[10];
char birth[20];
char address[100];
char phone[20];
char email[50];
}Student;
// 确定Win/Linux操作系统,清屏
void clearScreen();
// 保存学生信息
void saveStudentsToFile(Student students[], int count);
// 加载学生信息(每次启动程序,加载信息)
void loadStudentsFromFile(Student students[], int* count);
// 添加学生信息
void addStudent(Student students[], int* count);
// 展示所有学生信息
void displayStudents(Student students[], int count);
// 根据学号查询学生信息
void searchStudentById(Student students[], int count);
// 根据姓名查询学生信息
void searchStudentByName(Student students[], int count);
// 根据学号删除学生信息
void deleteStudentById(Student students[], int* count);
// 修改学生信息
void modifyStudentById(Student students[], int count);
studentInformationManagement.c
#include"studentInformationManagement.h"
// 确定Win/Linux操作系统,清屏
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
// 保存学生信息
void saveStudentsToFile(Student students[], int count) {
FILE* file = fopen(STUDENTS_FILE, "wb");
if (file == NULL) {
printf("无法打开文件 %s !\n", STUDENTS_FILE);
return;
}
fwrite(students, sizeof(Student), count, file);
fclose(file);
}
// 加载学生信息
void loadStudentsFromFile(Student students[], int* count) {
FILE* file = fopen(STUDENTS_FILE, "rb");
if (file == NULL) {
printf("无法打开文件 %s!\n", STUDENTS_FILE);
*count = 0;
return;
}
// fread 返回的是 size_t 类型的值
size_t temporaryCount = fread(students, sizeof(Student), MAX_STUDENTS, file);
printf("读取了 %zu 条记录。\n", temporaryCount);
*count = (int)temporaryCount;
fclose(file);
}
// 添加学生信息
void addStudent(Student students[], int* count) {
if (*count >= MAX_STUDENTS) {
printf("学生信息已满,无法继续添加。\n");
return;
}
Student newStudent;
printf("请输入学号: ");
scanf("%s", newStudent.id);
printf("请输入姓名: ");
scanf("%s", newStudent.name);
printf("请输入年龄: ");
scanf("%d", &newStudent.age);
printf("请输入性别: ");
scanf("%s", newStudent.gender);
printf("请输入出生年月: ");
scanf("%s", newStudent.birth);
printf("请输入地址: ");
scanf("%s", newStudent.address);
printf("请输入电话: ");
scanf("%s", newStudent.phone);
printf("请输入E-mail: ");
scanf("%s", newStudent.email);
students[*count] = newStudent;
(*count)++;
saveStudentsToFile(students, *count);
printf("学生信息已保存。\n");
}
// 展示所有学生信息
void displayStudents(Student students[], int count) {
//clearScreen();
printf("\n+-----------------------------------+\n");
printf("| 学生信息列表 |\n");
printf("+-----------------------------------+\n");
printf("| 学号 | 姓名 | 年龄 | 性别 | 出生年月 | 地址 | 电话 | E-mail |\n");
printf("+-----------------------------------+\n");
for (int i = 0; i < count; i++) {
printf("| %-6s | %-6s | %-3d | %-6s | %-10s | %-10s | %-10s | %-10s |\n",
students[i].id, students[i].name, students[i].age, students[i].gender,
students[i].birth, students[i].address, students[i].phone, students[i].email);
}
printf("+-----------------------------------+\n");
}
// 根据学号查询学生信息
void searchStudentById(Student students[], int count) {
char id[20];
printf("请输入要查询的学号: ");
scanf("%s", id);
int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("\n找到的学生信息:\n");
printf("学号: %s\n", students[i].id);
printf("姓名: %s\n", students[i].name);
printf("年龄: %d\n", students[i].age);
printf("性别: %s\n", students[i].gender);
printf("出生年月: %s\n", students[i].birth);
printf("地址: %s\n", students[i].address);
printf("电话: %s\n", students[i].phone);
printf("E-mail: %s\n", students[i].email);
found = 1;
break;
}
}
if (!found) {
printf("未找到该学号的学生信息。\n");
}
}
// 根据姓名查询学生信息
void searchStudentByName(Student students[], int count) {
char name[50];
printf("请输入要查询的姓名: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("\n找到的学生信息:\n");
printf("学号: %s\n", students[i].id);
printf("姓名: %s\n", students[i].name);
printf("年龄: %d\n", students[i].age);
printf("性别: %s\n", students[i].gender);
printf("出生年月: %s\n", students[i].birth);
printf("地址: %s\n", students[i].address);
printf("电话: %s\n", students[i].phone);
printf("E-mail: %s\n", students[i].email);
found = 1;
}
}
if (!found) {
printf("未找到该姓名的学生信息。\n");
}
}
// 根据学号删除学生信息
void deleteStudentById(Student students[], int* count) {
char id[20];
printf("请输入要删除的学号: ");
scanf("%s", id);
// 把要删除的学生移到最后面
int found = 0;
for (int i = 0; i < *count; i++) {
if (strcmp(students[i].id, id) == 0) {
for (int j = i; j < *count - 1; j++) {
students[j] = students[j + 1];
}
(*count)--;
saveStudentsToFile(students, *count);
printf("学生信息已删除。\n");
found = 1;
break;
}
}
if (!found) {
printf("未找到该学号的学生信息。\n");
}
}
// 修改学生信息
void modifyStudentById(Student students[], int count) {
char id[20];
printf("请输入要修改的学号: ");
scanf("%s", id);
int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("请选择要修改的信息:\n");
printf("1. 姓名\n");
printf("2. 年龄\n");
printf("3. 性别\n");
printf("4. 出生年月\n");
printf("5. 地址\n");
printf("6. 电话\n");
printf("7. E-mail\n");
int choice;
printf("请输入选项: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入新的姓名: ");
scanf("%s", students[i].name);
break;
case 2:
printf("请输入新的年龄: ");
scanf("%d", &students[i].age);
break;
case 3:
printf("请输入新的性别: ");
scanf("%s", students[i].gender);
break;
case 4:
printf("请输入新的出生年月: ");
scanf("%s", students[i].birth);
break;
case 5:
printf("请输入新的地址: ");
scanf("%s", students[i].address);
break;
case 6:
printf("请输入新的电话: ");
scanf("%s", students[i].phone);
break;
case 7:
printf("请输入新的E-mail: ");
scanf("%s", students[i].email);
break;
default:
printf("无效的选择。\n");
break;
}
saveStudentsToFile(students, count);
printf("学生信息已修改。\n");
found = 1;
break;
}
}
}
void menu() {
printf("\n+-----------------------------------+\n");
printf("| 学生信息管理系统 |\n");
printf("+-----------------------------------+\n");
printf("| 1. 添加学生信息 |\n");
printf("| 2. 显示所有学生信息 |\n");
printf("| 3. 按学号查询学生信息 |\n");
printf("| 4. 按姓名查询学生信息 |\n");
printf("| 5. 删除学生信息 |\n");
printf("| 6. 修改学生信息 |\n");
printf("| 7. 退出系统 |\n");
printf("+-----------------------------------+\n");
printf("请选择功能: ");
}
void runMenu() {
Student students[MAX_STUDENTS];
int count = 0;
//加载学生信息
loadStudentsFromFile(students, &count);
int choice;
do {
menu();
scanf("%d", &choice);
switch (choice)
{
case 1:
addStudent(students, &count);
break;
case 2:
displayStudents(students, count);
break;
case 3:
searchStudentById(students, count);
break;
case 4:
searchStudentByName(students, count);
break;
case 5:
deleteStudentById(students, &count);
break;
case 6:
modifyStudentById(students, count);
break;
case 7:
printf("退出程序\n");
break;
default:
printf("无效的输入,请重新输入\n");
break;
}
// 清除输入缓冲区中的换行符
getchar();
// 暂停程序,等待用户按键
system("pause");
//clearScreen();
} while (choice != 7);
}
test.c
#include"studentInformationManagement.h"
int main() {
runMenu();
return 0;
}