C语言学生信息管理系统,包含统计学生信息功能
时间: 2023-07-19 08:07:43 浏览: 111
好的,一个简单的C语言学生信息管理系统可以包含以下几个功能:
1. 添加学生信息:输入学生的姓名、学号、性别、年龄等信息,将其保存到文件中。
2. 修改学生信息:根据学生的学号或者姓名,找到对应的学生信息并修改。
3. 删除学生信息:根据学生的学号或者姓名,找到对应的学生信息并从文件中删除。
4. 查询学生信息:根据学生的学号或者姓名,找到对应的学生信息并打印出来。
5. 统计学生信息:统计总人数、男女人数、平均年龄等信息。
下面是一个简单的示例代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student {
char name[20];
int id;
char gender[10];
int age;
};
void add_student(struct student *stu, FILE *fp);
void modify_student(struct student *stu, FILE *fp);
void delete_student(struct student *stu, FILE *fp);
void query_student(struct student *stu, FILE *fp);
void statistic_student(struct student *stu, FILE *fp);
int main() {
struct student stu;
FILE *fp = fopen("student.txt", "a+"); // 打开文件
int choice;
do {
printf("\n\n\t\t\t\tStudent Information Management System\n");
printf("\t\t\t===============================================\n");
printf("\t\t\t1. Add Student Information\n");
printf("\t\t\t2. Modify Student Information\n");
printf("\t\t\t3. Delete Student Information\n");
printf("\t\t\t4. Query Student Information\n");
printf("\t\t\t5. Statistic Student Information\n");
printf("\t\t\t6. Exit\n");
printf("\t\t\t===============================================\n");
printf("\t\t\tEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(&stu, fp);
break;
case 2:
modify_student(&stu, fp);
break;
case 3:
delete_student(&stu, fp);
break;
case 4:
query_student(&stu, fp);
break;
case 5:
statistic_student(&stu, fp);
break;
case 6:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != 6);
fclose(fp); // 关闭文件
return 0;
}
void add_student(struct student *stu, FILE *fp) {
printf("\nEnter Student Name: ");
scanf("%s", stu->name);
printf("Enter Student ID: ");
scanf("%d", &stu->id);
printf("Enter Student Gender: ");
scanf("%s", stu->gender);
printf("Enter Student Age: ");
scanf("%d", &stu->age);
fwrite(stu, sizeof(struct student), 1, fp); // 将学生信息写入文件
printf("\nStudent Information Added Successfully!\n");
}
void modify_student(struct student *stu, FILE *fp) {
int id;
char name[20];
printf("\nEnter Student ID or Name to Modify: ");
scanf("%s", name);
fseek(fp, 0, SEEK_SET); // 将文件指针移动到文件开头
while (fread(stu, sizeof(struct student), 1, fp) == 1) {
if (strcmp(stu->name, name) == 0 || stu->id == atoi(name)) { // 根据学号或姓名找到对应学生信息
printf("Enter New Student Name: ");
scanf("%s", stu->name);
printf("Enter New Student ID: ");
scanf("%d", &stu->id);
printf("Enter New Student Gender: ");
scanf("%s", stu->gender);
printf("Enter New Student Age: ");
scanf("%d", &stu->age);
fseek(fp, -sizeof(struct student), SEEK_CUR); // 将文件指针移动到当前学生信息的位置
fwrite(stu, sizeof(struct student), 1, fp); // 将修改后的学生信息写入文件
printf("\nStudent Information Modified Successfully!\n");
return;
}
}
printf("\nStudent Information Not Found!\n");
}
void delete_student(struct student *stu, FILE *fp) {
int id;
char name[20];
printf("\nEnter Student ID or Name to Delete: ");
scanf("%s", name);
FILE *temp = fopen("temp.txt", "w"); // 打开临时文件
fseek(fp, 0, SEEK_SET); // 将文件指针移动到文件开头
while (fread(stu, sizeof(struct student), 1, fp) == 1) {
if (strcmp(stu->name, name) != 0 && stu->id != atoi(name)) { // 将不需要删除的学生信息写入临时文件
fwrite(stu, sizeof(struct student), 1, temp);
}
}
fclose(fp);
fclose(temp);
remove("student.txt"); // 删除原文件
rename("temp.txt", "student.txt"); // 重命名临时文件
fp = fopen("student.txt", "a+"); // 重新打开文件
printf("\nStudent Information Deleted Successfully!\n");
}
void query_student(struct student *stu, FILE *fp) {
int id;
char name[20];
printf("\nEnter Student ID or Name to Query: ");
scanf("%s", name);
fseek(fp, 0, SEEK_SET); // 将文件指针移动到文件开头
while (fread(stu, sizeof(struct student), 1, fp) == 1) {
if (strcmp(stu->name, name) == 0 || stu->id == atoi(name)) { // 根据学号或姓名找到对应学生信息并打印
printf("\nStudent Name: %s\n", stu->name);
printf("Student ID: %d\n", stu->id);
printf("Student Gender: %s\n", stu->gender);
printf("Student Age: %d\n", stu->age);
return;
}
}
printf("\nStudent Information Not Found!\n");
}
void statistic_student(struct student *stu, FILE *fp) {
int total = 0, male = 0, female = 0, age_total = 0;
float age_avg;
fseek(fp, 0, SEEK_SET); // 将文件指针移动到文件开头
while (fread(stu, sizeof(struct student), 1, fp) == 1) {
total++; // 总人数加1
if (strcmp(stu->gender, "male") == 0) {
male++; // 男生人数加1
} else if (strcmp(stu->gender, "female") == 0) {
female++; // 女生人数加1
}
age_total += stu->age; // 年龄累加
}
age_avg = (float)age_total / total; // 平均年龄计算
printf("\nTotal Number of Students: %d\n", total);
printf("Number of Male Students: %d\n", male);
printf("Number of Female Students: %d\n", female);
printf("Average Age of Students: %.2f\n", age_avg);
}
```
注意,在上面的代码中,我们将学生信息保存在了一个文件中,每个学生信息占用一个固定大小的结构体。当需要修改或删除学生信息时,我们需要先找到对应的学生信息在文件中的位置,然后再进行操作。当需要查询或统计学生信息时,我们需要遍历整个文件并逐个处理每个学生信息。
阅读全文