#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define A 1314
/*定义了一个结构体来存放学生信息*/
typedef struct
{
char stu_name[20];
int stu_id;
double math;
double chinese;
double english;
double av_grade;
}student;
student all_stu[100], rf[100];
int stu_number = 0;
void start_menu();//开始菜单
void teacher_menu();//老师界面
void stu_menu();//学生界面
void input_stu();//输入信息
void output_stu();//输出信息
void delete_stu();//删除信息
void find_stu();//查找信息
void change_stu();//修改信息
void write_file();//写入文件操作
void printflove();//打印爱心
void find_stu2();//学生端查询成绩
void Exit();//程序结束函数
int main() {
FILE* pf;
pf = fopen("学生成绩信息.dat", "at+");//打开文件
if (pf == NULL)
{
printf("打开失败,请检查文件是否存在\n");
}
while (!feof(pf))//将文件内容写入
{
fscanf(pf, "%s%d%lf%lf%lf%lf\n", &all_stu[stu_number].stu_name, &all_stu[stu_number].stu_id, &all_stu[stu_number].chinese, &all_stu[stu_number].math, &all_stu[stu_number].english, &all_stu[stu_number].av_grade);
if (strcmp(all_stu[stu_number].stu_name, "\0"))
stu_number++;
}
fclose(pf);//关闭文件
start_menu();
return 0;
}
/*开始菜单*/
void start_menu() {
int num;
int YesOrNo = 1;
int y;
int i;
system("cls");
printf("\t\t\t\t\t*******************************************\n");
printf("\t\t\t\t\t*******************************************\n");
printf("\t\t\t\t\t* 尊敬的用户:您好! *\n");
printf("\t\t\t\t\t* 欢迎使用 *\n");
printf("\t\t\t\t\t* 成绩管理系统 *\n");
printf("\t\t\t\t\t###########################################\n");
printf("\t\t\t\t\t* 感谢使用本系统, *\n");
printf("\t\t\t\t\t* 希望您体验愉快。 *\n");
printf("\t\t\t\t\t* *\n");
printf("\t\t\t\t\t* 按1登录老师系统 *\n");
printf("\t\t\t\t\t* 按2登录学生系统 *\n");
printf("\t\t\t\t\t* 按0退出系统 *\n");
printf("\t\t\t\t\t*******************************************\n");
printf("\t\t\t\t\t*******************************************\n");
st:
printf("\n");
printf("请选择:");
while (YesOrNo)
{
scanf("%d", &num); //选择界面
switch (num)
{
case 1:
//进入系统
//输入密码
printf("请输入您的个人密码:\n");
scanf("%d", &i);
int temp;
for (y = 2; y >= 0; y--) //3次机会
{
if (i != A)
{
printf("账号或密码输入错误,您还有%d次机会。\n请重新输入:\n", y+1);
scanf("%d", &i);
if (y==0 ) {
printf("账号或密码多次输入错误,系统关闭。\n");
system("pause");
system("cls");
Exit();
}}
else {
teacher_menu();
}
}
break;
case 2:
system("cls");
stu_menu();
break;
case 0:
//退出系统
Exit();
break;
default:
printf("输入错误,请重新输入:");
goto st;
YesOrNo = 0;
break;
}
}
}
/*老师操作界面*/
void teacher_menu()
{
system("cls");
int isExist = 1;
int num;
char Out;
int isExist1 = 1;
while (isExist)
{
system("cls");
printf("\n\n\n\n");
printf("\t\t=====================学生成绩管理系统=================\n");
printf("\t\t* *\n");
printf("\t\t* 1. 输入学生成绩 *\n");
printf("\t\t* 2. 查找学生成绩 *\n");
printf("\t\t* 3. 显示所有成绩 *\n");
printf("\t\t* 4. 删除学生成绩 *\n");
printf("\t\t* 5. 修改学生成绩 *\n");
printf("\t\t* 6. 返回主界面 *\n");
printf("\t\t* 0. 退出管理系统 *\n");
printf("\t\t* 欢迎使用! *\n");
printf("\t\t=======================================================\n");
I:
printf("\t\t输入你的选项:");
scanf("%d", &num);
switch (num)
{
case 1:
system("cls");
input_stu();//输入信息
break;
case 2:
system("cls");
find_stu();//查找信息
break;
case 3:
system("cls");
output_stu();//输出信息
break;
case 4:
system("cls");
delete_stu();//删除信息
break;
case 5:
system("cls");
change_stu();//修改信息
break;
case 6:
system("cls");
start_menu();
break;
case 0://推出系统
Exit();
break;
default:
printf("指令错误,请重新输入\n");
goto I;
break;
}
}
}
/*输入学生信息*/
void input_stu()
{
start:
system("cls");
printf(" ******************输入学生信息******************");
printf("\n");
printf("\n");
int a = 0, temp = 0;
double aver;
char c1, c2;
printf(">>>>>>>>>请输入姓名:");
scanf("%s", all_stu[stu_number].stu_name);
printf("\n");
one:
printf(">>>>>>>>>请输入学号(数字):");
scanf("%d", &temp);
printf("\n");
all_stu[stu_number].stu_id = temp;
getchar();
for (a = 0; a < stu_number; a++)
{
if (all_stu[stu_number].stu_id == all_stu[a].stu_id)
{
printf("学号重复,请按(Y)重新输入,按(N)退出\n");
cf:
c1 = getch();
if (c1 == 'y' || c1 == 'Y')
goto one;
else if (c1 == 'n' || c1 == 'N')
goto end;
else
{
printf("指令错误,请重新输入\n");
goto cf;
}
}
}
two:
printf(">>>>>>>>>请输入语文成绩( <=100 ):");
scanf("%lf", &all_stu[stu_number].chinese);
printf("\n");
if (all_stu[stu_number].chinese > 100||all_stu[stu_number].chinese<0)
{
printf("输入错误,请按(Y)重新输入\n");
srI:
c1 = getch();
if (c1 == 'y' || c1 == 'Y')
goto two;
else
{
printf("指令错误,请重新输入\n");
goto srI;
}
}
three:
printf(">>>>>>>>>请输入数学成绩( <=100 ):");
scanf("%lf", &all_stu[stu_number].math);
printf("\n");
if (all_stu[stu_number].math > 100||all_stu[stu_number].math<0)
{
printf("输入错误,请按(Y)重新输入\n");
srII:
c1 = getch();
if (c1 == 'y' || c1 == 'Y')
goto three;
else
{
printf("指令错误,请重新输入\n");
goto srII;
}
}
four:
printf(">>>>>>>>>请输入英语成绩( <=100 ):");
scanf("%lf", &all_stu[stu_number].english);
printf("\n");
if (all_stu[stu_number].english > 100||all_stu[stu_number].english<0)
{
printf("输入错误,请按(Y)重新输入\n");
srIII:
c1 = getch();
if (c1 == 'y' || c1 == 'Y')
goto four;
else
{
printf("指令错误,请重新输入\n");
goto srIII;
}
}
aver = (all_stu[stu_number].chinese + all_stu[stu_number].math + all_stu[stu_number].english) / 3.0;//计算平均分
all_stu[stu_number].av_grade = aver;
printf(">>>>>>>>>>平均分为:%.2lf\n", all_stu[stu_number].av_grade);
write_file();//此处调用写入文件操作
stu_number++;
printf("是否还要输入,需要请按(Y),不需要请按(N)\n");
z:
c2 = getch();
if (c2 == 'y' || c2 == 'Y')
goto start;
else if (c2 == 'n' || c2 == 'N')
goto end;
else
{
printf("指令错误,请重新输入\n");
goto z;
}
end:
getchar();
}
/*显示所有学生的成绩信息*/
void output_stu()
{
char c;
printf(" <<<<<<<<<<<<<<<<<下面是各位学生的成绩信息>>>>>>>>>>>>>>>>");
printf("\n\n");
int j = 0;
int i = 0;
printf("名字\t学号\t语文\t数学\t英语\t平均值\n\n");
for (; i < stu_number; i++)
{
printf("%s\t", all_stu[i].stu_name);
printf("%d\t", all_stu[i].stu_id);
printf("%.2lf\t", all_stu[i].chinese);
printf("%.2lf\t", all_stu[i].math);
printf("%.2lf\t", all_stu[i].english);
printf("%0.2lf\t", all_stu[i].av_grade);
printf("\n\n");
}
getchar();
system("pause");
}
/*查找学生的信息*/
void find_stu()
{
char c;
start:
printf(" <<<<<<<<<<<<<<<<<查找学生的成绩信息>>>>>>>>>>>>>>>>");
printf("\n\n");
one:
system("cls");
printf(">>>>>>>>>请输入要查找的学生的学号:");
int a = 0, num = 0, t = 0;
scanf("%d", &num);
for (; a < stu_number; a++)
{
if (all_stu[a].stu_id == num)
{
t = 1;
break;
}
}
if (t == 0)
{
printf("\n对不起,没有查询到该学号,请按(Y)重新输入,按(N)退出\n");
II:
c = getch();
if (c == 'y' || c == 'Y')
goto one;
else if (c == 'n' || c == 'N')
goto end;
else
{
printf("指令错误,请重新输入\n");
goto II;
}
}
else if (t == 1)
{
printf(">>>名字:%s\n", all_stu[a].stu_name);
printf(">>>学号:%d\n", all_stu[a].stu_id);
printf(">>>语文:%0.2lf分\n", all_stu[a].chinese);
printf(">>>数学:%0.2lf分\n", all_stu[a].math);
printf(">>>英语:%0.2lf分\n", all_stu[a].english);
printf(">>>平均分:%0.2lf分\n", all_stu[a].av_grade);
}
printf("\n");
printf("查询已完成,是否还要查询,是则按(Y),否则按(N)\n");
j:
c = getch();
if (c == 'y' || c == 'Y')
goto start;
else if (c == 'n' || c == 'N')
goto end;
else
{
printf("指令错误,请重新输入\n");
goto j;
}
end:
getchar();
}
/*删除学生的信息*/
void delete_stu()
{
FILE* pf;
char c;
start:
system("cls");
printf(" <<<<<<<<<<<<<<<<<删除学生的成绩信息>>>>>>>>>>>>>>>>");
printf("\n\n");
one:
printf(">>>>>>>>>请输入要删除的学生的学号:");
int a = 0, num = 0, t = 0;
scanf("%d", &num);
for (; a < stu_number; a++)
{
if (all_stu[a].stu_id == num)//判断是否有这个学生
{
t = 1;
break;
}
}
if (t == 0)
{
printf("\n对不起,没有查询到该学号,请按(Y)重新输入,按(N)退出\n");
c = getch();
if (c == 'y' || c == 'Y')
goto one;
if (c == 'n' || c == 'N');
}
if (t == 1)
{
printf("下面是你想要删除的学生信息,确认删除请按(y),否则按(N)\n\n");
printf("名字\t学号\t语文\t数学\t英语\t平均分\n");
printf("%s\t", all_stu[a].stu_name);
printf("%d\t", all_stu[a].stu_id);
printf("%.2lf\t", all_stu[a].chinese);
printf("%.2lf\t", all_stu[a].math);
printf("%.2lf\t", all_stu[a].english);
printf("%0.2lf\t", all_stu[a].av_grade);
printf("\n\n");
HI:
c = getch();
if (c == 'y' || c == 'Y')
{
stu_number--;
for (; a < stu_number; a++)
{
all_stu[a] = all_stu[a + 1];//删除学生后,使得这个学生后面的学生往前推
}
}
else if (c == 'N' || c == 'n')
goto b;
else
{
printf("指令错误,请重新输入\n");
goto HI;
}
}
pf = fopen("temp2.dat", "w");//新建一个文件,用来储存新的学生学生信息
if (pf == NULL)
{
printf("打开失败\n");
}
int j = 0;
for (j = 0; j < stu_number; j++)
{
fprintf(pf, "%s %d %.2lf %.2lf %.2lf %.2lf\n", all_stu[j].stu_name, all_stu[j].stu_id, all_stu[j].chinese, all_stu[j].math, all_stu[j].english, all_stu[j].av_grade);
}
fclose(pf);
remove("学生成绩信息.dat");//删除原文件
rename("temp2.dat", "学生成绩信息.dat");//将新文件重命名
printf("\n");
b:
printf("是否还要删除,是则按(Y),否则按(N)\n");
c = getch();
if (c == 'y' || c == 'Y')
goto start;
else if (c == 'n' || c == 'N');
else
{
printf("指令错误,请重新输入\n");
goto b;
}
}
/*修改学生的信息*/
void change_stu()
{
FILE* pf2;
int j = 0;
int k=0;
char c;
start:
system("cls");
printf(" <<<<<<<<<<<<<<<<<修改学生的成绩信息>>>>>>>>>>>>>>>>");
printf("\n\n");
once:
printf(">>>>>>>>>请输入要修改的学生的学号:");
int a = 0, num = 0, t = 0, i = 0, b = 0;
scanf("%d", &num);
for (; a < stu_number; a++)
{
if (all_stu[a].stu_id == num)
{
t = 1;
break;
}
}
if (t == 0)
{
printf("\n对不起,没有查询到该学号,请按(Y)重新输入,按(N)退出\n");
VIII:
c = getch();
if (c == 'y' || c == 'Y')
goto once;
else if (c == 'n' || c == 'N')
goto end;
else
{
printf("指令错误,请重新输入\n");
goto VIII;
}
}
else if (t == 1)
{
printf(">>>名字:%s\n", all_stu[a].stu_name);
printf(">>>学号:%d\n", all_stu[a].stu_id);
printf(">>>语文:%0.2lf分\n", all_stu[a].chinese);
printf(">>>数学:%0.2lf分\n", all_stu[a].math);
printf(">>>英语:%0.2lf分\n", all_stu[a].english);
printf(">>>平均分:%0.2lf分\n", all_stu[a].av_grade);
}
printf("\n");
printf("是否要更改此学生的成绩信息(修改后不能复原)?是则按(Y),否则按(N)\n");
VV:
c = getch();
if (c == 'y' || c == 'Y')
goto change;
else if (c == 'n' || c == 'N')
goto end;
else
{
printf("指令错误,请重新输入\n");
goto VV;
}
change:
system("cls");
printf(" ******************请输入修改信息******************");
printf("\n");
printf("\n");
int temp = 0;
double aver;
char c1, c2;
printf(">>>>>>>>>请输入姓名:");
scanf("%s", all_stu[a].stu_name);
printf("\n");
one:
printf(">>>>>>>>>请输入学号(数字):");
scanf("%d", &temp);
printf("\n");
all_stu[a].stu_id = temp;
getchar();
for(j=0;j<a;j++)
{
if (all_stu[j].stu_id == all_stu[a].stu_id)
{
printf("学号重复,请按(Y)重新输入,按(N)退出\n");
III:
c1 = getch();
if (c1 == 'y' || c1 == 'Y')
goto one;
else if(c1=='n'||c1=='N')
goto end;
else
{
printf("指令错误,请重新输入\n");
goto III;
}
}
}
for(k=a+1;k<=stu_number;k++)
{
if (all_stu[k].stu_id == all_stu[a].stu_id)
{
printf("学号重复,请按(Y)重新输入,按(N)退出\n");
VIIIII:
c2 = getch();
if (c2 == 'y' || c2 == 'Y')
goto one;
else if(c2=='n'||c2=='N')
goto end;
else
{
printf("指令错误,请重新输入\n");
goto VIIIII;
}
}
}
two:
printf(">>>>>>>>>请输入语文成绩( <=100 ):");
scanf("%lf", &all_stu[a].chinese);
printf("\n");
if (all_stu[a].chinese > 100||all_stu[a].chinese<0)
{
printf("输入错误,请按(Y)重新输入\n");
IIII:
c1 = getch();
if (c1 == 'y' || c1 == 'Y')
goto two;
else
{
printf("指令错误,请重新输入\n");
goto IIII;
}
}
three:
printf(">>>>>>>>>请输入数学成绩( <=100 ):");
scanf("%lf", &all_stu[a].math);
printf("\n");
if (all_stu[a].math > 100||all_stu[a].math<0)
{
printf("输入错误,请按(Y)重新输入\n");
IV:
c1 = getch();
if (c1 == 'y' || c1 == 'Y')
goto three;
else
{
printf("指令错误,请重新输入\n");
goto IV;
}
}
four:
printf(">>>>>>>>>请输入英语成绩( <=100 ):");
scanf("%lf", &all_stu[a].english);
printf("\n");
if (all_stu[a].english > 100||all_stu[a].english<0)
{
printf("输入错误,请按(Y)重新输入\n");
IIV:
c1 = getch();
if (c1 == 'y' || c1 == 'Y')
goto four;
else
{
printf("指令错误,请重新输入\n");
goto IIV;
}
}
aver = (all_stu[a].chinese + all_stu[a].math + all_stu[a].english) / 3.0;
all_stu[a].av_grade = aver;
printf(">>>>>>>>>>平均分为:%.2lf\n", all_stu[a].av_grade);
l:
printf("修改工作已完成,是否还要修改,是则按(Y),否则按(N)\n");
c = getch();
if (c == 'y' || c == 'Y')
goto start;
else if (c == 'n' || c == 'N')
goto end;
else
{
printf("指令错误,请重新输入\n");
goto l;
}
end:
pf2 = fopen("temp2.dat", "w");//新建一个文件,用来储存修改后的学生成绩信息
if (pf2 == NULL)
{
printf("打开失败\n");
}
for (j = 0; j < stu_number; j++)
{
fprintf(pf2, "%s %d %.2lf %.2lf %.2lf %.2lf\n", all_stu[j].stu_name, all_stu[j].stu_id, all_stu[j].chinese, all_stu[j].math, all_stu[j].english, all_stu[j].av_grade);
}
fclose(pf2);
remove("学生成绩信息.dat");//删除原来的文件
rename("temp2.dat", "学生成绩信息.dat");//将新的文件重命名
getchar();
}
/*将学生信息写入文件*/
void write_file()
{
FILE* pf = fopen("学生成绩信息.dat", "a+");
if (pf == NULL)
{
printf("打开失败,请检查文件是否存在\n");
}
fprintf(pf, "%s ", all_stu[stu_number].stu_name);
fprintf(pf, "%d ", all_stu[stu_number].stu_id);
fprintf(pf, "%.2lf ", all_stu[stu_number].chinese);
fprintf(pf, "%.2lf ", all_stu[stu_number].math);
fprintf(pf, "%.2lf ", all_stu[stu_number].english);
fprintf(pf, "%.2lf\n", all_stu[stu_number].av_grade);
fclose(pf);
}
/*一个爱心*/
void printflove() {
float a, x, y;
for (y = 1.5f; y > -1.5f; y -= 0.1f) {
for (x = -1.5f; x < 1.5f; x += 0.05f) {
a = x * x + y * y - 1;
char ch = a * a * a - x * x * y * y * y <= 0.0f ? '*' : ' ';
putchar(ch);
}
printf("\n");
printf("\t\t\t ");
}
}
void Exit()
{
system("cls");
printflove();
exit(0);
}
/*学生操作界面*/
void stu_menu()
{
int x=0;
printf("\t\t=====================学生查询成绩=================\n\n\n");
printf("按(1)进入查询界面,按(2)返回主菜单\n\n") ;
VVV:
scanf("%d",&x);
switch(x)
{
case 1:
system("cls");
find_stu2();
break;
case 2:
system("cls");
start_menu();
break;
default:
printf("指令错误,请重新输入\n");
goto VVV;
break;
}
}
/*学生查询函数*/
void find_stu2()
{
char c;
start:
printf(" <<<<<<<<<<<<<<<<<查找学生的成绩信息>>>>>>>>>>>>>>>>");
printf("\n\n");
one:
system("cls");
printf(">>>>>>>>>请输入要查找的学生的学号:");
int a = 0, num = 0, t = 0;
scanf("%d", &num);
for (; a < stu_number; a++)
{
if (all_stu[a].stu_id == num)
{
t = 1;
break;
}
}
if (t == 0)
{
printf("\n对不起,没有查询到该学号,请按(Y)重新输入,按(N)退出\n");
II:
c = getch();
if (c == 'y' || c == 'Y')
goto one;
else if (c == 'n' || c == 'N')
stu_menu;
else
{
printf("指令错误,请重新输入\n");
goto II;
}
}
else if (t == 1)
{
printf(">>>名字:%s\n", all_stu[a].stu_name);
printf(">>>学号:%d\n", all_stu[a].stu_id);
printf(">>>语文:%0.2lf分\n", all_stu[a].chinese);
printf(">>>数学:%0.2lf分\n", all_stu[a].math);
printf(">>>英语:%0.2lf分\n", all_stu[a].english);
printf(">>>平均分:%0.2lf分\n", all_stu[a].av_grade);
}
printf("\n");
printf("查询已完成,是否还要查询,是则按(Y),否则按(N)\n");
j:
c=getch();
if (c == 'y' || c == 'Y')
goto start;
else if (c == 'n' || c == 'N')
{
system("cls");
stu_menu();
}
else
{
printf("指令错误,请重新输入\n");
goto j;
}
}
班级成绩管理系统(非链表版)
最新推荐文章于 2023-06-15 00:13:56 发布