能帮我用c语言做一个包含注册,登陆, 添加学生信息,查找学生信息,删除学生信息,显示学生信息列表的学生信息管理系统的程序吗?
时间: 2024-03-13 18:44:20 浏览: 23
当然可以!这个程序需要用到文件读写、字符串处理等技术,需要一定的编程经验和基础。以下是一个简单的示例,您可以根据自己的需求进行修改和完善。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100 // 最大学生数
#define MAX_NAME_LEN 20 // 姓名最大长度
#define MAX_ID_LEN 10 // 学号最大长度
// 学生信息结构体
typedef struct {
char id[MAX_ID_LEN];
char name[MAX_NAME_LEN];
int age;
float score;
} Student;
// 学生信息数组
Student students[MAX_STUDENT_NUM];
int student_num = 0;
// 注册
void register_student() {
char id[MAX_ID_LEN];
char name[MAX_NAME_LEN];
int age;
float score;
printf("请输入学号:");
scanf("%s", id);
printf("请输入姓名:");
scanf("%s", name);
printf("请输入年龄:");
scanf("%d", &age);
printf("请输入成绩:");
scanf("%f", &score);
// 检查是否已经存在该学生
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("该学号已经被注册!\n");
return;
}
}
// 添加学生信息
strcpy(students[student_num].id, id);
strcpy(students[student_num].name, name);
students[student_num].age = age;
students[student_num].score = score;
student_num++;
printf("注册成功!\n");
}
// 登录
int login() {
char id[MAX_ID_LEN];
printf("请输入学号:");
scanf("%s", id);
// 查找学生信息
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("欢迎,%s!\n", students[i].name);
return i;
}
}
printf("该学号未注册!\n");
return -1;
}
// 添加学生信息
void add_student_info() {
int index = login();
if (index == -1) {
return;
}
printf("请输入要添加的信息:\n");
printf("姓名:");
scanf("%s", students[index].name);
printf("年龄:");
scanf("%d", &students[index].age);
printf("成绩:");
scanf("%f", &students[index].score);
printf("添加成功!\n");
}
// 查找学生信息
void search_student_info() {
int index = login();
if (index == -1) {
return;
}
printf("学号:%s\n", students[index].id);
printf("姓名:%s\n", students[index].name);
printf("年龄:%d\n", students[index].age);
printf("成绩:%f\n", students[index].score);
}
// 删除学生信息
void delete_student_info() {
int index = login();
if (index == -1) {
return;
}
// 删除学生信息
for (int i = index; i < student_num - 1; i++) {
strcpy(students[i].id, students[i+1].id);
strcpy(students[i].name, students[i+1].name);
students[i].age = students[i+1].age;
students[i].score = students[i+1].score;
}
student_num--;
printf("删除成功!\n");
}
// 显示学生信息列表
void show_student_info_list() {
printf("学号\t姓名\t年龄\t成绩\n");
for (int i = 0; i < student_num; i++) {
printf("%s\t%s\t%d\t%f\n", students[i].id, students[i].name, students[i].age, students[i].score);
}
}
// 保存学生信息到文件
void save_student_info_to_file() {
FILE *fp;
fp = fopen("students.txt", "w");
fprintf(fp, "学号\t姓名\t年龄\t成绩\n");
for (int i = 0; i < student_num; i++) {
fprintf(fp, "%s\t%s\t%d\t%f\n", students[i].id, students[i].name, students[i].age, students[i].score);
}
fclose(fp);
printf("保存成功!\n");
}
// 从文件中读取学生信息
void load_student_info_from_file() {
FILE *fp;
fp = fopen("students.txt", "r");
if (fp == NULL) {
printf("文件不存在!\n");
return;
}
char line[100];
fgets(line, 100, fp); // 读取表头
while (fgets(line, 100, fp) != NULL) {
sscanf(line, "%s\t%s\t%d\t%f\n", students[student_num].id, students[student_num].name, &students[student_num].age, &students[student_num].score);
student_num++;
}
fclose(fp);
printf("读取成功!\n");
}
int main() {
int choice;
do {
printf("=======学生信息管理系统=======\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("0.退出\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
register_student();
break;
case 2:
login();
break;
case 3:
add_student_info();
break;
case 4:
search_student_info();
break;
case 5:
delete_student_info();
break;
case 6:
show_student_info_list();
break;
case 7:
save_student_info_to_file();
break;
case 8:
load_student_info_from_file();
break;
case 0:
printf("谢谢使用!\n");
break;
default:
printf("无效选项,请重新输入!\n");
break;
}
} while (choice != 0);
return 0;
}
```
注意:这只是一个简单的示例,可能存在一些不足之处,例如输入验证、异常处理等,您需要自行完善。另外,为了方便演示,这个程序将学生信息保存到了一个文本文件中,实际应用中您可能需要使用数据库等更加高效和安全的方式来保存数据。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)