C语言实现一个简易的学生管理系统,写的有点简陋凑合凑合
功能全部封装成函数了,可以看看注释还是写的比较详细的。
当然你也可以自己拿去运行一下,看看效果怎么样
#include<stdio.h> #include<string.h> #include<stdbool.h> #include <malloc.h> typedef struct test //成绩 { float math; float English; float c_test; }TEST; typedef struct student //学生信息 { int id; //学号 char password[20]; //密码 char name[20]; //姓名 char sex; //性别 TEST score; //成绩 }stu_s,* stu_p; int max_size = 5; int administrator_account = 111111; //管理员账号 char administrator_password[20] = "admin123"; //管理员密码 bool is_true(stu_p p,const int *count,int inputid);//判断学号是否存在 void capacity(stu_p p ,const int *count); //扩容 void stu_insert(stu_p p,int *count); //添加学生信息; void sut_score(stu_p p,int *count,int stu_id); //成绩信息,以及改密码操作 void sign(stu_p p,int *count) ;//学生登录系统 void admin(stu_p p,int *count) ; //管理员操作 void admin_input(stu_p p,int *count) ; //管理员登录 void stu_print(stu_p p,const int *count) ; //输出成绩信息 void stu_delete(stu_p p ,int *count) ; //删除学生 bool is_true(stu_p p,const int *count,int inputid)//判断学号是否存在 { for(int i=0;i<*count;i++) { if(p[i].id == inputid) return false; } return true; } void capacity(stu_p p ,const int *count) //扩容 { stu_s * new_space = (stu_s *)malloc(sizeof(stu_s) * (*count) * 2); for(int i=0;i<*count;i++) { new_space[i] = p[i]; } new_space = p; max_size *= 2; } void stu_insert(stu_p p,int *count) //添加学生信息; { int id; if(*count == max_size) { capacity(p,count); } printf("输入学生的学号:\n"); scanf("%d",&id); //如果学号存在,则退出程序;否则录入学生信息 if(is_true(p,count,id))//没有该学号 { p[*count].id = id; printf("请输入学生初始密码\n"); scanf("%s", p[*count].password); getchar(); printf("请输入学生姓名:\n"); scanf("%s", p[*count].name); getchar(); printf("请输入学生性别:\n"); scanf("%c", &p[*count].sex); printf("请输入学生数学成绩:\n"); scanf("%f", &p[*count].score.math); printf("请输入学生英语成绩:\n"); scanf("%f", &p[*count].score.English); printf("请输入学生c语言成绩\n"); scanf("%f", &p[*count].score.c_test); } else{ printf("已有该学号!请重新"); stu_insert(p,count); } } void sign(stu_p p,int *count) //学生登录系统 { int id; char pass[20]; int n = 3; while(n) { printf("请输入学生学号:\n"); scanf("%d",&id); printf("请输入密码:\n"); scanf("%s",pass); printf("%d",*count); int i; for (i = 0; i < *count; i++) { if (p[i].id == id && strcmp(p[i].password,pass) == 0) { printf("欢迎登录学生系统!\n"); break; } } if(i == *count) { n--; printf("输入账号或密码有误,你还有%d次机会\n", n); }else { sut_score(p, count, id); break; } } } void sut_score(stu_p p,int *count,int stu_id) //成绩信息,以及改密码操作 { int n; //输入 char password1[20]; //新密码 char password2[20]; //确认新密码 printf("输入0退出系统,输入1查看成绩,输入2更改密码\n"); scanf("%d",&n); int i = 0; //写在外面,后续修改密码直接修改数据库里的密码; if(n == 1) { for( ; i < *count; i++){ if(p[i].id == stu_id) { printf("==================\n"); printf("|数学成绩:%.1f|\n|英语成绩:%.1f|\n|c语言成绩:%.1f|\n",p[i].score.math,p[i].score.English,p[i].score.c_test); printf("==================\n"); sut_score(p,count,stu_id); } } } if(n == 2) { printf("请输入新的密码:\n"); scanf("%s",password1); printf("请再次确认密码:\n"); scanf("%s",password2); if(strcmp(password1,password2) == 0){ for(int j=0;j<sizeof(password1);j++){ //修改密码 p[i].password[j] = password1[j]; } printf("修改密码成功!\n"); sut_score(p,count,stu_id); } } } void admin_input(stu_p p,int *count) //管理员登录 { int input; //输入账号 char password[20]; //密码 int n = 3; while(n) { printf("请输入管理员账号:\n"); scanf("%d",&input); printf("请输入管理员密码:\n"); scanf("%s",password); if(input == administrator_account && strcmp(password,administrator_password) == 0) { printf("欢迎登录管理员系统!\n"); admin(p,count); break; } else { n--; printf("账号或密码错误,您还有%d次机会重新输入\n", n); } } } void admin(stu_p p,int *count) //管理员操作 { int input ; //选择 int input_id; //学号 int m; //另一个选择 char mod_password[20]; //修改密码 int mod_id; //修改学号 float achieve;//成绩 stu_print(p,count); printf("0退出系统、1修改学生基本信息、2修改成绩信息\n"); scanf("%d",&input); if(input == 0) { return ; } if(input == 1) { printf("请输入要修改学生的学号\n"); scanf("%d",&input_id); for(int i=0;i<*count;i++){ if(input_id == p[i].id){ printf("0退出1更改密码2更改学号\n"); scanf("%d",&m); if(m == 1){ printf("你要将密码修改成为:\n"); scanf("%s",mod_password); strcpy(p[i].password,mod_password); } else if(m == 2){ printf("输入你要将此学生的学号修改成:\n"); scanf("%d",&mod_id); //修改成的学号 int j; for(j=0;j<*count;j++){ if(mod_id == p[j].id) { printf("修改失败,此学号已存在!\n"); break; } } if(j == *count) p[i].id = mod_id; } else if(m == 0){ return ; } } } } if(input == 2) { printf("请输入要修改成绩的学生学号\n"); scanf("%d",&input_id); for(int i=0;i<*count;i++){ if(p[i].id == input_id) { printf("1更改数学成绩2更改英语成绩3更改C语言成绩\n"); scanf("%d",&m); printf("输入此项正确的成绩\n"); scanf("%f",&achieve); if(m == 1){ p[i].score.math = achieve; } if(m == 2){ p[i].score.English = achieve; } if(m == 3){ p[i].score.c_test = achieve; } } } } admin(p,count); } void stu_print(stu_p p,const int *count) //打印成绩信息 { printf("====================================================\n"); printf("|学号\t密码\t姓名\t性别\tmath\tEnglish\tC_test|\n"); for(int i=0;i<*count;i++){ printf("|%d\t%s\t%s\t%c\t%.1f\t%.1f\t%.1f |\n",p[i].id,p[i].password,p[i].name,p[i].sex,p[i].score.math,p[i].score.English,p[i].score.c_test); } printf("====================================================\n"); } void stu_find(stu_p p,int *count) //查找某个学生 { int input; //选择用什么查找; int stu_id; //学号查找 char stu_name[20]; //姓名查找 printf("1、通过学号查找 2、通过姓名查找\n"); scanf("%d",&input); if(input == 1) { printf("请输入该学生的学号\n"); scanf("%d",&stu_id); int i; for(i=0;i<*count;i++) { if(p[i].id == stu_id) { printf("有此学生!\n"); printf("==================================\n"); printf("学号\t密码\t姓名\t性别\tmath\tEnglish\tC_test\n"); printf("%d\t%s\t%s\t%c\t%.1f\t%.1f\t%.1f\n",p[i].id,p[i].password,p[i].name,p[i].sex,p[i].score.math,p[i].score.English,p[i].score.c_test); printf("==================================\n"); break; } } if(i == *count) { printf("无此学号学生!\n"); } } if(input == 2) { printf("请输入该学生的姓名\n"); scanf("%s",stu_name); int i; for(i=0;i<*count;i++) { if(strcmp(p[i].name,stu_name) == 0) { printf("有此学生!\n"); printf("==================================\n"); printf("学号\t密码\t姓名\t性别\tmath\tEnglish\tC_test\n"); printf("%d\t%s\t%s\t%c\t%.1f\t%.1f\t%.1f\n",p[i].id,p[i].password,p[i].name,p[i].sex,p[i].score.math,p[i].score.English,p[i].score.c_test); printf("==================================\n"); break; } } if(i == *count) { printf("无此学生!\n"); } } int input_next; printf("是否继续查找0、退出 1、继续查找\n"); scanf("%d",&input_next); if(input_next == 1) { stu_find(p,count); } if(input_next == 0) return ; } void Initial_value(stu_p p) //初始值 { p[0].id = 101; strcpy(p[0].password , "123456"); strcpy(p[0].name , "张三"); p[0].sex = 'm'; p[0].score.math = 88; p[0].score.English = 78; p[0].score.c_test =87; p[1].id = 102; strcpy(p[1].password , "123456"); strcpy(p[1].name , "李四"); p[1].sex = 'f'; p[1].score.math = 98; p[1].score.English = 68; p[1].score.c_test =89; } void stu_delete(stu_p p ,int *count) //删除学生 { int input_id; //操作 printf("请输入要删除学生的学号:\n"); scanf("%d",&input_id); if(is_true(p,count,input_id) == false) { for (int i = 0; i < *count; i++) { if (p[i].id == input_id) { for (int j = i; j < *count - 1; j++) { p[j] = p[j + 1]; } } } } else printf("没有该学号学生!\n"); } void display(stu_p p,int *count) { stu_print(p,count); int input1 ; //选择登录的系统 printf("0、退出系统 1、登录学生系统 2、登录管理员系统\n 3、查找某个学生是否存在 4、添加学生信息 5、删除学生\n"); scanf("%d",&input1); if(input1 == 1) { sign(p,count); } if(input1 == 2) { admin_input(p,count); } if(input1 == 3) { stu_find(p,count); } if(input1 == 4) { stu_insert(p,count); (*count)++; } if(input1 == 5) { stu_delete(p,count); (*count)--; } if(input1 == 0) return; display(p,count); } int main() { stu_s arr[2]; //结构体数组 stu_p p = arr; int count = 2; //学生数量 Initial_value(p); //初始化一些数据 display(p,&count); return 0; }