#define MATH 0
#define CHINESE 1
#define ENGLISH 2
#define HIGHMATH 3
#define BIGGESTSTUDENT 50
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <windows.h>
typedef struct
{
int ID;
char name[20];
char sex[20];
int score[4];
} Student;
Student student[BIGGESTSTUDENT] = {0};
char AllCourse[4][100] = {"数学", "语文", "英语", "计算机"};
bool PasswordVerify();
void ReadStudentAllTable();
void WriteStudentAllTable();
void InputStudent();
Student *SeekStudent(char *name);
Student *SeekStudent(int ID);
Student *SeekVoidTable();
void ModifyStudentID(int oldID, int newID);
void ModifyStudentID(char *oldname, int newID);
void ModifyStudentName(int ID, char *name);
void ModifyStudentName(char *oldname, char *newname);
void ModifyStudentSex(int ID, char *sex);
void ModifyStudentSex(char *name, char *sex);
void ModifyStudentScore(int ID, int course, int score);
void ModifyStudentScore(char *name, int course, int score);
void ShowAllStudentScore();
void ShowStudentScore(int ID);
void ShowStudentScore(char *name);
void SeekStudentSort(int ID);
void SortAllScore(int course);
int StatsCourseScore(float max, float min, int course);
void StatsAll(int course);
void ModifyBaseInformation(int ID);
void ModifyBaseInformation(char *name);
void ModifyScoreInformation(int ID);
void ModifyScoreInformation(char *name);
void ShowAllStudentInformation();
void toxy(int x, int y);
void color(short x);
void over();
void Login();
void RootMenu();
void StudentMenu();
bool ModifyPassword();
void ModifyStudentInformationMenu();
void ReadStudentAllTable()
{
FILE *fp;
fp = fopen("./mysocre.txt", "rb");
if (fp != NULL)
{
if (fread(&student, sizeof(Student) * BIGGESTSTUDENT, 1, fp) != 1)
{
printf("配置文件错误");
}
}
else
{
printf("配置文件错误");
}
fclose(fp);
}
void color(short x)
{
if (x >= 0 && x <= 15)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
}
}
void toxy(int x, int y)
{
COORD pos = {x, y};
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
void WriteStudentAllTable()
{
FILE *fp;
fp = fopen("./mysocre.txt", "wb");
if (fp == NULL)
{
fclose(fp);
fp = fopen("./mysocre.txt", "wb+");
}
if (fwrite(&student, sizeof(Student) * BIGGESTSTUDENT, 1, fp) != 1)
{
printf("写入配置文件错误");
}
fclose(fp);
}
Student *SeekVoidTable()
{
for (int i = 0; i < 100; i++)
{
if (student[i].ID == 0)
{
return &student[i];
}
}
}
void InputStudent()
{
char i;
for (; i != 27;)
{
Student *tempstu = SeekVoidTable();
printf("请输入学号:");
scanf("%d", &tempstu->ID);
printf("请输入姓名:");
scanf("%s", tempstu->name);
printf("请输入性别:");
scanf("%s", tempstu->sex);
printf("请输入数学:");
scanf("%d", &tempstu->score[0]);
printf("请输入语文:");
scanf("%d", &tempstu->score[1]);
printf("请输入英语:");
scanf("%d", &tempstu->score[2]);
printf("请输入计算机:");
scanf("%d", &tempstu->score[3]);
WriteStudentAllTable();
printf("继续输入请按回车,结束输入请按Esc\n");
i = getch();
for (; i != 13 && i != 27;)
i = getch();
}
}
Student *SeekStudent(char *name)
{
for (int i = 0; i < BIGGESTSTUDENT; i++)
{
if (strcmp(student[i].name, name) == 0)
{
return &student[i];
}
}
return NULL;
}
Student *SeekStudent(int ID)
{
for (int i = 0; i < BIGGESTSTUDENT; i++)
{
if (student[i].ID == ID)
{
return &student[i];
}
}
return NULL;
}
void ModifyStudentID(int oldID, int newID)
{
Student *tempstu = SeekStudent(oldID);
if (tempstu != NULL)
{
tempstu->ID = newID;
WriteStudentAllTable();
}
}
void ModifyStudentID(char *oldname, int newID)
{
Student *tempstu = SeekStudent(oldname);
if (tempstu != NULL)
{
tempstu->ID = newID;
WriteStudentAllTable();
}
}
void ModifyStudentName(int ID, char *name)
{
Student *tempstu = SeekStudent(ID);
if (tempstu != NULL)
{
memcpy(tempstu->name, name, sizeof(tempstu->name));
WriteStudentAllTable();
}
}
void ModifyStudentName(char *oldname, char *newname)
{
Student *tempstu = SeekStudent(oldname);
if (tempstu != NULL)
{
memcpy(tempstu->name, newname, sizeof(tempstu->name));
WriteStudentAllTable();
}
}
void ModifyStudentSex(int ID, char *sex)
{
Student *tempstu = SeekStudent(ID);
if (tempstu != NULL)
{
memcpy(tempstu->sex, sex, sizeof(tempstu->sex));
WriteStudentAllTable();
}
}
void ModifyStudentSex(char *name, char *sex)
{
Student *tempstu = SeekStudent(name);
if (tempstu != NULL)
{
memcpy(tempstu->name, name, sizeof(tempstu->name));
WriteStudentAllTable();
}
}
void ModifyStudentScore(int ID, int course, int score)
{
Student *tempstu = SeekStudent(ID);
if (tempstu != NULL)
{
tempstu->score[course] = score;
WriteStudentAllTable();
}
else
{
printf("学生不存在");
}
}
void ModifyStudentScore(char *name, int course, int score)
{
Student *tempstu = SeekStudent(name);
if (tempstu != NULL)
{
tempstu->score[course] = score;
WriteStudentAllTable();
}
else
{
printf("学生不存在");
}
}
void ShowAllStudentScore()
{
for (int i = 0; i < BIGGESTSTUDENT; i++)
{
if (student[i].ID != 0)
{
printf("学号ID:%d\t姓名:%s\t数学:%d\t汉语:%d\t英语:%d\t计算机:%d\t",
student[i].ID, student[i].name, student[i].score[0], student[i].score[1], student[i].score[2], student[i].score[3]);
}
}
}
void ShowStudentScore(int ID)
{
Student *tempstu = SeekStudent(ID);
if (tempstu != NULL && tempstu->ID != 0)
{
printf("学号ID:%d\t姓名:%s\t数学:%d\t汉语:%d\t英语:%d\t计算机:%d\t总成绩:%d\t",
tempstu->ID, tempstu->name, tempstu->score[0], tempstu->score[1], tempstu->score[2], tempstu->score[3],
tempstu->score[0] + tempstu->score[1] + tempstu->score[2] + tempstu->score[3]);
}
else
{
printf("学生不存在");
}
}
void ShowStudentScore(char *name)
{
Student *tempstu = SeekStudent(name);
if (tempstu != NULL && tempstu->ID != 0)
{
printf("学号ID:%d\t姓名:%s\t数学:%d\t汉语:%d\t英语:%d\t计算机:%d\t总成绩:%d\t",
tempstu->ID, tempstu->name, tempstu->score[0], tempstu->score[1], tempstu->score[2], tempstu->score[3],
tempstu->score[0] + tempstu->score[1] + tempstu->score[2] + tempstu->score[3]);
}
else
{
printf("学生不存在");
}
}
void SeekStudentSort(int ID)
{
int rank = 1;
int score = 0;
Student *tempstu = SeekStudent(ID);
if (tempstu != NULL && tempstu->ID != 0)
{
for (int i = 0; i < 4; i++)
{
score += tempstu->score[i];
}
for (int i = 0; i < BIGGESTSTUDENT; i++)
{
int otherscore = 0;
for (int j = 0; j < 4; j++)
{
otherscore += student[i].score[j];
}
if (score < otherscore)
{
rank++;
}
}
printf("总名次为:%d", rank);
}
else
{
printf("学生不存在");
}
}
void SortAllScore(int course)
{
int tempsort[BIGGESTSTUDENT] = {0};
int tempscore[BIGGESTSTUDENT] = {0};
for (int i = 0; i < BIGGESTSTUDENT; i++)
{
tempsort[i] = i;
if (course != 4)
{
tempscore[i] = student[i].score[course];
}
else
{
for (int j = 0; j < 4; j++)
{
tempscore[i] += student[i].score[j];
}
}
}
for (int i = 0; i < BIGGESTSTUDENT; i++)
{
for (int j = 0; j < BIGGESTSTUDENT - i - 1; j++)
{
if (tempscore[j] < tempscore[j + 1])
{
int temp = tempscore[j];
tempscore[j] = tempscore[j + 1];
tempscore[j + 1] = temp;
temp = tempsort[j];
tempsort[j] = tempsort[j + 1];
tempsort[j + 1] = temp;
}
}
}
for (int i = 0; i < BIGGESTSTUDENT; i++)
{
if (student[tempsort[i]].ID != 0)
{
printf("学号ID:%d\t姓名:%s\t数学:%d\t汉语:%d\t英语:%d\t计算机:%d\t总成绩:%d\t\n",
student[tempsort[i]].ID, student[tempsort[i]].name, student[tempsort[i]].score[0], student[tempsort[i]].score[1], student[tempsort[i]].score[2], student[tempsort[i]].score[3],
student[tempsort[i]].score[0] + student[tempsort[i]].score[1] + student[tempsort[i]].score[2] + student[tempsort[i]].score[3]);
printf("------------------------------------------------------------------------------------------------------------------------\n");
}
}
}
int StatsCourseScore(float max, float min, int course)
{
int number = 0;
float tempscore = 0;
for (int i = 0; i < BIGGESTSTUDENT; i++)
{
if (course != 5)
{
tempscore = student[i].score[course];
if (tempscore <= max && tempscore > min)
{
number++;
}
}
else
{
for (int j = 0; j < 4; j++)
{
tempscore = student[i].score[j];
}
if (tempscore <= max && tempscore > min)
{
number++;
}
}
}
return number;
}
void StatsAll(int course)
{
float pass = 0, max = 0, min = 100, avg = 0, all = 0;
int passpeople = 0;
int allpeople = 0;
if (course != 5)
{
for (int i = 0; i < BIGGESTSTUDENT; i++)
{
if (student[i].ID != 0)
{
allpeople++;
if (student[i].score[course] > max)
{
max = student[i].score[course];
}
if (student[i].score[course] < min)
{
min = student[i].score[course];
}
if (student[i].score[course] >= 60)
{
passpeople++;
}
all += student[i].score[course];
}
}
pass = passpeople * 1.0 / allpeople;
avg = all / allpeople;
printf("科目:%s\t总分:%.2f\t及格率:%.2f\t最高分:%.2f\t最低分:%.2f\t平均分:%.2f\t\n\n", AllCourse[course], all, pass, max, min, avg);
}
else
{
printf("科目错误");
}
}
void ModifyBaseInformation(int ID)
{
Student *tempstu = SeekStudent(ID);
if (tempstu != NULL)
{
printf("请输入学号:");
scanf("%d", &tempstu->ID);
printf("请输入姓名:");
scanf("%s", tempstu->name);
printf("请输入性别:");
scanf("%s", tempstu->sex);
WriteStudentAllTable();
}
else
{
printf("学生不存在");
}
}
void ModifyBaseInformation(char *name)
{
Student *tempstu = SeekStudent(name);
if (tempstu != NULL)
{
printf("请输入学号:");
scanf("%d", &tempstu->ID);
memset(tempstu->name, '\0', sizeof(tempstu->name));
printf("请输入姓名:");
scanf("%s", tempstu->name);
memset(tempstu->sex, '\0', sizeof(tempstu->sex));
printf("请输入性别:");
scanf("%s", tempstu->sex);
WriteStudentAllTable();
}
else
{
printf("学生不存在");
}
}
void ModifyScoreInformation(int ID)
{
Student *tempstu = SeekStudent(ID);
if (tempstu != NULL)
{
printf("请输入数学:");
scanf("%d", &tempstu->score[0]);
printf("请输入语文:");
scanf("%d", &tempstu->score[1]);
printf("请输入英语:");
scanf("%d", &tempstu->score[2]);
printf("请输入计算机:");
scanf("%d", &tempstu->score[3]);
WriteStudentAllTable();
}
else
{
printf("学生不存在");
}
}
void ModifyScoreInformation(char *name)
{
Student *tempstu = SeekStudent(name);
if (tempstu != NULL)
{
printf("请输入数学:");
scanf("%d", &tempstu->score[0]);
printf("请输入语文:");
scanf("%d", &tempstu->score[1]);
printf("请输入英语:");
scanf("%d", &tempstu->score[2]);
printf("请输入计算机:");
scanf("%d", &tempstu->score[3]);
WriteStudentAllTable();
}
else
{
printf("学生不存在");
}
}
void ShowAllStudentInformation()
{
for (int i = 0; i < BIGGESTSTUDENT; i++)
{
if (student[i].ID != 0)
{
printf("学号:%d\t姓名:%s\t性别:%s\n", student[i].ID, student[i].name, student[i].sex);
printf("-----------------------------------------------\n");
}
}
}
void Login()
{
system("cls");
color(15);
toxy(50, 5);
printf(" 成绩管理系统");
toxy(48, 8);
printf("| 1.管理登录 |");
toxy(48, 10);
printf("| 2.学生入口 |");
toxy(48, 12);
printf("| 3.退出系统 |");
toxy(48, 14);
printf("| 请按键选择,回车确定 |");
printf("\n");
char choose[10];
char t;
char choosetable[3][100] = {"1", "2", "3"};
bool judgechoose = false;
do
{
toxy(48, 16);
scanf("%s", choose);
for (int i = 0; i < 3; i++)
{
if (strcmp(choose, choosetable[i]) == 0)
{
judgechoose = true;
break;
}
if (i == 3)
{
printf("\n选择不合规,请重新输入功能选项:");
}
}
} while (!judgechoose);
switch (choose[0])
{
case '1':
if (PasswordVerify())
{
while (true)
{
RootMenu();
}
}
break;
case '2':
while (true)
{
StudentMenu();
}
break;
case '3':
over();
break;
}
}
void ModifyStudentInformationMenu()
{
system("cls");
printf("**************************************************");
printf("\n 1.修改学生基本信息\n");
printf("\n 2.修改学生成绩\n");
printf("\n 3.返回上一层\n");
printf("\n 请按键选择,回车确定\n");
printf("*************************************************\n");
char choose[10];
char choosetable[3][100] = {"1", "2", "3"};
bool judgechoose = false;
do
{
scanf("%s", choose);
for (int i = 0; i < 3; i++)
{
if (strcmp(choose, choosetable[i]) == 0)
{
judgechoose = true;
break;
}
if (i == 3)
{
printf("\n选择不合规,请重新输入功能选项:");
}
}
} while (!judgechoose);
int tempID;
switch (choose[0])
{
case '1':
system("cls");
printf("输入需要修改的ID:");
scanf("%d", &tempID);
ModifyBaseInformation(tempID);
printf("\n\n\n******按任意键继续******\n\n");
system("pause");
system("cls");
break;
case '2':
system("cls");
printf("输入需要修改的ID:");
scanf("%d", &tempID);
ModifyScoreInformation(tempID);
printf("\n\n\n******按任意键继续******\n\n");
system("pause");
system("cls");
break;
case '3':
break;
}
}
void RootMenu()
{
system("cls");
printf("**************************************************");
printf("\n 1.输入学生成绩\n");
printf("\n 2.更改学生信息\n");
printf("\n 3.显示学生基本信息\n");
printf("\n 4.成绩统计分析\n");
printf("\n 5.统计各科各分数段人数\n");
printf("\n 6.学生成绩排名\n");
printf("\n 7.修改密码\n");
printf("\n 8.返回上一层\n");
printf("\n 请按键选择,回车确定\n");
printf("*************************************************\n");
char choose[10];
char choosetable[8][100] = {"1", "2", "3", "4", "5", "6", "7", "8"};
bool judgechoose = false;
do
{
scanf("%s", choose);
for (int i = 0; i < 8; i++)
{
if (strcmp(choose, choosetable[i]) == 0)
{
judgechoose = true;
break;
}
if (i == 8)
{
printf("\n选择不合规,请重新输入功能选项:");
}
}
} while (!judgechoose);
switch (choose[0])
{
case '1':
system("cls");
InputStudent();
break;
case '2':
system("cls");
ModifyStudentInformationMenu();
system("pause");
system("cls");
break;
case '3':
system("cls");
ShowAllStudentInformation();
system("pause");
system("cls");
break;
case '4':
system("cls");
for (int i = 0; i < 4; i++)
{
StatsAll(i);
}
system("pause");
system("cls");
break;
case '5':
system("cls");
for (int i = 0; i < 4; i++)
{
for (int j = 0, k = 0; j < 5; j++, k += 20)
{
printf("%s\t%d~%d\t人数%d\t\t", AllCourse[i], k, k + 20, StatsCourseScore(k + 20, k, i));
}
printf("\n");
}
system("pause");
system("cls");
break;
case '6':
system("cls");
printf("总成绩:\n");
SortAllScore(4);
printf("\n\n\n");
system("pause");
system("cls");
break;
case '7':
system("cls");
if (ModifyPassword())
{
printf("\n修改完成\n");
}
else
{
printf("\n修改失败\n");
}
system("pause");
break;
case '8':
Login();
break;
}
}
void StudentMenu()
{
system("cls");
printf("**************************************************");
printf("\n 1.查找成绩\n");
printf("\n 2.成绩排名\n");
printf("\n 3.返回上一层\n");
printf("\n 请按键选择,回车确定\n");
printf("*************************************************\n");
char choose[10];
char choosetable[3][100] = {"1", "2", "3"};
bool judgechoose = false;
do
{
scanf("%s", choose);
for (int i = 0; i < 3; i++)
{
if (strcmp(choose, choosetable[i]) == 0)
{
judgechoose = true;
break;
}
if (i == 3)
{
printf("\n选择不合规,请重新输入功能选项:");
}
}
} while (!judgechoose);
char temp[20];
int ID;
int tempID;
int tempbit = 0;
;
switch (choose[0])
{
case '1':
system("cls");
printf("请输入要查询的学号或姓名:");
scanf("%s", temp);
ID = atoi(temp);
tempID = ID;
while (tempID > 0)
{
tempID = tempID / 10;
tempbit++;
}
if (tempbit == strlen(temp))
{
ShowStudentScore(ID);
}
else
{
ShowStudentScore(temp);
}
printf("\n");
system("pause");
system("cls");
break;
case '2':
system("cls");
printf("请输入要查询的学号:");
scanf("%d", &tempID);
SeekStudentSort(tempID);
printf("\n");
system("pause");
system("cls");
break;
case '3':
Login();
break;
}
}
bool PasswordVerify()
{
system("cls");
char password_buff[255];
char inputpassword[255];
FILE *fp;
fp = fopen("PassWord.txt", "r");
if (fp != NULL)
{
fgets(password_buff, 255, fp);
}
else
{
printf("配置文件错误");
}
fclose(fp);
bool judgepassword = false;
printf("%s", "请输入账号:\n");
char number[255];
scanf("%s", &number);
printf("请输入密码:\n");
int passwordBit = 0;
while (true)
{
while (true)
{
char temp;
temp = getch();
if (temp != 8)
{
if (temp != '\r')
{
inputpassword[passwordBit] = temp;
}
else
{
inputpassword[passwordBit] = '\0';
break;
}
passwordBit++;
printf("*");
}
if (temp == 8 && passwordBit > 0)
{
printf("\b \b");
passwordBit--;
}
}
if (strcmp(inputpassword, password_buff) == 0)
{
return true;
}
else if (inputpassword[0] == '#' && inputpassword[1] == '\0')
{
return false;
}
printf("\n密码错误,请重新输入:");
passwordBit = 0;
}
}
void over()
{
char t;
toxy(48, 11);
printf("-----------------------");
toxy(48, 12);
printf("| 您确定要退出吗? |");
toxy(48, 14);
printf("| 1.确定 2.取消 |");
toxy(48, 15);
printf("-----------------------");
while (1)
{
t = getch();
switch (t)
{
case '1':
system("cls");
color(6);
toxy(48, 10);
printf("正在安全退出....");
Sleep(1000);
system("cls");
color(8);
toxy(48, 10);
printf("已安全退出软件");
toxy(48, 12);
printf("谢谢使用!");
toxy(48, 14);
printf("by-by^_^");
exit(0);
break;
case '2':
Login();
break;
default:
break;
}
}
}
bool ModifyPassword()
{
char inputPassword1[255];
char inputPassword2[255];
char *inputpassword;
if (PasswordVerify())
{
int passwordBit = 0;
while (true)
{
for (int k = 0; k < 2; k++)
{
if (k == 0)
{
inputpassword = inputPassword1;
printf("\n请输入新密码,或输入#退出修改:");
}
else
{
inputpassword = inputPassword2;
printf("\n请输入第二次输入新密码:");
}
while (true)
{
char temp;
temp = getch();
if (temp != 8)
{
if (temp != '\r')
{
inputpassword[passwordBit] = temp;
}
else
{
inputpassword[passwordBit] = '\0';
break;
}
passwordBit++;
printf("*");
}
if (temp == 8 && passwordBit > 0)
{
printf("\b \b");
passwordBit--;
}
if (k == 0 && inputPassword1[0] == '#' && inputPassword1[1] == '\0')
{
return false;
}
}
passwordBit = 0;
}
if (strcmp(inputPassword1, inputPassword2) == 0)
{
break;
}
printf("\n两次密码不一致,请重新输入:");
}
printf("\n修改成功!");
FILE *fp;
fp = fopen("PassWord.txt", "w+");
if (fp != NULL)
{
fputs(inputPassword1, fp);
}
else
{
printf("配置文件错误");
}
fclose(fp);
return true;
}
return false;
}
int main(int argc, char const *argv[])
{
system("chcp 65001");
system("cls");
ReadStudentAllTable();
while (1)
{
Login();
}
return 0;
}