用c语言写一个学生学籍管理系统 主要功能: 欢迎及菜单页面 通过文本文件读入学生信息 新增学生(学号不可重复) 录入成绩 删除学生 展示所有学生信息 计算最高最低平均成绩 统计ABCD每个分段的人数 按成绩从高到低排序 按学号排序 退出前将所有学生信息回写进文本文档
时间: 2023-06-24 16:09:19 浏览: 83
c语言学生学籍管理系统,功能强大
很好,以下是一个简单的学生学籍管理系统的C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
char id[20];
char name[20];
int score;
};
int num_students = 0;
struct student students[MAX_STUDENTS];
void load_students(char *filename) {
FILE *fp = fopen(filename, "r");
if (!fp) {
printf("Error: cannot open file %s for reading\n", filename);
return;
}
char line[100];
while (fgets(line, sizeof(line), fp)) {
char *p = strtok(line, ",");
strcpy(students[num_students].id, p);
p = strtok(NULL, ",");
strcpy(students[num_students].name, p);
p = strtok(NULL, ",");
students[num_students].score = atoi(p);
num_students++;
}
fclose(fp);
}
void save_students(char *filename) {
FILE *fp = fopen(filename, "w");
if (!fp) {
printf("Error: cannot open file %s for writing\n", filename);
return;
}
for (int i = 0; i < num_students; i++) {
fprintf(fp, "%s,%s,%d\n", students[i].id, students[i].name, students[i].score);
}
fclose(fp);
}
struct student *find_student(char *id) {
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
return &students[i];
}
}
return NULL;
}
void add_student() {
struct student s;
printf("Enter student ID: ");
scanf("%s", s.id);
if (find_student(s.id)) {
printf("Error: student with ID %s already exists\n", s.id);
return;
}
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter student score: ");
scanf("%d", &s.score);
students[num_students++] = s;
printf("Student added successfully\n");
}
void remove_student() {
char id[20];
printf("Enter student ID: ");
scanf("%s", id);
struct student *s = find_student(id);
if (!s) {
printf("Error: student with ID %s does not exist\n", id);
return;
}
for (int i = 0; i < num_students; i++) {
if (&students[i] == s) {
for (int j = i; j < num_students - 1; j++) {
students[j] = students[j + 1];
}
num_students--;
printf("Student removed successfully\n");
return;
}
}
}
void display_all_students() {
printf("ID\tName\tScore\n");
for (int i = 0; i < num_students; i++) {
printf("%s\t%s\t%d\n", students[i].id, students[i].name, students[i].score);
}
}
void compute_statistics() {
int min_score = students[0].score;
int max_score = students[0].score;
int sum_score = 0;
int count_a = 0, count_b = 0, count_c = 0, count_d = 0;
for (int i = 0; i < num_students; i++) {
int score = students[i].score;
if (score < min_score) {
min_score = score;
}
if (score > max_score) {
max_score = score;
}
sum_score += score;
if (score >= 90) {
count_a++;
} else if (score >= 80) {
count_b++;
} else if (score >= 70) {
count_c++;
} else {
count_d++;
}
}
double avg_score = (double) sum_score / num_students;
printf("Minimum score: %d\n", min_score);
printf("Maximum score: %d\n", max_score);
printf("Average score: %.2f\n", avg_score);
printf("A: %d\n", count_a);
printf("B: %d\n", count_b);
printf("C: %d\n", count_c);
printf("D: %d\n", count_d);
}
void sort_by_score() {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[i].score < students[j].score) {
struct student tmp = students[i];
students[i] = students[j];
students[j] = tmp;
}
}
}
printf("Students sorted by score\n");
}
void sort_by_id() {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (strcmp(students[i].id, students[j].id) > 0) {
struct student tmp = students[i];
students[i] = students[j];
students[j] = tmp;
}
}
}
printf("Students sorted by ID\n");
}
int main() {
char filename[] = "students.txt";
load_students(filename);
printf("Welcome to Student Management System!\n");
while (1) {
printf("\nMenu:\n");
printf("1. Add student\n");
printf("2. Remove student\n");
printf("3. Display all students\n");
printf("4. Compute statistics\n");
printf("5. Sort by score\n");
printf("6. Sort by ID\n");
printf("7. Save & quit\n");
int choice;
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
remove_student();
break;
case 3:
display_all_students();
break;
case 4:
compute_statistics();
break;
case 5:
sort_by_score();
break;
case 6:
sort_by_id();
break;
case 7:
save_students(filename);
printf("Goodbye!\n");
return 0;
default:
printf("Invalid choice, please try again\n");
}
}
}
```
注意,这只是一个简单的示例,实际应用中可能需要更多的功能和更复杂的逻辑。
阅读全文