C语言编程,设计编写一个传统的小型学生信息管理系统,需要完整代码。 1、该系统可以用于管理一个班的学生信息。学生人数在50人左右。 2、该系统存放了有关学生的某些方面的信息:如学号、姓名、性别、出生日期、成绩、所在系等,具体包含的数据内容由设计者分析确定。 3、所有学生信息的数据结构及其存储结构由设计者分析确定。 4、所有学生的信息数据可以用文件形式存放。(可选项) 5、该系统具有相关的数据输入、输出、查询、修改、插入、删除、统计、排序等功能。(功能的多少设计者可根据需要自行调整,但必须具备输出、输出、查询、某一方面数据统计的基本功能。) 6、系统应具有友好的用户界面,并能够让用户通过菜单进行相应功能的选择。 7、选用适当的算法和数据结构,建立相应的程序代码。
时间: 2023-07-10 21:17:03 浏览: 91
以下是一个简单的学生信息管理系统的C语言代码,实现基本的增删改查和文件存储功能,具体细节可以根据实际需求进行修改和完善:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STU_NUM 50
#define FILE_NAME "student.dat"
typedef struct {
char id[20];
char name[20];
char gender[5];
char birth[20];
int score;
char department[20];
} Student;
Student stuList[MAX_STU_NUM];
int stuNum = 0;
void printMenu() {
printf("1. Add a student\n");
printf("2. Delete a student\n");
printf("3. Modify student information\n");
printf("4. Query student information\n");
printf("5. Display all students\n");
printf("6. Save to file\n");
printf("7. Load from file\n");
printf("8. Exit\n");
}
void addStudent() {
if (stuNum >= MAX_STU_NUM) {
printf("The student list is full!\n");
return;
}
Student newStu;
printf("Please input the student ID: ");
scanf("%s", newStu.id);
printf("Please input the student name: ");
scanf("%s", newStu.name);
printf("Please input the student gender: ");
scanf("%s", newStu.gender);
printf("Please input the student birth: ");
scanf("%s", newStu.birth);
printf("Please input the student score: ");
scanf("%d", &newStu.score);
printf("Please input the student department: ");
scanf("%s", newStu.department);
stuList[stuNum++] = newStu;
printf("Add student successfully!\n");
}
void deleteStudent() {
char id[20];
printf("Please input the ID of the student you want to delete: ");
scanf("%s", id);
int index = -1;
for (int i = 0; i < stuNum; i++) {
if (strcmp(stuList[i].id, id) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("The student is not found!\n");
return;
}
for (int i = index; i < stuNum - 1; i++) {
stuList[i] = stuList[i + 1];
}
stuNum--;
printf("Delete student successfully!\n");
}
void modifyStudent() {
char id[20];
printf("Please input the ID of the student you want to modify: ");
scanf("%s", id);
int index = -1;
for (int i = 0; i < stuNum; i++) {
if (strcmp(stuList[i].id, id) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("The student is not found!\n");
return;
}
Student newStu;
printf("Please input the new student ID (or press Enter to skip): ");
fgets(newStu.id, 20, stdin);
if (newStu.id[0] != '\n') {
newStu.id[strcspn(newStu.id, "\n")] = 0;
}
printf("Please input the new student name (or press Enter to skip): ");
fgets(newStu.name, 20, stdin);
if (newStu.name[0] != '\n') {
newStu.name[strcspn(newStu.name, "\n")] = 0;
}
printf("Please input the new student gender (or press Enter to skip): ");
fgets(newStu.gender, 5, stdin);
if (newStu.gender[0] != '\n') {
newStu.gender[strcspn(newStu.gender, "\n")] = 0;
}
printf("Please input the new student birth (or press Enter to skip): ");
fgets(newStu.birth, 20, stdin);
if (newStu.birth[0] != '\n') {
newStu.birth[strcspn(newStu.birth, "\n")] = 0;
}
printf("Please input the new student score (or press Enter to skip): ");
char buf[10];
fgets(buf, 10, stdin);
if (buf[0] != '\n') {
newStu.score = atoi(buf);
} else {
newStu.score = stuList[index].score;
}
printf("Please input the new student department (or press Enter to skip): ");
fgets(newStu.department, 20, stdin);
if (newStu.department[0] != '\n') {
newStu.department[strcspn(newStu.department, "\n")] = 0;
}
stuList[index] = newStu;
printf("Modify student information successfully!\n");
}
void queryStudent() {
char id[20];
printf("Please input the ID of the student you want to query: ");
scanf("%s", id);
int index = -1;
for (int i = 0; i < stuNum; i++) {
if (strcmp(stuList[i].id, id) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("The student is not found!\n");
return;
}
printf("ID: %s\n", stuList[index].id);
printf("Name: %s\n", stuList[index].name);
printf("Gender: %s\n", stuList[index].gender);
printf("Birth: %s\n", stuList[index].birth);
printf("Score: %d\n", stuList[index].score);
printf("Department: %s\n", stuList[index].department);
}
void displayAll() {
if (stuNum == 0) {
printf("No student information is stored!\n");
return;
}
printf("ID\tName\tGender\tBirth\tScore\tDepartment\n");
for (int i = 0; i < stuNum; i++) {
printf("%s\t%s\t%s\t%s\t%d\t%s\n", stuList[i].id, stuList[i].name, stuList[i].gender, stuList[i].birth, stuList[i].score, stuList[i].department);
}
}
void saveToFile() {
FILE *fp = fopen(FILE_NAME, "wb");
if (fp == NULL) {
printf("Failed to open file!\n");
return;
}
fwrite(&stuNum, sizeof(int), 1, fp);
fwrite(stuList, sizeof(Student), stuNum, fp);
fclose(fp);
printf("Save to file successfully!\n");
}
void loadFromFile() {
FILE *fp = fopen(FILE_NAME, "rb");
if (fp == NULL) {
printf("Failed to open file!\n");
return;
}
fread(&stuNum, sizeof(int), 1, fp);
fread(stuList, sizeof(Student), stuNum, fp);
fclose(fp);
printf("Load from file successfully!\n");
}
int main() {
int choice;
while (1) {
printMenu();
printf("Please input your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
queryStudent();
break;
case 5:
displayAll();
break;
case 6:
saveToFile();
break;
case 7:
loadFromFile();
break;
case 8:
exit(0);
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
```
阅读全文