#include<stdio.h>
#include<stdlib.h>
#include<Windows.h> //支持清屏、光标移动等功能
#include<conio.h> //_getch()等函数
#include<string.h> //strcmp()等函数
#define stuSIZE 50 //支持储存的学生成绩信息数目
#define admSIZE 10 //支持储存的管理员账户数目
#define siteX 41 //学生成绩信息显示位置横坐标
char code[20] = "123"; //管理员注册授权码
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //载入光标信息
//定义坐标
COORD coord_zh = { 17,4 }; //账号输入处坐标
COORD coord_mm = { 17,5 }; //密码输入处坐标
COORD coord_qrmm = { 17,6 }; //确认密码输入处坐标
COORD coord_sqm = { 17,7 }; //授权码输入处坐标
COORD coord_rel = { 0,6 }; //登录提示坐标
COORD coord_rer = { 0,8 }; //注册提示坐标
COORD coord_menu = { 0,14 }; //主菜单输入处坐标
COORD coord_stuOUT1 = { siteX,14 }; //学生成绩信息显示坐标1
COORD coord_stuOUT2 = { siteX,15 }; //学生成绩信息显示坐标2
COORD coord_stuOUT3 = { siteX,16 }; //学生成绩信息显示坐标2
//建立管理员账户结构体
struct administrator
{
char zh[20]; //账号
char mm[20]; //密码
}adm[admSIZE];
//建立学生成绩信息结构体
struct student
{
int num; //学号
char name[20]; //姓名
double cls0; //平均分
double clsA; //科目A
double clsB; //科目B
double clsC; //科目C
double clsD; //科目D
double clsE; //科目E
char level[20]; //成绩等级
} stu[stuSIZE];
//排序
void sort(int a)
{
student t;
switch (a)
{
case 1: //按学号排序
for (int j = stuSIZE - 1; j > 0; j--)
{
for (int i = 0; i < j; i++)
{
if (stu[i].num > stu[i + 1].num)
{
t = stu[i + 1];
stu[i + 1] = stu[i];
stu[i] = t;
}
}
}
break;
case 2: //按成绩排序
for (int j = stuSIZE - 1; j > 0; j--)
{
for (int i = 0; i < j; i++)
{
if (stu[i].cls0 < stu[i + 1].cls0)
{
t = stu[i + 1];
stu[i + 1] = stu[i];
stu[i] = t;
}
}
}
break;
default:
break;
}
}
//读取管理员账户文件
void read_adm()
{
FILE* fp;
if (fopen_s(&fp, "administrators.dat", "rb"))
fopen_s(&fp, "administrators.dat", "wb");
for (int i = 0; i < admSIZE; i++)
fread(&adm[i], sizeof(administrator), 1, fp);
fclose(fp);
}
//读取学生成绩信息文件
void read_stu()
{
FILE* fp;
if (fopen_s(&fp, "students.dat", "rb"))
fopen_s(&fp, "students.dat", "wb");
for (int i = 0; i < stuSIZE; i++)
fread(&stu[i], sizeof(student), 1, fp);
fclose(fp);
sort (1); //第一次读取将默认按学号排序
}
//保存管理员账户文件
void save_adm()
{
FILE* fp;
fopen_s(&fp, "administrators.dat", "rb+");
for (int i = 0; i < admSIZE; i++)
fwrite(&adm[i], sizeof(administrator), 1, fp);
fclose(fp);
}
//保存学生成绩信息文件
void save_stu()
{
FILE* fp;
fopen_s(&fp, "students.dat", "rb+");
for (int i = 0; i < stuSIZE; i++)
fwrite(&stu[i], sizeof(student), 1, fp);
fclose(fp);
}
//绘制首页
void firstUI()
{
printf("|***欢迎使用学生成绩管理系统***|\n");
printf("| |\n");
printf("| (1)登录 |\n");
printf("| (2)注册 |\n");
printf("| (3)退出 |\n");
printf("| |\n");
printf("| 按下对应数字键选择功能 |\n");
printf("| |\n");
printf("|******************|\n");
}
//绘制登录界面图形
void loginUI()
{
printf("|*******欢迎使用学生成绩管理系统*******|\n");
printf("| |\n");
printf("| *登录* |\n");
printf("| |\n");
printf("| 账号: |\n");
printf("| 密码: |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("|**************************|\n");
}
//绘制注册界面图形
void registerUI()
{
printf("|*******欢迎使用学生成绩管理系统*******|\n");
printf("| |\n");
printf("| *注册* |\n");
printf("| (账号处输入数字0可直接退出注册) |\n");
printf("| 账号: |\n");
printf("| 密码: |\n");
printf("| 确认密码: |\n");
printf("| 授权码: |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("|**************************|\n");
}
//绘制主菜单界面图形
void menuUI()
{
printf("|*******欢迎使用学生成绩管理系统*******|\n");
printf("| |\n");
printf("| (1)添加信息 |\n");
printf("| (2)删除信息 |\n");
printf("| (3)修改信息 |\n");
printf("| (4)查找学生 |\n");
printf("| (5)按学号排序 |\n");
printf("| (6)按成绩排序 |\n");
printf("| |\n");
printf("| (0)退出系统 |\n");
printf("| |\n");
printf("| 注意,请勿直接关闭程序,否则数据将不会保存 |\n");
printf("| |\n");
printf("|**************************|\n");
}
//绘制学生成绩信息栏图形
void informationUI()
{
int i, j = 0;
int a, b, c, d, e;
a = b = c = d = e = 0;
for (i = 0; i < stuSIZE; i++) //判断学生成绩信息是否为空
{
if (stu[i].num == 0)
j++;
}
SetConsoleCursorPosition(handle, coord_stuOUT1); //将光标移动至合适位置打印信息
if (j == 50)
printf("|当前学生成绩信息为空\n");
else
{
for (i = 0; i < stuSIZE; i++) //统计各层次学生人数
{
if (stu[i].num)
{
if (stu[i].cls0 > 90)
a++;
else if (stu[i].cls0 > 80)
b++;
else if (stu[i].cls0 > 70)
c++;
else if (stu[i].cls0 > 60)
d++;
else
e++;
}
}
printf("|优:%d人; 良:%d人; 中:%d人; 及格:%d人; 不及格:%d人",a,b,c,d,e);
SetConsoleCursorPosition(handle, coord_stuOUT2);
printf("|");
SetConsoleCursorPosition(handle, coord_stuOUT3);
printf("|学号\t姓名\t平均分\t科目A\t科目B\t科目C\t科目D\t科目E\t等级");
int m = 17;
for (i = 0; i < stuSIZE; i++)
{
if (stu[i].num)
{
COORD coord_outinf = { siteX,m };
SetConsoleCursorPosition(handle, coord_outinf);
printf("|%d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\t%s",
stu[i].num, stu[i].name, stu[i].cls0, stu[i].clsA, stu[i].clsB, stu[i].clsC, stu[i].clsD, stu[i].clsE, stu[i].level);
m++;
}
}
}
}
//隐藏式输入密码
void hide_password(char mm[20])
{
int i = 0;
while ((mm[i] = _getch()) != '\r')
{
if (mm[i] == '\b')
{
printf("\b \b");
i--;
}
else
{
printf("*");
i++;
}
}
mm[i] = '\0';
}
//登陆
void login()
{
int i, j;
char zh[20];
char mm[20];
system("cls");
loginUI();
for (j = 0; j < 3; j++)
{
SetConsoleCursorPosition(handle, coord_zh);
scanf_s("%s", zh, 20);
SetConsoleCursorPosition(handle, coord_mm);
hide_password(mm);
for (i = 0; i < admSIZE; i++)
{
if (strcmp(zh, adm[i].zh) == 0 && strcmp(mm, adm[i].mm) == 0)
{
system("cls");
loginUI();
SetConsoleCursorPosition(handle, coord_rel);
printf("\t 登陆成功!\n\t ");
system("pause");
return;
}
}
if (i == 10)
{
system("cls");
loginUI();
SetConsoleCursorPosition(handle, coord_rel);
printf("\t 账号或密码输入错误,请重新输入\n\t 输入错误3次将退出程序!已错误%d次\n", j + 1);
}
}
exit(0);
}
//注册
void register_s()
{
char zh[20] = "1";
char mm[20] = "1";
char qrmm[20] = "1";
char sqm[20];
int i;
strcpy_s(sqm, code);
while (1)
{
system("cls");
registerUI();
SetConsoleCursorPosition(handle, coord_rer);
if (strcmp(sqm, code) != 0)
printf("\t\t\b授权码不正确!\n");
if (strcmp(mm, qrmm) != 0)
printf("\t\t\b两次输入的密码不一致!\n");
SetConsoleCursorPosition(handle, coord_zh);
scanf_s("%s", zh, 20);
if (strcmp(zh, "0") == 0)
return;
SetConsoleCursorPosition(handle, coord_mm);
scanf_s("%s", mm, 20);
SetConsoleCursorPosition(handle, coord_qrmm);
scanf_s("%s", qrmm, 20);
SetConsoleCursorPosition(handle, coord_sqm);
scanf_s("%s", sqm, 20);
if (strcmp(sqm, code) == 0 && strcmp(mm, qrmm) == 0)
{
for (i = 0; i < admSIZE; i++)
{
if (strcmp(adm[i].zh, "\0") == 0)
break;
}
strcpy_s(adm[i].zh, zh);
strcpy_s(adm[i].mm, mm);
system("cls");
registerUI();
SetConsoleCursorPosition(handle, coord_rer);
printf("\t 注册成功!\n\t 按任意键回到首页");
_getch();
return;
}
}
}
//首页
void first()
{
while (1)
{
system("cls");
firstUI();
switch (_getch())
{
case '1':
read_adm();
login();
return;
case '2':
read_adm();
register_s();
save_adm();
break;
case '3':
exit(0);
default:
break;
}
}
}
//主菜单
int menu()
{
int a;
system("cls");
menuUI();
informationUI();
SetConsoleCursorPosition(handle, coord_menu);
printf("输入对应数字以选择功能:");
scanf_s("%d", &a);
return a;
}
//(1)添加信息
void add()
{
int i;
while (1)
{
for (i = 0; stu[i].num; i++); //遍历每个结构体,直至信息为空的结构体
printf("添加学号(输入0退出):");
scanf_s("%d", &stu[i].num);
if (stu[i].num == 0)
return;
printf("姓名:");
scanf_s("%s", stu[i].name, 20);
printf("科目A(0~100):");
scanf_s("%lf", &stu[i].clsA);
printf("科目B(0~100):");
scanf_s("%lf", &stu[i].clsB);
printf("科目C(0~100):");
scanf_s("%lf", &stu[i].clsC);
printf("科目D(0~100):");
scanf_s("%lf", &stu[i].clsD);
printf("科目E(0~100):");
scanf_s("%lf", &stu[i].clsE);
stu[i].cls0 = stu[i].clsA + stu[i].clsB + stu[i].clsC + stu[i].clsD + stu[i].clsE;
stu[i].cls0 = stu[i].cls0 / 5;
if (stu[i].cls0 > 90)
strcpy_s(stu[i].level, "优");
else if (stu[i].cls0 > 80)
strcpy_s(stu[i].level, "良");
else if (stu[i].cls0 > 70)
strcpy_s(stu[i].level, "中");
else if (stu[i].cls0 > 60)
strcpy_s(stu[i].level, "及格");
else
strcpy_s(stu[i].level, "不及格");
system("cls");
menuUI();
informationUI();
SetConsoleCursorPosition(handle, coord_menu);
printf("提示:添加成功!\n\n");
}
}
//(2)删除信息
void dlt()
{
int S, i = 0;
while (1)
{
if (i == 50)
printf("(输入0回到主菜单)\n该学号不存在!请重新输入:");
else
printf("(输入0回到主菜单)\n请输出要删除的成绩信息的学号:");
scanf_s("%d", &S);
if (S == 0)
return;
for (i = 0; i < stuSIZE; i++)
{
if (stu[i].num == S)
{
stu[i].num = 0;
system("cls");
menuUI();
informationUI();
SetConsoleCursorPosition(handle, coord_menu);
printf("提示:删除成功!\n\n");
break;
}
}
}
}
//(3)修改信息
void modify()
{
int S, i = 0;
while (1)
{
if (i == 50)
printf("(输入0回到主菜单)\n该学号不存在!请重新输入:");
else
printf("(输入0回到主菜单)\n请输出要修改的成绩信息的学号:");
scanf_s("%d", &S);
if (S == 0)
return;
for (i = 0; i < stuSIZE; i++)
{
if (stu[i].num == S)
{
printf("您需要修改这条信息的哪一项?\n(1)学号 (2)姓名\n(a)科目A(b)科目B(c)科目C(d)科目D(e)科目E\n【按下对应按键以选择】\n");
switch (_getch())
{
case '1':
while (1)
{
printf("将学号更改为:");
scanf_s("%d", &S);
if
(S == 0) printf("学号不能为0!请重新输入\n");
else
{
stu[i].num = S;
system("cls");
menuUI();
informationUI();
SetConsoleCursorPosition(handle, coord_menu);
printf("提示:修改成功!\n\n");
break;
}
}
break;
case '2':
printf("将姓名更改为:");
scanf_s("%s", stu[i].name, 20);
system("cls");
menuUI();
informationUI();
SetConsoleCursorPosition(handle, coord_menu);
printf("提示:修改成功!\n\n");
break;
case 'a':
printf("将科目A成绩更改为(0~100):");
scanf_s("%lf", &stu[i].clsA);
stu[i].cls0 = stu[i].clsA + stu[i].clsB + stu[i].clsC + stu[i].clsD + stu[i].clsE;
stu[i].cls0 = stu[i].cls0 / 5;
if (stu[i].cls0 > 90)
strcpy_s(stu[i].level, "优");
else if (stu[i].cls0 > 80)
strcpy_s(stu[i].level, "良");
else if (stu[i].cls0 > 70)
strcpy_s(stu[i].level, "中");
else if (stu[i].cls0 > 60)
strcpy_s(stu[i].level, "及格");
else
strcpy_s(stu[i].level, "不及格");
system("cls");
menuUI();
informationUI();
SetConsoleCursorPosition(handle, coord_menu);
printf("提示:修改成功!\n\n");
break;
case 'b':
printf("将科目B成绩更改为(0~100):");
scanf_s("%lf", &stu[i].clsB);
stu[i].cls0 = stu[i].clsA + stu[i].clsB + stu[i].clsC + stu[i].clsD + stu[i].clsE;
stu[i].cls0 = stu[i].cls0 / 5;
if (stu[i].cls0 > 90)
strcpy_s(stu[i].level, "优");
else if (stu[i].cls0 > 80)
strcpy_s(stu[i].level, "良");
else if (stu[i].cls0 > 70)
strcpy_s(stu[i].level, "中");
else if (stu[i].cls0 > 60)
strcpy_s(stu[i].level, "及格");
else
strcpy_s(stu[i].level, "不及格");
system("cls");
menuUI();
informationUI();
SetConsoleCursorPosition(handle, coord_menu);
printf("提示:修改成功!\n\n");
break;
case 'c':
printf("将科目C成绩更改为(0~100):");
scanf_s("%lf", &stu[i].clsC);
stu[i].cls0 = stu[i].clsA + stu[i].clsB + stu[i].clsC + stu[i].clsD + stu[i].clsE;
stu[i].cls0 = stu[i].cls0 / 5;
if (stu[i].cls0 > 90)
strcpy_s(stu[i].level, "优");
else if (stu[i].cls0 > 80)
strcpy_s(stu[i].level, "良");
else if (stu[i].cls0 > 70)
strcpy_s(stu[i].level, "中");
else if (stu[i].cls0 > 60)
strcpy_s(stu[i].level, "及格");
else
strcpy_s(stu[i].level, "不及格");
system("cls");
menuUI();
informationUI();
SetConsoleCursorPosition(handle, coord_menu);
printf("提示:修改成功!\n\n");
break;
case 'd':
printf("将科目D成绩更改为(0~100):");
scanf_s("%lf", &stu[i].clsD);
stu[i].cls0 = stu[i].clsA + stu[i].clsB + stu[i].clsC + stu[i].clsD + stu[i].clsE;
stu[i].cls0 = stu[i].cls0 / 5;
if (stu[i].cls0 > 90)
strcpy_s(stu[i].level, "优");
else if (stu[i].cls0 > 80)
strcpy_s(stu[i].level, "良");
else if (stu[i].cls0 > 70)
strcpy_s(stu[i].level, "中");
else if (stu[i].cls0 > 60)
strcpy_s(stu[i].level, "及格");
else
strcpy_s(stu[i].level, "不及格");
system("cls");
menuUI();
informationUI();
SetConsoleCursorPosition(handle, coord_menu);
printf("提示:修改成功!\n\n");
break;
case 'e':
printf("将科目E成绩更改为(0~100):");
scanf_s("%lf", &stu[i].clsE);
stu[i].cls0 = stu[i].clsA + stu[i].clsB + stu[i].clsC + stu[i].clsD + stu[i].clsE;
stu[i].cls0 = stu[i].cls0 / 5;
if (stu[i].cls0 > 90)
strcpy_s(stu[i].level, "优");
else if (stu[i].cls0 > 80)
strcpy_s(stu[i].level, "良");
else if (stu[i].cls0 > 70)
strcpy_s(stu[i].level, "中");
else if (stu[i].cls0 > 60)
strcpy_s(stu[i].level, "及格");
else
strcpy_s(stu[i].level, "不及格");
system("cls");
menuUI();
informationUI();
SetConsoleCursorPosition(handle, coord_menu);
printf("提示:修改成功!\n\n");
break;
default:
system("cls");
menuUI();
informationUI();
SetConsoleCursorPosition(handle, coord_menu);
printf("错误!非法输入!\n\n");
break;
}
break;
}
}
}
}
//(4)查找学生
void search()
{
int S, i = 0;
while (1)
{
if (i == 50)
printf("(输入0回到主菜单)\n该学号不存在!请重新输入:");
else
printf("(输入0回到主菜单)\n请输出学号:");
scanf_s("%d", &S);
if (S == 0)
return;
for (i = 0; i < stuSIZE; i++)
{
if (stu[i].num == S)
{
system("cls");
menuUI();
informationUI();
SetConsoleCursorPosition(handle, coord_menu);
printf("搜索结果:\n");
printf("学号:%d\n姓名:%s\n平均分:%.1lf分\n科目A:%.1lf分\n科目B:%.1lf分\n科目C:%.1lf分\n科目D:%.1lf分\n科目E:%.1lf分\n等级:%s\n\n",
stu[i].num, stu[i].name, stu[i].cls0, stu[i].clsA, stu[i].clsB, stu[i].clsC, stu[i].clsD, stu[i].clsE,stu[i].level);
break;
}
}
}
}
int main()
{
first();
read_stu();
while (1)
{
switch (menu())
{
case 1: add(); break;
case 2: dlt(); break;
case 3: modify(); break;
case 4: search(); break;
case 5: sort(1); break;
case 6: sort(2); break;
case 0: save_stu(); exit(0);
default: break;
}
}
}
学生成绩管理系统(大一C语言课程设计)分享
最新推荐文章于 2023-07-24 13:20:52 发布