Student Management System
- foreword
- Topic requirements
- function realization
-
- Define the structure
- main function
- function
-
- Student Information Entry
- delete student information
- Modify a student's grade
- Query student grades
- Find information about all students
- Query all student results
- sort by grade
- Sort by student number
- Query student information based on student number
- Statistical grade point average
- read student information
- Save file information
- full code
- Project Effect Demonstration
foreword
C language, the student management system is a relatively basic project, involving structure, function, array, pointer, file reading and writing and other related knowledge, we can review and summarize the knowledge we have learned by completing a student management system, And we can also learn some extracurricular knowledge to enrich ourselves.
Topic requirements
The system needs to implement the following functions:
log in to the system (judging whether you can log in by entering the user name and password)
enter student information
delete student information (delete student information by name)
modify student grades
query student grades
query all student information
query all student grades
students Sorting grades (bubble sorting)
Querying a single student
Importing student information (importing student information through a file)
Saving student information (saving student information to a file) Viewing class average scores
according to student number sorting Exiting the system (implemented by code, not Ctrl+C ) The system interface is as follows:
function realization
Define the structure
typedef struct Student
{
char num[20];
char name[20];
char sex;
int age;
int score;
} Student_t;
typedef struct class
{
Student_t stu_arr[50]; // 这个班级最多有50个学生
unsigned int index; // 当前班级学生的个数
} class_t; // 班级的类
main function
The login of the system is realized by judging the input account password, and different functions are controlled and realized through the switch statement. Jump and exit the system through while loop and goto statement.
int main(int argc, char const *argv[])
{
int id = 0;
int code = 0;
int chose = 0;
class_t *class = (class_t *)malloc(sizeof(class_t));
if (class == NULL)
{
printf("malloc memory failed!\n");
return -1;
}
class->index = 0;
while (1)
{
printf("*********学生管理系统*********\n");
printf("请输入登录账号->\n");
scanf("%d", &id);
printf("请输入登录密码->\n");
scanf("%d", &code);
if (id == 1001 && code == 1001)
{
printf("|******登录成功******|\n");
while (1)
{
printf("请选择所需要的功能(输入0进入功能菜单):\n");
scanf("%d", &chose);
switch (chose)
{
case 0:
show();
break;
case 1:
student_interp(class);
break;
case 2:
student_delete(class);
break;
case 3:
student_changed(class);
break;
case 4:
student_inquire(class);
break;
case 5:
student_print(class);
break;
case 6:
student_score(class);
break;
case 7:
student_sort(class);
student_score(class);
break;
case 8:
student_one(class);
break;
case 9:
readStu(class);
student_print(class);
break;
case 10:
saveStu(class);
break;
case 11:
student_sort_num(class);
student_print(class);
break;
case 12:
printf("班级平均分为:%.2f\n",student_score_avg(class));
break;
case 13:
goto END;
default:
break;
}
}
}
else
{
printf("账号或密码错误\n");
}
}
END :
return 0;
}
function
Student Information Entry
Enter student information, if the entry of the same student number appears, the entry will fail; the relevant codes are as follows:
int student_interp(class_t *class)
{
int i;
Student_t stu;
printf("请输入学生的 学号、姓名、性别、年龄、成绩:\n");
BEGIN:
scanf("%s %s %c %d %d", stu.num, stu.name, &stu.sex, &stu.age, &stu.score);
if(stu.sex!='M'&&stu.sex!='W')
{
printf("输入的性别有误,请重新输入:\n");
goto BEGIN;
}
if(stu.score>100)
{
printf("输入成绩有误,请重新输入:\n");
goto BEGIN;
}
for (i = 0; i < class->index; i++)
{
if (strcmp(class->stu_arr[i].num, stu.num) == 0)
{
printf("该学生已经存在,请重新输入:\n");
goto BEGIN;
}
}
class->stu_arr[class->index] = stu;
class->index++;
printf("插入成功!\n");
return 0;
}
delete student information
Delete the corresponding student by entering the name of the student to be deleted, first determine whether there is student information, and then delete. code show as below:
int student_delete(class_t *class)
{
int i = 0;
int j = 0;
char name[20];
if (class->index == 0)
{
printf("还没有学生,请输入\n");
return -1;
}
printf("请输入要删除学生的姓名:");
scanf("%s", name);
for (i = 0; i < class->index; i++)
{
if (strcmp(name, class->stu_arr[i].name) == 0)
{
// print_info(&class->stu_arr[i]);
for (j = i; j < class->index - 1; j++)
{
class->stu_arr[j] = class->stu_arr[j + 1];
}
class->index--;
printf("删除成功!!!\n");
break;
}
if (i == class->index)
{
printf("班级没有这个学员!\n");
return -1;
}
}
return 0;
}
Modify a student's grade
Modify the student's grade by entering the student's name, the code is as follows:
int student_changed(class_t *class)
{
char name[20];
int score = 0;
int i = 0;
if(class->index == 0){
printf("还没有学生,请输入\n");
return -1;
}
printf("请输入要修改学生的姓名:");
scanf("%s",name);
for(i = 0; i < class->index; i++){
if(strcmp(name,class->stu_arr[i].name) == 0){
printf("请输入要修改的成绩:");
scanf("%d",&score);
class->stu_arr[i].score = score;
printf("修改成功\n");
break;
}
}
if(i == class->index){
printf("没有该学员!\n");
}
return 0;
}
Query student grades
Query the student's grades by name; the code is as follows:
int student_inquire(class_t *class)
{
char name[20];
int i;
if(class->index==0)
{
printf("还没有学生,请输入:\n");
return -1;
}
printf("请输入要查询的学生:");
scanf("%s",name);
for(i = 0; i < class->index; i++){
if(strcmp(name,class->stu_arr[i].name) == 0){
printf("姓名:%-10s 成绩:%-5d\n",name,class->stu_arr[i].score);
break;
}
}
if(i==class->index){
printf("没有该学员!");
}
return 0;
}
Find information about all students
Traverse the entire array and traverse the student information; the code is as follows:
int student_print(class_t *class)
{
int i;
if(class->index == 0){
printf("还没有学生,请输入\n");
return -1;
}
for (i = 0; i < class->index; i++)
{
printf("所有学生信息:学号:%-10s 姓名:%-10s 性别:%-5c 年龄:%-5d 成绩:%-5d\n", class->stu_arr[i].num, class->stu_arr[i].name, class->stu_arr[i].sex, class->stu_arr[i].age, class->stu_arr[i].score);
}
return 0;
}
Query all student results
code show as below:
int student_score(class_t *class)
{
if(class->index == 0){
printf("还没有学生,请输入\n");
return -1;
}
int i;
for (i = 0; i < class->index; i++)
{
printf("姓名:%-10s 成绩:%-5d\n", class->stu_arr[i].name, class->stu_arr[i].score);
}
return 0;
}
sort by grade
Use bubble sorting to sort the grades of the students and output them from high to low. Here I made a low-level mistake, that is, I defined an int variable temp to exchange, and finally found that only the grades were exchanged, and other information about the students There is no exchange, and finally found that a student variable temp should be defined; the code is as follows:
int student_sort(class_t *class)
{
int i,j;
Student_t temp;
if(class->index==0)
{
printf("还没有学生,请输入:\n");
return -1;
}
for(i=0;i<class->index-1;i++)
{
for(j=0;j<class->index-1-i;j++)
{
if(class->stu_arr[j].score<class->stu_arr[j+1].score)
{
temp=class->stu_arr[j];
class->stu_arr[j]=class->stu_arr[j+1];
class->stu_arr[j+1]=temp;
}
}
}
printf("成绩由高到低排序为:\n");
return 0;
}
Sort by student number
Same as above, sort by student number from low to high, note that the student number here is a string, use the strcmp function to compare; the code is as follows:
int student_sort_num(class_t *class)
{
int i,j;
Student_t temp;
if(class->index==0)
{
printf("还没有学生,请输入:\n");
return -1;
}
for(i=0;i<class->index-1;i++)
{
for(j=0;j<class->index-1-i;j++)
{
if(strcmp(class->stu_arr[j].num,class->stu_arr[j+1].num)>0)
{
temp=class->stu_arr[j];
class->stu_arr[j]=class->stu_arr[j+1];
class->stu_arr[j+1]=temp;
}
}
}
printf("学号由低到高排序:\n");
return 0;
}
Query student information based on student number
Enter the student number to view the basic information of the student; the code is as follows:
int student_one(class_t *class)
{
char num[20];
int i;
if(class->index==0)
{
printf("还没有学生,请输入:\n");
return -1;
}
printf("请输入要查询的学生学号:\n");
scanf("%s",num);
for(i = 0; i < class->index; i++){
if(strcmp(num,class->stu_arr[i].num) == 0){
printf("学号:%-10s 姓名:%-10s 性别:%-5c 年龄:%-5d 成绩:%-5d\n", class->stu_arr[i].num, class->stu_arr[i].name, class->stu_arr[i].sex, class->stu_arr[i].age, class->stu_arr[i].score);
break;
}
}
if(i==class->index){
printf("没有该学员!\n");
}
return 0;
}
Statistical grade point average
Calculate the grades of all students in the class and calculate the average grade; the code is as follows:
float student_score_avg(class_t *class)
{
int i;
float avg=0,sum=0;
for(i=0;i<class->index;i++)
{
sum=sum+class->stu_arr[i].score;
}
avg=sum/class->index;
return avg;
}
read student information
Realize the import of student information from the file, and use the information of the file as input. code show as below:
void readStu(class_t *class){
int i = 0;
FILE * fp;
fp = fopen("students.txt","a+");
if(fp == NULL){
printf("文件打开失败!\n");
exit(0);
}
fscanf(fp,"%d",&class->index);
for(i = 0;i < class->index;i++){
fscanf(fp,"%s %s %c %d %d",class->stu_arr[i].num,class->stu_arr[i].name,&class->stu_arr[i].sex,&class->stu_arr[i].age,&class->stu_arr[i].score);
}
fclose(fp);
printf("导入学生信息成功!\n");
}
Save file information
Write the student information entered by the system into the file; the code is as follows:
int saveStu(class_t *class){
int i = 0;
if(class->index == 0){
printf("还没有学生,请输入\n");
return -1;
}
FILE * fp;
fp = fopen("students.txt","w");
if(fp == NULL){
printf("文件打开失败!\n");
exit(0);
}
fprintf(fp,"%d\n",class->index);
for(i = 0;i < class->index;i++){
fprintf(fp,"%s %s %c %d %d\n",class->stu_arr[i].num,class->stu_arr[i].name,class->stu_arr[i].sex,class->stu_arr[i].age,class->stu_arr[i].score);
}
fclose(fp);
printf("写入成功!\n");
}
full code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student
{
char num[20];
char name[20];
char sex;
int age;
int score;
} Student_t;
typedef struct class
{
Student_t stu_arr[50]; // 这个班级最多有50个学生
unsigned int index; // 当前班级学生的个数
} class_t; // 班级的类
void show();
int student_one(class_t *class);
int student_interp(class_t *class);
int student_delete(class_t *class);
int student_changed(class_t *class);
int student_inquire(class_t *class);
int student_print(class_t *class);
int student_score(class_t *class);
int student_sort(class_t *class);
int student_sort_num(class_t *class);
float student_score_avg(class_t *class);
void readStu(class_t *class);
int saveStu(class_t *class);
int main(int argc, char const *argv[])
{
int id = 0;
int code = 0;
int chose = 0;
class_t *class = (class_t *)malloc(sizeof(class_t));
if (class == NULL)
{
printf("malloc memory failed!\n");
return -1;
}
class->index = 0;
while (1)
{
printf("*********学生管理系统*********\n");
printf("请输入登录账号->\n");
scanf("%d", &id);
printf("请输入登录密码->\n");
scanf("%d", &code);
if (id == 1001 && code == 1001)
{
printf("|******登录成功******|\n");
while (1)
{
printf("请选择所需要的功能(输入0进入功能菜单):\n");
scanf("%d", &chose);
switch (chose)
{
case 0:
show();
break;
case 1:
student_interp(class);
break;
case 2:
student_delete(class);
break;
case 3:
student_changed(class);
break;
case 4:
student_inquire(class);
break;
case 5:
student_print(class);
break;
case 6:
student_score(class);
break;
case 7:
student_sort(class);
student_score(class);
break;
case 8:
student_one(class);
break;
case 9:
readStu(class);
student_print(class);
break;
case 10:
saveStu(class);
break;
case 11:
student_sort_num(class);
student_print(class);
break;
case 12:
printf("班级平均分为:%.2f\n",student_score_avg(class));
break;
case 13:
goto END;
default:
break;
}
}
}
else
{
printf("账号或密码错误\n");
}
}
END :
return 0;
}
//菜单
void show()
{
printf("=======================学生信息管理系统============================\n");
printf("-----------------------------功能菜单------------------------------\n");
printf("\t\t\t1.录入学生信息\n");
printf("\t\t\t2.删除学生信息\n");
printf("\t\t\t3.修改学生成绩\n");
printf("\t\t\t4.查询学生成绩\n");
printf("\t\t\t5.查询所有学生的信息\n");
printf("\t\t\t6.查询所有学生成绩\n");
printf("\t\t\t7.学生成绩排序\n");
printf("\t\t\t8.查询单个学生\n");
printf("\t\t\t9.导入学生信息\n");
printf("\t\t\t10.保存学生信息\n");
printf("\t\t\t11.根据学号排序\n");
printf("\t\t\t12.查看班级平均分\n");
printf("\t\t\t13.退出系统\n");
printf("------------------------------------------------------------------\n");
}
//插入一个学生 (姓名, 性别, 年龄,成绩)
int student_interp(class_t *class)
{
int i;
Student_t stu;
printf("请输入学生的 学号、姓名、性别、年龄、成绩:\n");
BEGIN:
scanf("%s %s %c %d %d", stu.num, stu.name, &stu.sex, &stu.age, &stu.score);
if(stu.sex!='M'&&stu.sex!='W')
{
printf("输入的性别有误,请重新输入:\n");
goto BEGIN;
}
if(stu.score>100)
{
printf("输入成绩有误,请重新输入:\n");
goto BEGIN;
}
for (i = 0; i < class->index; i++)
{
if (strcmp(class->stu_arr[i].num, stu.num) == 0)
{
printf("该学生已经存在,请重新输入:\n");
goto BEGIN;
}
}
class->stu_arr[class->index] = stu;
class->index++;
printf("插入成功!\n");
return 0;
}
//删除一个学生 (根据姓名删除一个学生)
int student_delete(class_t *class)
{
int i = 0;
int j = 0;
char name[20];
if (class->index == 0)
{
printf("还没有学生,请输入\n");
return -1;
}
printf("请输入要删除学生的姓名:");
scanf("%s", name);
for (i = 0; i < class->index; i++)
{
if (strcmp(name, class->stu_arr[i].name) == 0)
{
// print_info(&class->stu_arr[i]);
for (j = i; j < class->index - 1; j++)
{
class->stu_arr[j] = class->stu_arr[j + 1];
}
class->index--;
printf("删除成功!!!\n");
break;
}
if (i == class->index)
{
printf("班级没有这个学员!\n");
return -1;
}
}
return 0;
}
//修改学生成绩 (根据姓名修改成绩)
int student_changed(class_t *class)
{
char name[20];
int score = 0;
int i = 0;
if(class->index == 0){
printf("还没有学生,请输入\n");
return -1;
}
printf("请输入要修改学生的姓名:");
scanf("%s",name);
for(i = 0; i < class->index; i++){
if(strcmp(name,class->stu_arr[i].name) == 0){
printf("请输入要修改的成绩:");
scanf("%d",&score);
class->stu_arr[i].score = score;
printf("修改成功\n");
break;
}
}
if(i == class->index){
printf("没有该学员!\n");
}
return 0;
}
//查询学生成绩 (根据姓名查询成绩)
int student_inquire(class_t *class)
{
char name[20];
int i;
if(class->index==0)
{
printf("还没有学生,请输入:\n");
return -1;
}
printf("请输入要查询的学生:");
scanf("%s",name);
for(i = 0; i < class->index; i++){
if(strcmp(name,class->stu_arr[i].name) == 0){
printf("姓名:%-10s 成绩:%-5d\n",name,class->stu_arr[i].score);
break;
}
}
if(i==class->index){
printf("没有该学员!");
}
return 0;
}
//查询所有学生的信息
int student_print(class_t *class)
{
int i;
if(class->index == 0){
printf("还没有学生,请输入\n");
return -1;
}
for (i = 0; i < class->index; i++)
{
printf("所有学生信息:学号:%-10s 姓名:%-10s 性别:%-5c 年龄:%-5d 成绩:%-5d\n", class->stu_arr[i].num, class->stu_arr[i].name, class->stu_arr[i].sex, class->stu_arr[i].age, class->stu_arr[i].score);
}
return 0;
}
//查询所有学生成绩
int student_score(class_t *class)
{
if(class->index == 0){
printf("还没有学生,请输入\n");
return -1;
}
int i;
for (i = 0; i < class->index; i++)
{
printf("姓名:%-10s 成绩:%-5d\n", class->stu_arr[i].name, class->stu_arr[i].score);
}
return 0;
}
//根据成绩进行排序(从高到低, 冒泡排序)
int student_sort(class_t *class)
{
int i,j;
Student_t temp;
if(class->index==0)
{
printf("还没有学生,请输入:\n");
return -1;
}
for(i=0;i<class->index-1;i++)
{
for(j=0;j<class->index-1-i;j++)
{
if(class->stu_arr[j].score<class->stu_arr[j+1].score)
{
temp=class->stu_arr[j];
class->stu_arr[j]=class->stu_arr[j+1];
class->stu_arr[j+1]=temp;
}
}
}
printf("成绩由高到低排序为:\n");
return 0;
}
//根据学号查询学生信息
int student_one(class_t *class)
{
char num[20];
int i;
if(class->index==0)
{
printf("还没有学生,请输入:\n");
return -1;
}
printf("请输入要查询的学生学号:\n");
scanf("%s",num);
for(i = 0; i < class->index; i++){
if(strcmp(num,class->stu_arr[i].num) == 0){
printf("学号:%-10s 姓名:%-10s 性别:%-5c 年龄:%-5d 成绩:%-5d\n", class->stu_arr[i].num, class->stu_arr[i].name, class->stu_arr[i].sex, class->stu_arr[i].age, class->stu_arr[i].score);
break;
}
}
if(i==class->index){
printf("没有该学员!\n");
}
return 0;
}
//读取学生信息
void readStu(class_t *class){
int i = 0;
FILE * fp;
fp = fopen("students.txt","a+");
if(fp == NULL){
printf("文件打开失败!\n");
exit(0);
}
fscanf(fp,"%d",&class->index);
for(i = 0;i < class->index;i++){
fscanf(fp,"%s %s %c %d %d",class->stu_arr[i].num,class->stu_arr[i].name,&class->stu_arr[i].sex,&class->stu_arr[i].age,&class->stu_arr[i].score);
}
fclose(fp);
printf("导入学生信息成功!\n");
}
//保存学生信息
int saveStu(class_t *class){
int i = 0;
if(class->index == 0){
printf("还没有学生,请输入\n");
return -1;
}
FILE * fp;
fp = fopen("students.txt","w");
if(fp == NULL){
printf("文件打开失败!\n");
exit(0);
}
fprintf(fp,"%d\n",class->index);
for(i = 0;i < class->index;i++){
fprintf(fp,"%s %s %c %d %d\n",class->stu_arr[i].num,class->stu_arr[i].name,class->stu_arr[i].sex,class->stu_arr[i].age,class->stu_arr[i].score);
}
fclose(fp);
printf("写入成功!\n");
}
//按学号排序
int student_sort_num(class_t *class)
{
int i,j;
Student_t temp;
if(class->index==0)
{
printf("还没有学生,请输入:\n");
return -1;
}
for(i=0;i<class->index-1;i++)
{
for(j=0;j<class->index-1-i;j++)
{
if(strcmp(class->stu_arr[j].num,class->stu_arr[j+1].num)>0)
{
temp=class->stu_arr[j];
class->stu_arr[j]=class->stu_arr[j+1];
class->stu_arr[j+1]=temp;
}
}
}
printf("学号由低到高排序:\n");
return 0;
}
//统计班级平均成绩
float student_score_avg(class_t *class)
{
int i;
float avg=0,sum=0;
for(i=0;i<class->index;i++)
{
sum=sum+class->stu_arr[i].score;
}
avg=sum/class->index;
return avg;
}
Project Effect Demonstration
Here are a few demonstrations of several functions, and others who are interested can try it.
Log in to
the main interface
, enter student information,
modify student grades
, view student grades,
import student information
file content:
After importing successfully:
save student information
First insert a student 1005;
save information:
view file:
the function is demonstrated here. . .