写一个c语言写出学生管理系统,支持增删改查和存入文件操作
时间: 2023-09-11 17:05:19 浏览: 194
### 回答1:
好的,我可以回答这个问题。学生管理系统可以使用结构体来存储学生的信息,使用链表来实现增删改查的操作。存入文件操作可以使用文件读写函数来实现。以下是一个简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_ID_LEN 10
typedef struct student {
char name[MAX_NAME_LEN];
char id[MAX_ID_LEN];
struct student *next;
} Student;
Student *head = NULL;
void add_student() {
Student *new_student = (Student *)malloc(sizeof(Student));
printf("请输入学生姓名:");
scanf("%s", new_student->name);
printf("请输入学生学号:");
scanf("%s", new_student->id);
new_student->next = head;
head = new_student;
printf("添加成功!\n");
}
void delete_student() {
char id[MAX_ID_LEN];
printf("请输入要删除的学生学号:");
scanf("%s", id);
Student *prev = NULL;
Student *curr = head;
while (curr != NULL) {
if (strcmp(curr->id, id) == ) {
if (prev == NULL) {
head = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
printf("删除成功!\n");
return;
}
prev = curr;
curr = curr->next;
}
printf("未找到该学生!\n");
}
void modify_student() {
char id[MAX_ID_LEN];
printf("请输入要修改的学生学号:");
scanf("%s", id);
Student *curr = head;
while (curr != NULL) {
if (strcmp(curr->id, id) == ) {
printf("请输入新的学生姓名:");
scanf("%s", curr->name);
printf("修改成功!\n");
return;
}
curr = curr->next;
}
printf("未找到该学生!\n");
}
void search_student() {
char id[MAX_ID_LEN];
printf("请输入要查找的学生学号:");
scanf("%s", id);
Student *curr = head;
while (curr != NULL) {
if (strcmp(curr->id, id) == ) {
printf("学生姓名:%s\n", curr->name);
printf("学生学号:%s\n", curr->id);
return;
}
curr = curr->next;
}
printf("未找到该学生!\n");
}
void save_to_file() {
FILE *fp = fopen("students.txt", "w");
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
Student *curr = head;
while (curr != NULL) {
fprintf(fp, "%s %s\n", curr->name, curr->id);
curr = curr->next;
}
fclose(fp);
printf("保存成功!\n");
}
void load_from_file() {
FILE *fp = fopen("students.txt", "r");
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
char name[MAX_NAME_LEN];
char id[MAX_ID_LEN];
while (fscanf(fp, "%s %s", name, id) != EOF) {
Student *new_student = (Student *)malloc(sizeof(Student));
strcpy(new_student->name, name);
strcpy(new_student->id, id);
new_student->next = head;
head = new_student;
}
fclose(fp);
printf("加载成功!\n");
}
int main() {
int choice;
while (1) {
printf("请选择操作:\n");
printf("1. 添加学生\n");
printf("2. 删除学生\n");
printf("3. 修改学生\n");
printf("4. 查找学生\n");
printf("5. 保存到文件\n");
printf("6. 从文件加载\n");
printf("7. 退出程序\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
modify_student();
break;
case 4:
search_student();
break;
case 5:
save_to_file();
break;
case 6:
load_from_file();
break;
case 7:
return ;
default:
printf("无效的选择!\n");
}
}
}
```
### 回答2:
学生管理系统是一个基于C语言的程序,用于对学生信息进行增加、删除、修改和查询,并能够将学生信息存储到文件中。以下是一个简单实现的示例:
```C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct Student {
char name[50];
int age;
int grade;
};
struct Student students[MAX_STUDENTS];
int numStudents = 0;
void addStudent() {
if (numStudents == MAX_STUDENTS) {
printf("学生人数已达到最大值!\n");
return;
}
struct Student newStudent;
printf("请输入学生姓名:");
scanf("%s", newStudent.name);
printf("请输入学生年龄:");
scanf("%d", &newStudent.age);
printf("请输入学生成绩:");
scanf("%d", &newStudent.grade);
students[numStudents] = newStudent;
numStudents++;
printf("添加成功!\n");
}
void deleteStudent() {
if (numStudents == 0) {
printf("学生列表为空!\n");
return;
}
char name[50];
printf("请输入要删除的学生姓名:");
scanf("%s", name);
int index = -1;
for (int i = 0; i < numStudents; i++) {
if (strcmp(students[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到该学生!\n");
} else {
for (; index < numStudents - 1; index++) {
students[index] = students[index + 1];
}
numStudents--;
printf("删除成功!\n");
}
}
void modifyStudent() {
if (numStudents == 0) {
printf("学生列表为空!\n");
return;
}
char name[50];
printf("请输入要修改的学生姓名:");
scanf("%s", name);
int index = -1;
for (int i = 0; i < numStudents; i++) {
if (strcmp(students[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到该学生!\n");
} else {
printf("请输入新的学生姓名:");
scanf("%s", students[index].name);
printf("请输入新的学生年龄:");
scanf("%d", &students[index].age);
printf("请输入新的学生成绩:");
scanf("%d", &students[index].grade);
printf("修改成功!\n");
}
}
void searchStudent() {
if (numStudents == 0) {
printf("学生列表为空!\n");
return;
}
char name[50];
printf("请输入要查询的学生姓名:");
scanf("%s", name);
int index = -1;
for (int i = 0; i < numStudents; i++) {
if (strcmp(students[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到该学生!\n");
} else {
printf("学生姓名:%s\n", students[index].name);
printf("学生年龄:%d\n", students[index].age);
printf("学生成绩:%d\n", students[index].grade);
}
}
void saveToFile() {
FILE *file;
file = fopen("students.txt", "w");
if (file == NULL) {
printf("无法打开文件!\n");
return;
}
for (int i = 0; i < numStudents; i++) {
fprintf(file, "%s %d %d\n", students[i].name, students[i].age, students[i].grade);
}
fclose(file);
printf("保存成功!\n");
}
int main() {
int choice;
do {
printf("学生管理系统\n");
printf("1. 添加学生\n");
printf("2. 删除学生\n");
printf("3. 修改学生\n");
printf("4. 查询学生\n");
printf("5. 保存到文件\n");
printf("0. 退出系统\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
searchStudent();
break;
case 5:
saveToFile();
break;
case 0:
printf("退出系统!\n");
break;
default:
printf("无效的选择!\n");
break;
}
} while (choice != 0);
return 0;
}
```
以上是一个简单的学生管理系统的C语言实现,支持增加、删除、修改、查询和保存到文件操作。程序通过循环菜单供用户选择操作,并根据用户的选择调用相应的函数来实现相应的功能。学生信息存储在`struct Student`结构体数组中,文件操作使用C语言标准库函数来完成。
### 回答3:
学生管理系统是一个可以管理学生信息的程序。下面是用C语言实现的一个简单的学生管理系统,支持增加、删除、修改和查询学生信息,并能将学生信息存入文件中。
首先,我们需要定义一个学生的结构体,包含学生的姓名、年龄和成绩等信息。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
typedef struct {
char name[20];
int age;
float score;
} Student;
```
接下来,我们定义一个学生的数组作为学生信息的容器。
```c
#define MAX_STUDENTS 100
Student students[MAX_STUDENTS];
int count = 0; // 当前学生数量
```
然后,我们可以实现增加学生信息的功能。通过用户输入学生的姓名、年龄和成绩,在数组中新增一个学生。
```c
void addStudent() {
if (count >= MAX_STUDENTS) {
printf("学生数量已达上限!\n");
return;
}
printf("请输入学生姓名:");
scanf("%s", students[count].name);
printf("请输入学生年龄:");
scanf("%d", &students[count].age);
printf("请输入学生成绩:");
scanf("%f", &students[count].score);
count++;
printf("添加学生成功!\n");
}
```
接着,我们可以实现删除学生信息的功能。通过用户输入要删除的学生的姓名,在数组中找到并删除该学生。
```c
void deleteStudent() {
char name[20];
printf("请输入要删除的学生姓名:");
scanf("%s", name);
int i;
for (i = 0; i < count; i++) {
if (strcmp(students[i].name, name) == 0) {
break;
}
}
if (i == count) {
printf("未找到该学生!\n");
return;
}
for (int j = i; j < count - 1; j++) {
students[j] = students[j + 1];
}
count--;
printf("删除学生成功!\n");
}
```
然后,我们可以实现修改学生信息的功能。通过用户输入要修改的学生的姓名,在数组中找到该学生,并更新其信息。
```c
void updateStudent() {
char name[20];
printf("请输入要修改的学生姓名:");
scanf("%s", name);
int i;
for (i = 0; i < count; i++) {
if (strcmp(students[i].name, name) == 0) {
break;
}
}
if (i == count) {
printf("未找到该学生!\n");
return;
}
printf("请输入学生年龄:");
scanf("%d", &students[i].age);
printf("请输入学生成绩:");
scanf("%f", &students[i].score);
printf("修改学生信息成功!\n");
}
```
最后,我们可以实现查询学生信息的功能。通过用户输入要查询的学生的姓名,在数组中找到并显示该学生的信息。
```c
void searchStudent() {
char name[20];
printf("请输入要查询的学生姓名:");
scanf("%s", name);
int i;
for (i = 0; i < count; i++) {
if (strcmp(students[i].name, name) == 0) {
break;
}
}
if (i == count) {
printf("未找到该学生!\n");
return;
}
printf("学生姓名:%s\n", students[i].name);
printf("学生年龄:%d\n", students[i].age);
printf("学生成绩:%f\n", students[i].score);
}
```
当用户选择退出程序时,我们可以将学生信息存入文件中。
```c
void saveToFile() {
FILE *fp;
fp = fopen("students.txt", "w");
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
for (int i = 0; i < count; i++) {
fprintf(fp, "%s %d %f\n", students[i].name, students[i].age, students[i].score);
}
fclose(fp);
printf("学生信息已成功保存到文件!\n");
}
```
综上所述,我们实现了一个简单的学生管理系统,通过C语言实现了增加、删除、修改和查询学生信息,并能将学生信息存入文件中。完整代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
typedef struct {
char name[20];
int age;
float score;
} Student;
#define MAX_STUDENTS 100
Student students[MAX_STUDENTS];
int count = 0;
void addStudent() {
if (count >= MAX_STUDENTS) {
printf("学生数量已达上限!\n");
return;
}
printf("请输入学生姓名:");
scanf("%s", students[count].name);
printf("请输入学生年龄:");
scanf("%d", &students[count].age);
printf("请输入学生成绩:");
scanf("%f", &students[count].score);
count++;
printf("添加学生成功!\n");
}
void deleteStudent() {
char name[20];
printf("请输入要删除的学生姓名:");
scanf("%s", name);
int i;
for (i = 0; i < count; i++) {
if (strcmp(students[i].name, name) == 0) {
break;
}
}
if (i == count) {
printf("未找到该学生!\n");
return;
}
for (int j = i; j < count - 1; j++) {
students[j] = students[j + 1];
}
count--;
printf("删除学生成功!\n");
}
void updateStudent() {
char name[20];
printf("请输入要修改的学生姓名:");
scanf("%s", name);
int i;
for (i = 0; i < count; i++) {
if (strcmp(students[i].name, name) == 0) {
break;
}
}
if (i == count) {
printf("未找到该学生!\n");
return;
}
printf("请输入学生年龄:");
scanf("%d", &students[i].age);
printf("请输入学生成绩:");
scanf("%f", &students[i].score);
printf("修改学生信息成功!\n");
}
void searchStudent() {
char name[20];
printf("请输入要查询的学生姓名:");
scanf("%s", name);
int i;
for (i = 0; i < count; i++) {
if (strcmp(students[i].name, name) == 0) {
break;
}
}
if (i == count) {
printf("未找到该学生!\n");
return;
}
printf("学生姓名:%s\n", students[i].name);
printf("学生年龄:%d\n", students[i].age);
printf("学生成绩:%f\n", students[i].score);
}
void saveToFile() {
FILE *fp;
fp = fopen("students.txt", "w");
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
for (int i = 0; i < count; i++) {
fprintf(fp, "%s %d %f\n", students[i].name, students[i].age, students[i].score);
}
fclose(fp);
printf("学生信息已成功保存到文件!\n");
}
int main() {
int choice;
while (1) {
printf("\n学生管理系统\n");
printf("1. 增加学生信息\n");
printf("2. 删除学生信息\n");
printf("3. 修改学生信息\n");
printf("4. 查询学生信息\n");
printf("0. 退出\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
updateStudent();
break;
case 4:
searchStudent();
break;
case 0:
saveToFile();
exit(0);
break;
default:
printf("无效操作,请重新输入!\n");
}
}
}
```
阅读全文