老师让实现一个能有三个子系统的程序管理系统,决定记录下来过程,同时代码会同步更新在博客中,本篇代码转载请标明出处。
最后更新:2021/9/7 11:30
本篇还有许多功能未写好
前言
先看要求,要有:学生系统,老师系统,管理员系统。这个跟以往的不同,传统的我觉得是从老师视角看,同时老师也有管理员权限,对学生的成绩,信息进行操作。现在类似于把权利细分,我们不妨先思考下,各个部分的职能应该是什么?
学生:
- 注册/登陆账号
- 修改自己的个人信息
- 查看自己的成绩(但是不能查看自己的排名)
老师
- 注册/登陆账号
- 修改自己的个人信息
- 修改学生的信息
- 可以对学生信息进行一些经典操作。
管理员
- 可以登陆账号,账号只能从后台文件中添加。
- 可以把个人账号设置为老师账号 (下面会有解释)
- 可以查看/修改所有账号信息
本篇代码的按照
C
C
C语言的逻辑/操作写,按照
C
C
C++
11
11
11编译。
注:本人能力有限,权利可以更加细分,如:老师可以细分到是哪个班的老师,管理员可以拥有所有权限,而且可以写成可以分配给一些老师特殊的权限,但因篇幅有限,码力不足,同时也为了简介,笔者就不多写了 (懒就直说) 。
正篇
注册/登陆账号的实现
懒得写OVQ,改天吧,阿巴阿巴
源代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
const int NAME_LEN = 50;
const int PASSWORD_LEN = 100;
const int ACCOUNT_LEN = 100;
const int USER_MAXN = 5000;
const int TEACHER_MAXN = 1000;
const int STUDENT_MAXN = 1000;
const int ADMIN_MAXN = 1000;
struct User{
char account[ACCOUNT_LEN];
char password[PASSWORD_LEN];
int type;
int number;
}user[USER_MAXN] , user_now;
struct Student {
char name[NAME_LEN];
int sex_idx;
int chinese;
int math;
int english;
}student[STUDENT_MAXN] , student_now;
struct Teacher{
char name[NAME_LEN];
}teacher[TEACHER_MAXN] , teacher_now;
struct Admin{
}admin[ADMIN_MAXN] , admin_now;
int user_num;
int student_num;
int teacher_num;
int wrong_count;
int check_account(char account[] , char password[]);
void Pretreatment();
void Pretreatment_user();
void Pretreatment_admin();
void Pretreatment_teacher();
void Pretreatment_student();
void main_menu();
void login();
void _register();
void save_user();
void type0_menu();
void type1_menu();
void type2_menu();
void type3_menu();
void student_inf();
void teacher_inf();
void teacher_menu();
void student_menu();
void save_student();
void save_teacher();
void deply_inf_student();
void deply_sc_student();
void change_stu_sc();
void sort_menu();
void deplay_all();
void sort_sum();
void sort_chinese();
void sort_math();
void sort_english();
int main()
{
Pretreatment();
main_menu();
return 0;
}
void change_stu_sc()
{
printf("请输入学生编号:\n");
int pos;
scanf("%d" , &pos);
printf("%s 同学当前的分数是(如果是-1,代表尚未更新):\n" , student[pos].name);
printf("语文:%d\t数学:%d\t英语:%d\n");
printf("请输入修改过过后的三个分数:");
scanf("%d %d %d" , &student[pos].chinese,&student[pos].math , &student[pos].english);
save_student();
Pretreatment();
}
void sort_menu()
{
}
void deplay_all()
{
}
void sort_sum()
{
}
void sort_chinese()
{
}
void sort_math()
{
}
void sort_english()
{
}
void login()
{
system("cls");
char input_account[ACCOUNT_LEN];
char input_password[PASSWORD_LEN];
printf("请输入账号:");
scanf("%s" , input_account);
printf("请输入密码:");
scanf("%s" , input_password);
if (check_account(input_account , input_password))
{
if (user_now.type == 0) type0_menu();
else if(user_now.type == 1) type1_menu();
else if(user_now.type == 2) type2_menu();
else if(user_now.type == 3) type3_menu();
}
else
{
wrong_count ++;
if (wrong_count == 3) exit(0);
printf("账号不存在或密码错误\n");
printf("您还可以尝试: %d 次,持续错误将退出程序.\n",3 - wrong_count);
printf("请重新输入\n");
login();
}
}
void student_menu()
{
system("cls");
printf("欢迎您:%s 同学" , student_now.name);
puts("您可以选择:");
puts("1.查看个人信息");
puts("2.查看成绩");
int operation;
scanf("%d" , &operation);
switch(operation)
{
case 1:
deply_inf_student();
break;
case 2:
deply_sc_student();
break;
default:
printf("请输入合法的操作序号!");
student_menu();
break;
}
}
void deply_inf_student()
{
system("cls");
printf("姓名: %s\n" , student_now.name);
printf("性别 : ");
if (!student_now.sex_idx) puts("男");
else puts("女");
}
void deply_sc_student()
{
system("cls");
if (student_now.chinese == -1) puts("老师尚未更新成绩");
else {
printf("您的语文成绩:%d\n" , student_now.chinese);
printf("您的数学成绩: %d\n" , student_now.math);
printf("您的英语成绩: %d\n" , student_now.english);
}
}
void teacher_menu()
{
system("cls");
printf("欢迎您:%s 老师" , teacher_now.name);
printf("您可以选择:\n");
puts("1.更新学生成绩");
puts("2.将学生成绩排序");
puts("3.展示当前排列的学生成绩");
int operation;
scanf("%d" , &operation);
switch(operation)
{
case 1:
change_stu_sc();
break;
case 2:
sort_menu();
break;
case 3:
deplay_all();
break;
default:
printf("请输入合法的操作序号!");
teacher_menu();
break;
}
}
void student_inf()
{
system("cls");
printf("请输入姓名:");
scanf("%s" , student_now.name);
printf("请输入性别(0/男,1/女):");
scanf("%d" , &student_now.sex_idx);
student_now.chinese = -1;
student_now.math = -1;
student_now.english = -1;
student_num ++;
strcpy(student[student_num].name , student_now.name);
student[student_num].sex_idx = student_now.sex_idx;
student[student_num].chinese = student_now.chinese;
student[student_num].math = student_now.math;
student[student_num].english = student_now.english;
user_now.number = student_num;
user_now.type = 1;
for (int i = 1;i <= user_num;i ++) {
if (!strcmp(user[i].account , user_now.account)) {
user[i].number = user_now.number;
user[i].type = user_now.type;
break;
}
}
save_user();
save_student();
Pretreatment();
printf("您已经更新个人信息,将在 3 秒后返回学生界面\n");
Sleep(3000);
student_menu();
}
void teacher_inf()
{
printf("请输入姓名:");
scanf("%s" , teacher_now.name);
teacher_num ++;
strcpy(teacher[teacher_num].name , teacher_now.name);
user_now.number = teacher_num;
user_now.type = 2;
for (int i = 1;i <= user_num;i ++) {
if (!strcmp(user[i].account , user_now.account)) {
user[i].number = user_now.number;
user[i].type = user_now.type;
break;
}
}
save_user();
save_teacher();
Pretreatment();
printf("您已经更新个人信息,将在 3 秒后返回老师界面\n");
Sleep(3000);
teacher_menu();
}
void type0_menu()
{
printf("检测到您没有更新信息:\n");
printf("您是老师,还是学生?\n");
puts("1.学生");
puts("2.老师");
int operation;
scanf("%d" , &operation);
switch(operation)
{
case 1:
student_inf();
break;
case 2:
teacher_inf();
break;
default:
printf("请输入合法的操作序号!");
main_menu();
break;
}
}
void type1_menu()
{
int pos = user_now.number;
strcpy(student_now.name ,student[pos].name);
student_now.sex_idx = student[pos].sex_idx;
student_now.chinese = student[pos].chinese;
student_now.math = student[pos].math;
student_now.english = student[pos].english;
student_menu();
}
void type2_menu()
{
strcpy(teacher_now.name , teacher[user_now.number].name);
teacher_menu();
}
void type3_menu()
{
}
void main_menu()
{
system("cls");
puts("Welcome to Zsh'student_system'");
printf("1.登录\n");
printf("2.注册\n");
printf("3.退出\n");
int operation;
printf("请输入操作:");
scanf("%d" , &operation);
switch(operation)
{
case 1:
login();
break;
case 2:
_register();
break;
case 3:
exit(0);
default:
printf("请输入合法的操作序号!");
main_menu();
break;
}
}
void Pretreatment_student()
{
FILE *fp_student = fopen("pretreatment_student.txt" , "r");
fscanf(fp_student , "%d" , &student_num);
for (int i = 1;i <= student_num;i ++) {
fscanf(fp_student , "%s%d%d%d%d" , student[i].name , &student[i].sex_idx , &student[i].chinese , &student[i].math , &student[i].english);
}
fclose(fp_student);
}
void save_student()
{
FILE *fp_student = fopen("pretreatment_student.txt" , "w");
fprintf(fp_student , "%d\n" , student_num);
for (int i = 1;i <= student_num;i ++) {
fprintf(fp_student , "%s\n%d\n%d\n%d\n%d\n" , student[i].name , student[i].sex_idx , student[i].chinese , student[i].math , student[i].english);
}
fclose(fp_student);
}
void Pretreatment_user()
{
FILE *fp = fopen("pretreatment.txt" , "r");
fscanf(fp , "%d" , &user_num);
for (int i = 1;i <= user_num;i ++) {
fscanf(fp , "%s%s%d%d" , user[i].account , user[i].password , &user[i].type , &user[i].number);
}
fclose(fp);
}
void Pretreatment_teacher()
{
FILE *fp_teacher = fopen("pretreatment_teacher.txt" , "r");
fscanf(fp_teacher , "%d" , &teacher_num);
for (int i = 1;i <= teacher_num;i ++) {
fscanf(fp_teacher , "%s" , teacher[i].name);
}
fclose(fp_teacher);
}
void save_teacher()
{
FILE *fp_teacher = fopen("pretreatment_teacher.txt" , "w");
fprintf(fp_teacher , "%d\n" , teacher_num);
for (int i = 1;i <= teacher_num;i ++) {
fprintf(fp_teacher , "%s" , teacher[i].name);
}
fclose(fp_teacher);
}
void Pretreatment_admin()
{
}
void Pretreatment()
{
wrong_count = 0;
Pretreatment_user();
Pretreatment_teacher();
Pretreatment_student();
Pretreatment_admin();
}
void save_user()
{
FILE *fp = fopen("pretreatment.txt" , "w");
fprintf(fp , "%d\n" , user_num);
for (int i = 1;i <= user_num;i ++) {
fprintf(fp , "%s\n%s\n%d\n%d\n" , user[i].account , user[i].password , user[i].type , user[i].number);
}
fclose(fp);
}
void main_menu2()
{
system("cls");
puts("Welcome to Zsh'student_system'");
printf("1.登录\n");
printf("2.退出\n");
int operation;
printf("请输入操作:");
scanf("%d" , &operation);
switch(operation)
{
case 1:
login();
break;
case 2:
exit(0);
default:
printf("请输入合法的操作序号!");
main_menu();
break;
}
}
void _register()
{
system("cls");
printf("您好,欢迎来到注册界面.\n");
printf("请输入注册账号:");
scanf("%s" , user_now.account);
for (int i = 1;i <= user_num;i ++) {
if (!strcmp(user[i].account , user_now.account))
{
printf("检测到有重复,将在 3s 返回注册界面,请重新输入,进行注册:");
Sleep(3000);
_register();
}
}
printf("请输入注册密码: ");
scanf("%s" , user_now.password);
user_num ++;
strcpy(user[user_num].account , user_now.account);
strcpy(user[user_num].password , user_now.password);
user[user_num].type = 0;
save_user();
printf("注册成功,将在 3 秒后返回主菜单.\n");
Sleep(3000);
main_menu2();
}
int check_account(char account[] , char password[])
{
printf("%d\n" , user_num);
for (int i = 1;i <= user_num;i ++) {
if (!strcmp(account , user[i].account) && !strcmp(password , user[i].password))
{
strcpy(user_now.account , user[i].account);
strcpy(user_now.password , user[i].password);
user_now.number = user[i].number;
user_now.type = user[i].type;
return 1;
}
}
return 0;
}
|