C语言学生管理系统怎么做,c语言学生管理系统
学生管理系统是一种常见的信息管理系统,它可以帮助学校或教育机构更好地管理学生信息,包括学生基本信息、课程成绩、考勤情况等。在C语言中,我们可以使用结构体和文件操作来实现一个简单的学生管理系统。
首先,我们需要定义一个学生结构体,包含学生的基本信息,如姓名、学号、性别、年龄等,以及课程成绩和考勤情况等信息。例如:
```
struct student {
char name[20];
int id;
char gender;
int age;
float score;
int attendance;
};
```
接下来,我们可以使用文件操作来实现学生信息的存储和读取。我们可以将学生信息保存在一个文本文件中,每行表示一个学生的信息,不同字段之间用空格或逗号隔开。例如:
```
张三 1001 M 18 85.5 90
李四 1002 F 19 92.0 95
王五 1003 M 20 78.5 80
```
我们可以使用fopen函数打开文件,使用fscanf函数读取文件中的数据,并将其存储到一个学生结构体数组中。例如:
```
FILE *fp;
struct student stu[100];
int i = 0;
fp = fopen("students.txt","r");
if (fp == NULL) {
printf("File open error!\n");
return -1;
}
while (fscanf(fp,"%s %d %c %d %f %d", stu[i].name, &stu[i].id, &stu[i].gender, &stu[i].age, &stu[i].score, &stu[i].attendance) != EOF) {
i++;
}
fclose(fp);
```
接下来,我们可以实现一些基本的功能,如添加学生、删除学生、修改学生信息、查询学生信息等。例如,添加学生可以通过向学生结构体数组中添加一个新的元素来实现:
```
void add_student(struct student stu[], int *count) {
printf("Please input the student's name, id, gender, age, score and attendance:\n");
scanf("%s %d %c %d %f %d", stu[*count].name, &stu[*count].id, &stu[*count].gender, &stu[*count].age, &stu[*count].score, &stu[*count].attendance);
(*count)++;
}
```
删除学生可以通过输入学生的学号来实现:
```
void delete_student(struct student stu[], int *count) {
int id, i, j;
printf("Please input the student's id:\n");
scanf("%d", &id);
for (i = 0; i < *count; i++) {
if (stu[i].id == id) {
for (j = i; j < *count - 1; j++) {
stu[j] = stu[j + 1];
}
(*count)--;
printf("Delete success!\n");
return;
}
}
printf("Student not found!\n");
}
```
修改学生信息可以通过输入学生的学号和要修改的字段来实现:
```
void modify_student(struct student stu[], int count) {
int id, choice, i;
printf("Please input the student's id:\n");
scanf("%d", &id);
for (i = 0; i < count; i++) {
if (stu[i].id == id) {
printf("Please choose the field to modify:\n");
printf("1. Name\n");
printf("2. Gender\n");
printf("3. Age\n");
printf("4. Score\n");
printf("5. Attendance\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Please input the new name:\n");
scanf("%s", stu[i].name);
break;
case 2:
printf("Please input the new gender:\n");
scanf("%c", &stu[i].gender);
break;
case 3:
printf("Please input the new age:\n");
scanf("%d", &stu[i].age);
break;
case 4:
printf("Please input the new score:\n");
scanf("%f", &stu[i].score);
break;
case 5:
printf("Please input the new attendance:\n");
scanf("%d", &stu[i].attendance);
break;
default:
printf("Invalid choice!\n");
break;
}
printf("Modify success!\n");
return;
}
}
printf("Student not found!\n");
}
```
查询学生信息可以通过输入学生的学号或姓名来实现:
```
void query_student(struct student stu[], int count) {
int choice, i;
char name[20];
printf("Please choose the query method:\n");
printf("1. By id\n");
printf("2. By name\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Please input the student's id:\n");
int id;
scanf("%d", &id);
for (i = 0; i < count; i++) {
if (stu[i].id == id) {
printf("Name: %s\n", stu[i].name);
printf("Gender: %c\n", stu[i].gender);
printf("Age: %d\n", stu[i].age);
printf("Score: %.1f\n", stu[i].score);
printf("Attendance: %d\n", stu[i].attendance);
return;
}
}
printf("Student not found!\n");
break;
case 2:
printf("Please input the student's name:\n");
scanf("%s", name);
for (i = 0; i < count; i++) {
if (strcmp(stu[i].name, name) == 0) {
printf("Id: %d\n", stu[i].id);
printf("Gender: %c\n", stu[i].gender);
printf("Age: %d\n", stu[i].age);
printf("Score: %.1f\n", stu[i].score);
printf("Attendance: %d\n", stu[i].attendance);
return;
}
}
printf("Student not found!\n");
break;
default:
printf("Invalid choice!\n");
break;
}
}
```
最后,我们可以编写一个主函数,调用上述函数来实现完整的学生管理系统。例如:
```
int main() {
struct student stu[100];
int count = 0;
int choice;
while (1) {
printf("Please choose the operation:\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Modify student\n");
printf("4. Query student\n");
printf("5. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(stu, &count);
break;
case 2:
delete_student(stu, &count);
break;
case 3:
modify_student(stu, count);
break;
case 4:
query_student(stu, count);
break;
case 5:
return 0;
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
```
以上就是一个简单的C语言学生管理系统的实现方法。当然,这只是一个基础的框架,还可以根据实际需求进行扩展和优化。
推荐阅读
- dnf已经有红字的怎么把红字洗掉,dnf已经洗出红字的装备怎么洗掉
- 海蛏子的家常做法,海蛏子的做法大全
- 微信聊天记录怎么恢复吗,微信聊天记录怎么恢复方法:
- qq飞车帧数如何能锁,QQ飞车帧数如何修改
- 1盎司相当于多少克黄金,盎司等于多少克及一盎司黄金等于多少克
- 怎么恢复路由器出厂,怎样恢复路由器出厂设置
- 触手tvlogo怎么买,如何录制触手TV文章
- 藏语常用问候语及礼貌语 旅行必备
- 腾讯文章的会员怎么取消自动续费,腾讯文章VIP会员怎么取消自动续费设置
- 支付宝绑定银行卡与银行预留手机号不符,支付宝绑定银行卡提示与预留手机号码不一致
- 如何饲养土狗,饲养土狗的实用方法
- 重装机兵最终明奇1.92红狼怎么加入,重装机兵最终明奇1.92攻略
- 华为云电脑是什么,怎么用
- 关山牧场住宿攻略,关山牧场出游攻略
- dnf云上长安搬砖攻略,DNF暗之血迹套装攻略
- 电脑双引号怎么打出来是反的,电脑双引号怎么打
- 生育津贴怎么查询进度 网上查询,生育津贴怎么查询
- 我的世界活塞怎么做成的,我的世界活塞怎么做
- word如何设置主题字体,手机QQ怎样设置主题和字体呢
- 家庭用电热水器和燃气热水器哪个好,电热水器和燃气热水器哪个好