题目:学生籍贯管理系统 内容:编制一个学生籍贯信息记录簿,每个学生信息包括:学号、姓名、籍贯、通信地址,以顺序存储结构设计程序实现以下功能。 1、输入学生信息并以磁盘文件保存; 2、读取磁盘文件并显示输出所有学生的籍贯信息; 3、按学号或姓名查询其籍贯; 4、按籍贯查询并输出该籍贯的所有学生; 5、能添加、删除(按学号)和修改(按学号)学生的籍贯信息; 6、显示输出上海籍和非上海籍学生的信息并可分别存盘; 7、浏览所有学生的籍贯信息; 8、按学号进行排序。
时间: 2023-08-13 19:05:01 浏览: 148
以下是一个基于C语言的学生籍贯管理系统的简单实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 学生信息结构体
typedef struct {
char id[10]; // 学号
char name[20]; // 姓名
char hometown[20]; // 籍贯
char address[50]; // 通信地址
} Student;
// 存储所有学生信息的数组
Student students[100];
// 学生数量
int studentCount = 0;
// 从文件中读取学生信息
void readStudentsFromFile(char *fileName) {
FILE *fp = fopen(fileName, "r");
if (fp == NULL) {
printf("File %s not found.\n", fileName);
return;
}
// 逐行读取学生信息
while (feof(fp) == 0) {
Student student;
fscanf(fp, "%s %s %s %s", student.id, student.name, student.hometown, student.address);
students[studentCount] = student;
studentCount++;
}
fclose(fp);
}
// 将所有学生信息保存到文件中
void saveStudentsToFile(char *fileName) {
FILE *fp = fopen(fileName, "w");
for (int i = 0; i < studentCount; i++) {
fprintf(fp, "%s %s %s %s\n", students[i].id, students[i].name, students[i].hometown, students[i].address);
}
fclose(fp);
}
// 添加学生
void addStudent() {
Student student;
printf("Enter student's id: ");
scanf("%s", student.id);
printf("Enter student's name: ");
scanf("%s", student.name);
printf("Enter student's hometown: ");
scanf("%s", student.hometown);
printf("Enter student's address: ");
scanf("%s", student.address);
students[studentCount] = student;
studentCount++;
}
// 删除学生
void deleteStudent() {
char id[10];
printf("Enter student's id to delete: ");
scanf("%s", id);
int index = -1;
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].id, id) == 0) {
index = i;
break;
}
}
if (index != -1) {
for (int i = index; i < studentCount; i++) {
students[i] = students[i + 1];
}
studentCount--;
printf("Student with id %s deleted.\n", id);
} else {
printf("Student with id %s not found.\n", id);
}
}
// 修改学生
void modifyStudent() {
char id[10];
printf("Enter student's id to modify: ");
scanf("%s", id);
int index = -1;
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].id, id) == 0) {
index = i;
break;
}
}
if (index != -1) {
printf("Enter new hometown for student with id %s: ", id);
scanf("%s", students[index].hometown);
printf("Enter new address for student with id %s: ", id);
scanf("%s", students[index].address);
printf("Student with id %s modified.\n", id);
} else {
printf("Student with id %s not found.\n", id);
}
}
// 按学号或姓名查询学生
void searchStudent() {
int option;
printf("Search by (1) id or (2) name? ");
scanf("%d", &option);
char keyword[20];
printf("Enter keyword: ");
scanf("%s", keyword);
for (int i = 0; i < studentCount; i++) {
if (option == 1 && strcmp(students[i].id, keyword) == 0) {
printf("%s %s %s %s\n", students[i].id, students[i].name, students[i].hometown, students[i].address);
} else if (option == 2 && strcmp(students[i].name, keyword) == 0) {
printf("%s %s %s %s\n", students[i].id, students[i].name, students[i].hometown, students[i].address);
}
}
}
// 按籍贯查询学生
void searchByHometown() {
char hometown[20];
printf("Enter hometown: ");
scanf("%s", hometown);
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].hometown, hometown) == 0) {
printf("%s %s %s %s\n", students[i].id, students[i].name, students[i].hometown, students[i].address);
}
}
}
// 显示所有学生信息
void displayAllStudents() {
for (int i = 0; i < studentCount; i++) {
printf("%s %s %s %s\n", students[i].id, students[i].name, students[i].hometown, students[i].address);
}
}
// 显示上海籍和非上海籍学生信息
void displayShanghaiStudents() {
FILE *shanghaiFile = fopen("shanghai.txt", "w");
FILE *nonShanghaiFile = fopen("non_shanghai.txt", "w");
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].hometown, "Shanghai") == 0) {
fprintf(shanghaiFile, "%s %s %s %s\n", students[i].id, students[i].name, students[i].hometown, students[i].address);
} else {
fprintf(nonShanghaiFile, "%s %s %s %s\n", students[i].id, students[i].name, students[i].hometown, students[i].address);
}
}
fclose(shanghaiFile);
fclose(nonShanghaiFile);
}
// 按学号排序
void sortStudentsById() {
for (int i = 0; i < studentCount - 1; i++) {
for (int j = 0; j < studentCount - i - 1; j++) {
if (strcmp(students[j].id, students[j + 1].id) > 0) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
int main() {
char fileName[50] = "students.txt";
int option = 0;
// 读取学生信息文件
readStudentsFromFile(fileName);
while (option != 9) {
printf("\nPlease choose an option:\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Modify student\n");
printf("4. Search student\n");
printf("5. Search by hometown\n");
printf("6. Display all students\n");
printf("7. Display Shanghai and non-Shanghai students\n");
printf("8. Sort students by id\n");
printf("9. Exit\n");
scanf("%d", &option);
switch (option) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
searchStudent();
break;
case 5:
searchByHometown();
break;
case 6:
displayAllStudents();
break;
case 7:
displayShanghaiStudents();
break;
case 8:
sortStudentsById();
printf("Students sorted by id.\n");
break;
case 9:
printf("Saving students to file %s...\n", fileName);
saveStudentsToFile(fileName);
printf("Students saved to file.\n");
break;
default:
printf("Invalid option.\n");
break;
}
}
return 0;
}
```
该程序使用了一个结构体来存储学生信息,并通过一个数组来存储所有学生信息。程序实现了输入学生信息、读取磁盘文件、查询学生籍贯、添加、删除和修改学生信息、分别显示上海籍和非上海籍学生信息、浏览所有学生信息、按学号排序等功能。
阅读全文