c语言学生成绩管理系统
时间: 2023-09-01 08:11:02 浏览: 63
好的,你想要一个简单的C语言学生成绩管理系统,以下是一个基本框架:
```c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_NUM 100
struct student {
char name[20];
int score;
};
int menu();
void add_student(struct student[], int);
void delete_student(struct student[], int);
void search_student(struct student[], int);
void print_all(struct student[], int);
void save_data(struct student[], int);
int main() {
struct student students[MAX_NUM];
int num = 0;
int choice;
// 从文件中读取之前保存的数据
FILE *fp = fopen("data.txt", "r");
if(fp != NULL) {
while(fscanf(fp, "%s %d", students[num].name, &students[num].score) != EOF) {
num++;
}
fclose(fp);
}
while(1) {
choice = menu();
switch(choice) {
case 1:
add_student(students, num);
num++;
break;
case 2:
delete_student(students, num);
num--;
break;
case 3:
search_student(students, num);
break;
case 4:
print_all(students, num);
break;
case 5:
save_data(students, num);
return 0;
default:
printf("请输入正确的选项!\n");
break;
}
}
return 0;
}
int menu() {
int choice;
printf("学生成绩管理系统\n");
printf("1. 添加学生\n");
printf("2. 删除学生\n");
printf("3. 查找学生\n");
printf("4. 显示所有学生信息\n");
printf("5. 保存并退出\n");
printf("请输入您的选择:");
scanf("%d", &choice);
return choice;
}
void add_student(struct student students[], int num) {
char name[20];
int score;
printf("请输入学生姓名:");
scanf("%s", name);
printf("请输入学生成绩:");
scanf("%d", &score);
strcpy(students[num].name, name);
students[num].score = score;
printf("添加成功!\n");
}
void delete_student(struct student students[], int num) {
char name[20];
int i, j;
printf("请输入要删除的学生姓名:");
scanf("%s", name);
for(i = 0; i < num; i++) {
if(strcmp(students[i].name, name) == 0) {
for(j = i; j < num - 1; j++) {
strcpy(students[j].name, students[j+1].name);
students[j].score = students[j+1].score;
}
printf("删除成功!\n");
return;
}
}
printf("查无此人!\n");
}
void search_student(struct student students[], int num) {
char name[20];
int i;
printf("请输入要查找的学生姓名:");
scanf("%s", name);
for(i = 0; i < num; i++) {
if(strcmp(students[i].name, name) == 0) {
printf("学生姓名:%s\n", students[i].name);
printf("学生成绩:%d\n", students[i].score);
return;
}
}
printf("查无此人!\n");
}
void print_all(struct student students[], int num) {
int i;
printf("学生姓名\t学生成绩\n");
for(i = 0; i < num; i++) {
printf("%s\t\t%d\n", students[i].name, students[i].score);
}
}
void save_data(struct student students[], int num) {
int i;
FILE *fp = fopen("data.txt", "w");
for(i = 0; i < num; i++) {
fprintf(fp, "%s %d\n", students[i].name, students[i].score);
}
fclose(fp);
printf("数据已保存并退出!\n");
}
```
这是一个基本的学生成绩管理系统,包括添加、删除、查找、显示和保存数据等功能。你可以根据自己的需求进行修改和完善。
阅读全文