用C语言做完整的学生管理系统
时间: 2023-08-26 21:09:40 浏览: 43
实现一个完整的学生管理系统需要以下模块:
1. 学生信息管理模块:包括学生信息的录入、修改、删除、查找和显示等功能。
2. 成绩管理模块:包括成绩的录入、修改、删除、查找和显示等功能。
3. 排名管理模块:包括对学生成绩进行排名和显示排名信息等功能。
4. 统计管理模块:包括对学生总人数、男女人数、平均成绩等信息进行统计和显示等功能。
下面是一个简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
typedef struct student {
char name[20];
char id[10];
int gender; // 0 for male, 1 for female
float score;
} Student;
Student students[MAX_STUDENT_NUM];
int student_count = 0;
void add_student() {
if (student_count >= MAX_STUDENT_NUM) {
printf("Too many students!\n");
return;
}
Student s;
printf("Input name: ");
scanf("%s", s.name);
printf("Input id: ");
scanf("%s", s.id);
printf("Input gender (0 for male, 1 for female): ");
scanf("%d", &s.gender);
printf("Input score: ");
scanf("%f", &s.score);
students[student_count++] = s;
printf("Add student successfully!\n");
}
void modify_student() {
char id[10];
printf("Input id of the student to modify: ");
scanf("%s", id);
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("Input new name: ");
scanf("%s", students[i].name);
printf("Input new gender (0 for male, 1 for female): ");
scanf("%d", &students[i].gender);
printf("Input new score: ");
scanf("%f", &students[i].score);
printf("Modify student successfully!\n");
return;
}
}
printf("Student not found!\n");
}
void delete_student() {
char id[10];
printf("Input id of the student to delete: ");
scanf("%s", id);
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].id, id) == 0) {
for (int j = i; j < student_count - 1; j++) {
students[j] = students[j + 1];
}
student_count--;
printf("Delete student successfully!\n");
return;
}
}
printf("Student not found!\n");
}
void search_student() {
char id[10];
printf("Input id of the student to search: ");
scanf("%s", id);
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("Name: %s\n", students[i].name);
printf("ID: %s\n", students[i].id);
printf("Gender: %s\n", students[i].gender == 0 ? "Male" : "Female");
printf("Score: %.2f\n", students[i].score);
return;
}
}
printf("Student not found!\n");
}
void show_students() {
printf("Name\tID\tGender\tScore\n");
for (int i = 0; i < student_count; i++) {
printf("%s\t%s\t%s\t%.2f\n", students[i].name, students[i].id, students[i].gender == 0 ? "Male" : "Female", students[i].score);
}
}
void rank_students() {
Student tmp;
for (int i = 0; i < student_count - 1; i++) {
for (int j = i + 1; j < student_count; j++) {
if (students[i].score < students[j].score) {
tmp = students[i];
students[i] = students[j];
students[j] = tmp;
}
}
}
printf("Rank\tName\tID\tGender\tScore\n");
for (int i = 0; i < student_count; i++) {
printf("%d\t%s\t%s\t%s\t%.2f\n", i + 1, students[i].name, students[i].id, students[i].gender == 0 ? "Male" : "Female", students[i].score);
}
}
void statistics() {
int male_count = 0;
int female_count = 0;
float total_score = 0;
for (int i = 0; i < student_count; i++) {
if (students[i].gender == 0) {
male_count++;
} else {
female_count++;
}
total_score += students[i].score;
}
printf("Total student count: %d\n", student_count);
printf("Male count: %d\n", male_count);
printf("Female count: %d\n", female_count);
printf("Average score: %.2f\n", total_score / student_count);
}
int main() {
int choice = -1;
while (choice != 0) {
printf("\n");
printf("1. Add student\n");
printf("2. Modify student\n");
printf("3. Delete student\n");
printf("4. Search student\n");
printf("5. Show students\n");
printf("6. Rank students\n");
printf("7. Statistics\n");
printf("0. Exit\n");
printf("Input your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
modify_student();
break;
case 3:
delete_student();
break;
case 4:
search_student();
break;
case 5:
show_students();
break;
case 6:
rank_students();
break;
case 7:
statistics();
break;
case 0:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
```
这只是一个简单的示例,实际的学生管理系统还需要更多的功能和优化,比如支持存储到文件、导入导出数据、密码保护等等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)