C语言版学生成绩管理系统
本文实例为大家分享了C语言版学生成绩管理系统的具体代码,供大家参考,具体内容如下
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<string.h>
#include<algorithm>
char buf[255];
char c=14;
char path[]="D:\\data";
char tmp[]="D:\\tmp";
struct Student
{
char Name[20];
char No[20];
char Dept[20];
char ClassNo[20];
double Math,Chinese,English;
double Ave;
bool Pass;
}students[500];
bool cmp1(Student s1,Student s2)
{
return s1.Chinese>s2.Chinese;
}
bool cmp2(Student s1,Student s2)
{
return s1.Math>s2.Math;
}
bool cmp3(Student s1,Student s2)
{
return s1.English>s2.English;
}
bool cmp4(Student s1,Student s2)
{
return s1.Ave>s2.Ave;
}
//---------------------------------------------------------void Sort(int cmd)
/*
1 ChineseSort
2 MathSort
3 EnglishSort
4 AveSort
*/
int Sort(int cmd)
{
Student stu;
FILE *fp=fopen(path,"ab+");
int n,cnt=0;
while((n=fread(&stu,sizeof(stu),1,fp)>0))
{
students[cnt++]=stu;
}
fclose(fp);
switch(cmd)
{
case 1:
std::sort(students,students+cnt,cmp1);
break;
case 2:
std::sort(students,students+cnt,cmp2);
break;
case 3:
std::sort(students,students+cnt,cmp3);
break;
case 4:
std::sort(students,students+cnt,cmp4);
break;
default:
exit(0);
}
return cnt;
}
//---------------------------------------------------------PrintTitle()
void PrintTitle()
{
printf("%-8s%-12s%-12s%-8s%-7s%-7s%-7s%-7s%-s\n","姓名","班级","学号","系院","语文","数学","英语","平均分","及格");
}
//---------------------------------------------------------Print(Student stu)
void Print(Student stu)
{
printf("%-8s%-12s%-12s%-8s%-7.2lf%-7.2lf%-7.2lf%-7.2lf%-s\n",stu.Name,stu.ClassNo,stu.No,stu.Dept,stu.Chinese,stu.Math,stu.English,stu.Ave,stu.Pass?"是":"否");
}
//---------------------------------------------------------PrintNo(char No[])
void PrintNo(char No[])
{
Student stu;
int n;
bool find=0;
FILE *fp=fopen(path,"ab+");//以ab+方式打开防止data文件不存在时发生错误
while((n=fread(&stu,sizeof(stu),1,fp))>0)
{
if(!strcmp(stu.No,No))
{
find=1;
break;
}
}
fclose(fp);
if(find)
{
PrintTitle();
Print(stu);
}
else
puts("未找到!");
}
//---------------------------------------------------------PrintClass(char ClassNo[])
void PrintClass(char ClassNo[])
{
Student stu;
FILE *fp=fopen(path,"ab+");
int n,cnt=0;
while((n=fread(&stu,sizeof(stu),1,fp)>0))
{
if(!strcmp(stu.ClassNo,ClassNo))
{
if(cnt==0)
PrintTitle();
Print(stu);
cnt++;
}
}
fclose(fp);
if(cnt==0)
puts("无数据");
else
printf("共%d条\n",cnt);
}
//---------------------------------------------------------PrintDept(char Dept[])
void PrintDept(char Dept[])
{
Student stu;
FILE *fp=fopen(path,"ab+");
int n,cnt=0;
while((n=fread(&stu,sizeof(stu),1,fp)>0))
{
if(!strcmp(stu.Dept,Dept))
{
if(cnt==0)
PrintTitle();
Print(stu);
cnt++;
}
}
fclose(fp);
if(cnt==0)
puts("无数据");
else
printf("共%d条\n",cnt);
}
//---------------------------------------------------------PrintAll()
void PrintAll()
{
Student stu;
FILE *fp=fopen(path,"ab+");
int n,cnt=0;
while((n=fread(&stu,sizeof(stu),1,fp)>0))
{
if(cnt==0)
PrintTitle();
Print(stu);
cnt++;
}
fclose(fp);
if(cnt==0)
puts("无数据");
else
printf("共%d条\n",cnt);
}
//---------------------------------------------------------Input()
Student Input()
{
Student stu;
puts("请输入姓名、班级、学号、系院:");
scanf("%s%s%s%s",&stu.Name,&stu.ClassNo,&stu.No,&stu.Dept);
puts("请输入各科成绩:语文、数学、英语");
scanf("%lf%lf%lf",&stu.Chinese,&stu.Math,&stu.English);
stu.Ave=(stu.Chinese+stu.Math+stu.English)/3;
if(stu.Chinese>=60&&stu.Math>=60&&stu.English>=60)
stu.Pass=1;
else
stu.Pass=0;
return stu;
}
//---------------------------------------------------------Insert()
void Insert()
{
int cmd;
do{
Student stu=Input();
FILE *fp=fopen(path,"ab+");
fwrite(&stu,sizeof(stu),1,fp);
fclose(fp);
puts("录入成功");
puts("继续录入吗?[1]是[0]否");
scanf("%d",&cmd);
}while(cmd==1);
}
//---------------------------------------------------------Del()
void Del()
{
int cmd,cmd2;
char No[20];
do{
puts("请输入要删除的学号:");
scanf("%s",&No);
Student stu;
FILE *fp=fopen(path,"ab+");
FILE *fp2=fopen(tmp,"wb");
int n,find=0;
while((n=fread(&stu,sizeof(stu),1,fp))>0)
{
if(!strcmp(stu.No,No))
{
find=1;
Print(stu);
puts("确定要删除吗?[1]确定[0]取消");
scanf("%d",&cmd2);
if(cmd2==1)
puts("已删除");
else
fwrite(&stu,sizeof(stu),1,fp2);
}
else
fwrite(&stu,sizeof(stu),1,fp2);
}
fclose(fp);
fclose(fp2);
if(find==0)
{
printf("未找到学号%s\n",No);
remove(tmp);
}
else
{
remove(path);
rename(tmp,path);
}
puts("继续删除吗?[1]是[0]否");
scanf("%d",&cmd);
}while(cmd==1);
}
//---------------------------------------------------------Change()
void Change()
{
int cmd;
char No[20];
do{
puts("请输入要更改的学号:");
scanf("%s",&No);
Student stu;
int find=0;
FILE *fp=fopen(path,"ab+");
FILE *fp2=fopen(tmp,"wb");
int n;
while((n=fread(&stu,sizeof(stu),1,fp))>0)
{
if(!strcmp(stu.No,No))
{
find=1;
stu=Input();
fwrite(&stu,sizeof(stu),1,fp2);
}
else
fwrite(&stu,sizeof(stu),1,fp2);
}
fclose(fp);
fclose(fp2);
if(find)
{
remove(path);
rename(tmp,path);
puts("更改成功!");
}
else
{
printf("未找到学号%s\n",No);
remove(tmp);
}
puts("继续更改吗?[1]是[0]否");
scanf("%d",&cmd);
}while(cmd==1);
}
//---------------------------------------------------------AboutCourse
void Chinese()
{
double sum=0,ave;
int pass=0,fail=0,better=0;
Student stu;
FILE *fp=fopen(path,"ab+");
int n,cnt=Sort(1);
while((n=fread(&stu,sizeof(stu),1,fp))>0)
{
sum+=stu.Chinese;
if(stu.Chinese>=60)
{
pass++;
if(stu.Chinese>=80)
better++;
}
else
fail++;
}
ave=sum/cnt;
puts("语文成绩分析……");
printf("共有%d学生\n优秀:%d人\n及格:%d人\n不及格:%d人\n及格率:%.2lf%%\n",cnt,better,pass,fail,100.0*pass/cnt);
printf("%-8s%-10s%-12s%-s\n","名次","姓名","学号","语文");
for(int i=0;i<cnt;i++)
{
printf("第%2d名 %-10s%-12s%-8.2lf\n",i+1,students[i].Name,students[i].No,students[i].Chinese);
}
printf("平均分:%.2lf\n",ave);
}
void Math()
{
double sum=0,ave;
int pass=0,fail=0,better=0;
Student stu;
FILE *fp=fopen(path,"ab+");
int n,cnt=Sort(2);
while((n=fread(&stu,sizeof(stu),1,fp))>0)
{
sum+=stu.Math;
if(stu.Math>=60)
{
pass++;
if(stu.Math>=80)
better++;
}
else
fail++;
}
ave=sum/cnt;
puts("数学成绩分析……");
printf("共有%d学生\n优秀:%d人\n及格:%d人\n不及格:%d人\n及格率:%.2lf%%\n",cnt,better,pass,fail,100.0*pass/cnt);
printf("%-8s%-10s%-12s%-s\n","名次","姓名","学号","数学");
for(int i=0;i<cnt;i++)
{
printf("第%2d名 %-10s%-12s%-8.2lf\n",i+1,students[i].Name,students[i].No,students[i].Math);
}
printf("平均分:%.2lf\n",ave);
}
void English()
{
double sum=0,ave;
int pass=0,fail=0,better=0;
Student stu;
FILE *fp=fopen(path,"ab+");
int n,cnt=Sort(3);
while((n=fread(&stu,sizeof(stu),1,fp))>0)
{
sum+=stu.English;
if(stu.English>=60)
{
pass++;
if(stu.English>=80)
better++;
}
else
fail++;
}
ave=sum/cnt;
puts("英语成绩分析……");
printf("共有%d学生\n优秀:%d人\n及格:%d人\n不及格:%d人\n及格率:%.2lf%%\n",cnt,better,pass,fail,100.0*pass/cnt);
printf("%-8s%-10s%-12s%-s\n","名次","姓名","学号","英语");
for(int i=0;i<cnt;i++)
{
printf("第%2d名 %-10s%-12s%-8.2lf\n",i+1,students[i].Name,students[i].No,students[i].English);
}
printf("平均分:%.2lf\n",ave);
}
void Average()
{
Student stu;
int n,cnt=Sort(4);
double sum=0;
FILE *fp=fopen(path,"ab+");
while((n=fread(&stu,sizeof(stu),1,fp))>0)
{
sum+=stu.Chinese;
sum+=stu.Math;
sum+=stu.English;
}
puts("平均成绩分析……");
printf("%-8s%-10s%-12s%-s\n","名次","姓名","学号","平均成绩");
for(int i=0;i<cnt;i++)
{
printf("第%2d名 %-10s%-12s%-8.2lf\n",i+1,students[i].Name,students[i].No,students[i].Ave);
}
printf("平均分:%.2lf\n",sum/cnt/3);
}
//---------------------------------------------------------Search()
void Search()
{
int cmd,cmd2;
while(1)
{
system("cls");
puts("[1] 按学号查询");
puts("[2] 按班级查询");
puts("[3] 输出全部");
puts("[4] 按系别查询");
puts("[5] 课程分析");
puts("[0] 返回主界面");
scanf("%d",&cmd);
if(cmd==0)
break;
switch(cmd)
{
case 1:
{
char No[20];
puts("请输入要查询的学号:");
scanf("%s",&No);
PrintNo(No);
}
break;
case 2:
{
char ClassNo[20];
puts("请输入要查询班级号:");
scanf("%s",&ClassNo);
PrintClass(ClassNo);
}
break;
case 3:
PrintAll();
break;
case 4:
{
char Dept[20];
puts("请输入要查询的系别:");
scanf("%s",&Dept);
PrintDept(Dept);
}
break;
case 5:
while(1)
{
puts("[1] 语文成绩分析");
puts("[2] 数学成绩分析");
puts("[3] 英语成绩分析");
puts("[4] 平均成绩分析");
puts("[0] 返回上一级");
scanf("%d",&cmd2);
if(cmd2==0)
break;
switch(cmd2)
{
case 1:
Chinese();
break;
case 2:
Math();
break;
case 3:
English();
break;
case 4:
Average();
break;
default:
puts("不合法指令!");
}
}
break;
default:
puts("不合法指令!");
}
while(1)
{
puts("[0] 返回");
puts("[1] 录入");
puts("[2] 删除");
puts("[3] 修改");
scanf("%d",&cmd2);
if(cmd2==0)
break;
switch(cmd2)
{
case 1:
Insert();
break;
case 2:
Del();
break;
case 3:
Change();
break;
default:
puts("不合法指令!");
}
}
}
}
//---------------------------------------------------------Menu()
void ShowMenu1()
{
int i;
puts("");
printf(" ");
for(i=0;i<14;i++)
putchar(c);
printf("成绩管理分析系统");
for(i=0;i<14;i++)
putchar(c);
puts("");
printf(" %c %c\n",c,c);
printf(" %c 请输入指令 %c\n",c,c);
printf(" %c %c\n",c,c);
printf(" %c [0] 退出 %c\n",c,c);
printf(" %c %c\n",c,c);
printf(" %c [1] 录入 %c\n",c,c);
printf(" %c %c\n",c,c);
printf(" %c [2] 删除 %c\n",c,c);
printf(" %c %c\n",c,c);
printf(" %c [3] 查询 %c\n",c,c);
printf(" %c %c\n",c,c);
printf(" %c [4] 更改 %c\n",c,c);
printf(" %c %c\n",c,c);
printf(" ");
for(i=0;i<44;i++)
putchar(c);
puts("");
}
void ShowMenu2()
{
int i;
puts("");
printf(" ");
for(i=0;i<14;i++)
putchar(c);
printf("成绩管理分析系统");
for(i=0;i<14;i++)
putchar(c);
puts("");
printf(" %c %c\n",c,c);
printf(" %c 请输入指令 %c\n",c,c);
printf(" %c %c\n",c,c);
printf(" %c [0] 退出 %c\n",c,c);
printf(" %c %c\n",c,c);
printf(" %c [1] 录入 %c\n",c,c);
printf(" %c %c\n",c,c);
printf(" %c [2] 删除 %c\n",c,c);
printf(" %c %c\n",c,c);
printf(" %c [3] 查询 %c\n",c,c);
printf(" %c %c\n",c,c);
printf(" %c [4] 更改 %c\n",c,c);
printf(" %c %c\n",c,c);
printf(" ");
for(i=0;i<44;i++)
putchar(c);
puts("");
}
//---------------------------------------------------------void RandColor()
void RandColor()
{
int a,b;
char param[20]="color ",param2[10];
do{
a=rand()%16;
b=rand()%16;
}while(a==b);
sprintf(param2,"%x%x",a,b);
strcat(param,param2);
system(param);
}
//---------------------------------------------------------main()
int main()
{
int cmd;
srand(time(NULL));
for(int i=0;i<8;i++)
{
RandColor();
system("cls");
ShowMenu1();
system("cls");
ShowMenu2();
}
system("color 0a");
while(1)
{
system("cls");
ShowMenu1();
printf("[ ]\b\b");
scanf("%d",&cmd);
if(cmd==0)
break;
switch(cmd)
{
case 1:
Insert();
break;
case 2:
Del();
break;
case 3:
Search();
break;
case 4:
Change();
break;
default:
puts("命令不合法!");
}
}
return 0;
}
以上是 C语言版学生成绩管理系统 的全部内容, 来源链接: utcz.com/z/360226.html