学生成绩管理系统c语言300行代码
时间: 2024-06-13 20:01:34 浏览: 14
学生成绩管理系统是一个比较经典的C语言练手项目,一般来说可以分为如下几个模块:学生信息录入、成绩信息录入、查询、修改、删除等功能。下面是一个大概的思路,希望能对您有所帮助。
1. 首先需要定义学生结构体,包括学号、姓名、性别、年龄等信息。
2. 定义成绩结构体,包括学生信息和各科成绩。
3. 实现学生信息录入功能,包括从键盘输入各项信息,并将信息保存到文件中。
4. 实现成绩信息录入功能,包括从键盘输入各项信息,并将信息保存到文件中。
5. 实现查询功能,包括按学号、姓名等信息查询学生及其成绩。
6. 实现修改功能,包括修改学生信息及其成绩。
7. 实现删除功能,包括删除学生信息及其成绩。
8. 实现统计功能,包括对所有学生的总分、平均分进行统计分析。
以下是一份比较简单的代码示例,仅供参考:
```
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
typedef struct student {
char no[10];
char name[20];
char sex;
int age;
} Student;
typedef struct score {
char no[10];
char name[20];
int math;
int english;
int computer;
} Score;
void input_student(Student *s);
void output_student(Student s);
void input_score(Score *s);
void output_score(Score s);
void add_student();
void add_score();
void search_student();
void search_score();
void modify_student();
void modify_score();
void delete_student();
void delete_score();
void show_all();
void show_menu();
int main() {
show_menu();
return 0;
}
void input_student(Student *s) {
printf("请输入学号:");
scanf("%s", s->no);
printf("请输入姓名:");
scanf("%s", s->name);
printf("请输入性别:");
scanf("%s", s->sex);
printf("请输入年龄:");
scanf("%d", &s->age);
}
void output_student(Student s) {
printf("学号:%s\t姓名:%s\t性别:%s\t年龄:%d\n", s.no, s.name, s.sex, s.age);
}
void input_score(Score *s) {
printf("请输入学号:");
scanf("%s", s->no);
printf("请输入姓名:");
scanf("%s", s->name);
printf("请输入数学成绩:");
scanf("%d", &s->math);
printf("请输入英语成绩:");
scanf("%d", &s->english);
printf("请输入计算机成绩:");
scanf("%d", &s->computer);
}
void output_score(Score s) {
printf("学号:%s\t姓名:%s\t数学:%d\t英语:%d\t计算机:%d\n", s.no, s.name, s.math, s.english, s.computer);
}
void add_student() {
Student s;
input_student(&s);
FILE *fp = fopen("student.dat", "ab+");
if (fp == NULL) {
printf("文件打开失败!");
exit(0);
}
fwrite(&s, sizeof(s), 1, fp);
fclose(fp);
}
void add_score() {
Score s;
input_score(&s);
FILE *fp = fopen("score.dat", "ab+");
if (fp == NULL) {
printf("文件打开失败!");
exit(0);
}
fwrite(&s, sizeof(s), 1, fp);
fclose(fp);
}
void search_student() {
char no[10];
printf("请输入要查找的学生的学号:");
scanf("%s", no);
FILE *fp = fopen("student.dat", "rb");
if (fp == NULL) {
printf("文件打开失败!");
exit(0);
}
Student s;
int flag = 0;
while (!feof(fp)) {
fread(&s, sizeof(s), 1, fp);
if (strcmp(s.no, no) == 0) {
output_student(s);
flag = 1;
break;
}
}
fclose(fp);
if (flag == 0) {
printf("没有找到该学生!");
}
}
void search_score() {
char no[10];
printf("请输入要查找的学生的学号:");
scanf("%s", no);
FILE *fp = fopen("score.dat", "rb");
if (fp == NULL) {
printf("文件打开失败!");
exit(0);
}
Score s;
int flag = 0;
while (!feof(fp)) {
fread(&s, sizeof(s), 1, fp);
if (strcmp(s.no, no) == 0) {
output_score(s);
flag = 1;
break;
}
}
fclose(fp);
if (flag == 0) {
printf("没有找到该学生成绩!");
}
}
void modify_student() {
char no[10];
printf("请输入要修改的学生的学号:");
scanf("%s", no);
FILE *fp = fopen("student.dat", "rb+");
if (fp == NULL) {
printf("文件打开失败!");
exit(0);
}
Student s;
int flag = 0;
while (!feof(fp)) {
fread(&s, sizeof(s), 1, fp);
if (strcmp(s.no, no) == 0) {
output_student(s);
input_student(&s);
fseek(fp, -sizeof(s), SEEK_CUR);
fwrite(&s, sizeof(s), 1, fp);
flag = 1;
break;
}
}
fclose(fp);
if (flag == 0) {
printf("没有找到该学生!");
}
}
void modify_score() {
char no[10];
printf("请输入要修改的学生的学号:");
scanf("%s", no);
FILE *fp = fopen("score.dat", "rb+");
if (fp == NULL) {
printf("文件打开失败!");
exit(0);
}
Score s;
int flag = 0;
while (!feof(fp)) {
fread(&s, sizeof(s), 1, fp);
if (strcmp(s.no, no) == 0) {
output_score(s);
input_score(&s);
fseek(fp, -sizeof(s), SEEK_CUR);
fwrite(&s, sizeof(s), 1, fp);
flag = 1;
break;
}
}
fclose(fp);
if (flag == 0) {
printf("没有找到该学生成绩!");
}
}
void delete_student() {
char no[10];
printf("请输入要删除的学生的学号:");
scanf("%s", no);
FILE *fp = fopen("student.dat", "rb+");
if (fp == NULL) {
printf("文件打开失败!");
exit(0);
}
Student s;
int flag = 0;
while (!feof(fp)) {
fread(&s, sizeof(s), 1, fp);
if (strcmp(s.no, no) == 0) {
output_student(s);
fseek(fp, -sizeof(s), SEEK_CUR); //将指针移到待删除记录的起始位置
fwrite("", sizeof(s), 1, fp); //用空白记录覆盖待删除记录
flag = 1;
break;
}
}
fclose(fp);
if (flag == 0) {
printf("没有找到该学生!");
}
}
void delete_score() {
char no[10];
printf("请输入要删除的学生成绩的学号:");
scanf("%s", no);
FILE *fp = fopen("score.dat", "rb+");
if (fp == NULL) {
printf("文件打开失败!");
exit(0);
}
Score s;
int flag = 0;
while (!feof(fp)) {
fread(&s, sizeof(s), 1, fp);
if (strcmp(s.no, no) == 0) {
output_score(s);
fseek(fp, -sizeof(s), SEEK_CUR); //将指针移到待删除记录的起始位置
fwrite("", sizeof(s), 1, fp); //用空白记录覆盖待删除记录
flag = 1;
break;
}
}
fclose(fp);
if (flag == 0) {
printf("没有找到该学生成绩!");
}
}
void show_all() {
FILE *fp = fopen("score.dat", "rb");
if (fp == NULL) {
printf("文件打开失败!");
exit(0);
}
Score s;
while (!feof(fp)) { //逐行读取
fread(&s, sizeof(s), 1, fp); //从文件中读取一行数据
if (strlen(s.no) > 0) { //只有当这行数据不为空时才输出
output_score(s); //输出这行数据
}
}
fclose(fp); //关闭文件
}
void show_menu() { //菜单函数
int choice;
do {
printf("\n\n\n\n");
printf("\t\t\t******************************\n");
printf("\t\t\t* 欢迎使用学生成绩管理系统!*\n");
printf("\t\t\t* *\n");
printf("\t\t\t* 1. 添加学生 *\n");
printf("\t\t\t* 2. 添加成绩 *\n");
printf("\t\t\t* 3. 查询学生信息 *\n");
printf("\t\t\t* 4. 查询成绩信息 *\n");
printf("\t\t\t* 5. 修改学生信息 *\n");
printf("\t\t\t* 6. 修改成绩信息 *\n");
printf("\t\t\t* 7. 删除学生信息 *\n");
printf("\t\t\t* 8. 删除成绩信息 *\n");
printf("\t\t\t* 9. 显示所有成绩 *\n");
printf("\t\t\t* 0. 退出系统 *\n");
printf("\t\t\t******************************\n");
printf("\n\n\n\n\n\n\n\n\n\n");
printf("\t\t请输入你的选择:");
scanf("%d",&choice);
switch(choice)
{
case 1:
add_student();
break;
case 2:
add_score();
break;
case 3:
search_student();
break;
case 4:
search_score();
break;
case 5:
modify_student();
break;
case 6:
modify_score();
break;
case 7:
delete_student();
break;
case 8:
delete_score();
break;
case 9:
show_all();
break;
case 0:
exit(0);
default:
break;
}
} while(choice);
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)