#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<conio.h>
#define H "--------------------------------------------------------------------------\n"
#define H0 "------------------------------------------------------------------------------\n"
#define H1 "| 学号 | 姓名 | 系别 | 班级 |语文 |数学 | 英语 | 总成绩 |\n"
#define H2 "|-----------|--------|-------|----------|-------|-------|-------|--------|\n"
#define H3 "|------------------|------------------|-------------------|-----------------|\n"
#define H4 "| 学号 | 姓名 | 系别 | 班级 |语文 |数学 | 英语 | 总成绩 |名次\n"
#define H5 "|-----------|--------|-------|----------|-------|-------|-------|--------|----\n"
struct subject
{
double math;
double english;
double chinese;
};
struct student
{
char name[20];//姓名
char num[20]; //学号
char cla[20]; //班级
char dept[20]; //系别
struct subject sub; //课程
double sum; //总成绩
}students[500];
bool cmp1(student s1,student s2) //语文成绩比较
{
return s1.sub.chinese>s2.sub.chinese;
}
bool cmp2(student s1,student s2)//数学成绩比较
{
return s1.sub.math>s2.sub.math;
}
bool cmp3(student s1,student s2) //英语成绩比较
{
return s1.sub.english>s2.sub.english;
}
bool cmp4(student s1,student s2) //总成绩比较
{
return s1.sum>s2.sum;
}
int Sort(int cmd) //成绩比较函数
{
student stu;
FILE *fp=fopen("D:\\inform.txt","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;
}
char menu()//菜单选择函数
{
char n;
system("CLS");//清屏
puts("\n");
puts("\t\t O(∩_∩)O~~ 欢迎来到学生成绩管理分析系统\n");
puts("\t\t╔*☆***◎***◇***☆*** 菜单 ***☆***◇***◎***☆*╗\n");
puts("\t\t|☆|\t\t 1.学生信息录入\t\t\t|☆|\n");
puts("\t\t|★|\t\t 2.学生信息显示\t\t\t|★|\n");
puts("\t\t|☆|\t\t 3.学生信息查询\t\t\t|☆|\n");
puts("\t\t|★|\t\t 4.学生信息修改\t\t\t|★|\n");
puts("\t\t|☆|\t\t 5.学生信息删除\t\t\t|☆|\n");
puts("\t\t|★|\t\t 6.学生成绩分析\t\t\t|★|\n");
puts("\t\t|☆|\t\t 7.退出\t\t\t\t|☆|\n");
puts("\t\t╚*☆***◎***◇****☆****★****☆****◇***◎***☆*╝\n");
printf("\t请选择您要实现的功能选项 (1-7): [ ]\b\b");
while(1)
{
n=getchar();getchar();
if(n<'1'||n>'7')
printf("输入错误,请您重新输入选项(1-7):[ ]\b\b");
else
break;
}
return n;
}
void Append()//信息输入函数
{
struct student stu;
FILE * fp;
char ch;
if((fp=fopen("D:\\inform.txt","ab"))==NULL)
{
printf("\tCan not open the inform file!");
getch();
exit(1);
}
do
{
printf("\t请输入学号:");gets(stu.num);
printf("\t请输入姓名:");gets(stu.name);
printf("\t请输入系院:");gets(stu.dept);
printf("\t请输入班级:");gets(stu.cla);
printf("\t请按顺序输入学生的语文成绩,数学成绩和英语成绩:");
scanf("%lf%lf%lf",&stu.sub.chinese,&stu.sub.math,&stu.sub.english);
stu.sum=stu.sub.chinese+stu.sub.math+stu.sub.english;
fwrite(&stu,sizeof(stu),1,fp);
printf("\t是否继续添加?(Y/N):[ ]\b\b");
getchar();
ch=getchar();
getchar();
}while(ch=='Y'||ch=='y');
fclose(fp);
}
void print1() //输出函数
{
printf(H);
printf(H1);
printf(H2);
}
void print2(struct student stu) //输出函数
{
printf("|%-11s|%-8s|%-7s|%-10s|%-7.2lf|%-7.2lf|%-7.2lf|%-8.2lf|\n",stu.num,stu.name,stu.dept,stu.cla,stu.sub.chinese,stu.sub.math,stu.sub.english,stu.sum);
printf(H2);
}
void Display() //信息显示函数
{
struct student stu;
FILE *fp;
int total=0;
if((fp=fopen("D:\\inform.txt","rb"))==NULL)
{
printf("\tCan not open the inform file!");
getch();
exit(1);
}
while(fread(&stu,sizeof(stu),1,fp)==1)
{
total++;
if(total==1)print1();
print2(stu);
}
fclose(fp);
printf("\n\n\tThere are %d record in all!",total);
printf("\n\n\tPress any key to continue......");
getch();
}
void Search() //信息查询函数
{
struct student stu;
FILE *fp;
int flag;
int total=0;
char ch[10];
char f;
if((fp=fopen("D:\\inform.txt","rb"))==NULL)
{
printf("\tCan not open the inform file!");
getch();
exit(1);
}
do
{
rewind(fp);
printf("\n\n\t1:按学号查询\n\t2:按姓名查询\n\t3:按系院查询\n\t4:按班级查询\n\t\t\t[ ]\b\b");
while(1)
{
scanf("%d",&flag);getchar();
if(flag<1||flag>4)
{
printf("输入错误,请您重新输入\n");
printf("\n1:按学号查询\n 2:按姓名查询\n 3:按系院查询\n 4:按班级查询\n):[ ]\b\b");
}
else
break;
}
if(flag==1)
{ printf("请输入您想要查询的学号:");
gets(ch);
total=0;
while(fread(&stu,sizeof(stu),1,fp)==1)
if(strcmp(ch,stu.num)==0)
{
total++;
if(total==1)print1();
print2(stu);
}
}
if(flag==2)
{
printf("请输入您想要查询的姓名:");
gets(ch);
total=0;
while(fread(&stu,sizeof(stu),1,fp)==1)
if(strcmp(ch,stu.name)==0)
{
total++;
if(total==1)print1();
print2(stu);
}
}
if(flag==3)
{
printf("请输入您想要查询的系院:");
gets(ch);
total=0;
while(fread(&stu,sizeof(stu),1,fp)==1)
if(strcmp(ch,stu.dept)==0)
{
total++;
if(total==1)print1();
print2(stu);
}
}
if(flag==4)
{
printf("请输入您想要查询的班级:");
gets(ch);
total=0;
while(fread(&stu,sizeof(stu),1,fp)==1)
if(strcmp(ch,stu.cla)==0)
{
total++;
if(total==1)print1();
print2(stu);
}
}
printf("\n\n\tThere are %d record include!\n",total);
printf("Search any more?(Y/N):[ ]\b\b");
f=getchar();getchar();
}while(f=='Y'||f=='y');
fclose(fp);
}
void Modify() //信息修改函数
{
struct student stu;
FILE *fp1,*fp2;
int flag;
char ch[15];
char f;
do
{
if((fp1=fopen("D://inform.txt","rb"))==NULL)
{
printf("\tCan not open the inform file!");
getch();
exit(1);
}
if((fp2=fopen("D://temp.txt","wb"))==NULL)
{
printf("\tCan not creat the temp file!");
getch();
exit(1);
}
printf("请输入您想要修改信息的学号:");
gets(ch);
flag=0;
while(fread(&stu,sizeof(stu),1,fp1)==1)
{
if(strcmp(ch,stu.num)==0)
{
print1();
print2(stu);
printf("\n\n请输入新的信息:\n");
printf("\t学号:");gets(stu.num);
printf("\t姓名:");gets(stu.name);
printf("\t系别:");gets(stu.dept);
printf("\t班级:");gets(stu.cla);
printf("\t请按顺序输入语文成绩、数学成绩、英语成绩:");
scanf("%lf%lf%lf",&stu.sub.chinese,&stu.sub.math,&stu.sub.english);
stu.sum=stu.sub.chinese+stu.sub.math+stu.sub.english;
getchar();
flag=1;
}
fwrite(&stu,sizeof(stu),1,fp2);
}
fclose(fp1);
fclose(fp2);
if(flag==1)
{
printf("修改成功!\n");
remove("D://inform.txt");
rename("D://temp.txt","D://inform.txt");
}
else
printf("对不起 %>_<%,找不到这项纪录!\n");
printf("是否要修改更多的信息?(Y/N):[ ]\b\b");
f=getchar(); getchar();
}while(f=='Y'||f=='y');
}
void Delete() //信息删除函数
{
struct student stu;
FILE *fp1,*fp2;
int flag;
char ch[10];
char f;
do
{
if((fp1=fopen("D://inform.txt","rb"))==NULL)
{
printf("\tCan not open the inform file!");
getchar();
exit(1);
}
if((fp2=fopen("D://temp.txt","wb"))==NULL)
{
printf("\tCan not creat the temp file!");
getch();
exit(1);
}
printf("请输入您想要删除信息的学号:");
gets(ch);
flag=0;
while(fread(&stu,sizeof(stu),1,fp1)==1)
{
if(strcmp(ch,stu.num)==0)
{
print1();
print2(stu);
flag=1;
}
else
fwrite(&stu,sizeof(stu),1,fp2);
}
fclose(fp1);
fclose(fp2);
if(flag==1)
{
printf("删除成功!\n");
remove("D://inform.txt");
rename("D://temp.txt","D://inform.txt");
}
else
printf("Can not find this record!\n");
printf("Delete any more?(Y/N):[ ]\b\b");
f=getchar();
getchar();
}while(f=='Y'||f=='y');
}
void Analyse() //信息分析函数
{
struct student stu;
FILE *fp;
int flag,flag1;
int total=0;
int pass=0,fail=0,better=0;
char ch[10];
char f;
if((fp=fopen("D://inform.txt","rb"))==NULL)
{
printf("\tCan not open the inform file!");
getch();
exit(1);
}
do
{
rewind(fp);
printf("\n\n\t1:课程成绩分析\n\t2:班级成绩分析\n\t3:院系成绩分析\n\t\t请选择:[ ]\b\b");
while(1)
{
scanf("%d",&flag);getchar();
if(flag<1||flag>3)
{
printf("输入错误,请您重新输入\n");
printf("\n\n\t1:课程成绩分析\n\t2:班级成绩分析\n\t3:院系成绩分析\n\t\t请选择:[ ]\b\b");
}
else
break;
}
if(flag==1)
{
printf("\n\n\t1:语文成绩分析\n\t2:数学成绩分析\n\t3:英语成绩分析\n\t4:总成绩排名\n");
printf("\t\t请选择:[ ]\b\b");
scanf("%d",&flag1);getchar();
if(flag1<1||flag1>4)
{
printf("\n\t\t输入错误,请您重新输入\n");
printf("\n\n\t1:语文成绩分析\n\t2:数学成绩分析\n\t3:英语成绩分析\n\t4:总成绩排名\n\t\t\t[ ]\b\b");
}
if(flag1==1)
{
total=0; //总人数
pass=0; //及格人数
fail=0; //不及格人数
better=0; //优秀人数
int cnt=Sort(1);
while(fread(&stu,sizeof(stu),1,fp)==1)
{
total+=stu.sub.chinese;
if(stu.sub.chinese>=60)
{
pass++;
if(stu.sub.chinese>=80)
better++;
}
else
fail++;
}
double ave=total/cnt;
puts("语文成绩成绩分析:");
printf("共有%d学生\n优秀:%d人\n及格:%d人\n不及格:%d人\n及格率:%.2lf%%\n",cnt,better,pass,fail,pass*100.0/cnt);
printf(H3);
printf(" %s %s %s %s\n","名次","姓名","学号","语文成绩");
printf(H3);
for(int i=0;i<cnt;i++)
{
printf(" 第%2d名\t\t %-17s%-23s%-8.2lf\n",i+1,students[i].name,students[i].num,students[i].sub.chinese);
printf(H3);
}
printf("平均分:%.2lf\n",ave);
}
if(flag1==2)
{
total=0;pass=0;fail=0;better=0;
int cnt=Sort(2);
while(fread(&stu,sizeof(stu),1,fp)==1)
{
total+=stu.sub.math;
if(stu.sub.math>=60)
{
pass++;
if(stu.sub.math>=80)
better++;
}
else
fail++;
}
double ave=total/cnt;
puts("数学成绩成绩分析:");
printf("共有%d学生\n优秀:%d人\n及格:%d人\n不及格:%d人\n及格率:%.2lf%%\n",cnt,better,pass,fail,pass*100.0/cnt);
printf(H3);
printf(" %s %s %s %s\n","名次","姓名","学号","数学成绩");
printf(H3);
for(int i=0;i<cnt;i++)
{
printf(" 第%2d名\t\t %-17s%-23s%-8.2lf\n",i+1,students[i].name,students[i].num,students[i].sub.math);
printf(H3);
}
printf("平均分:%.2lf\n",ave);
}
if(flag1==3)
{
total=0;pass=0;fail=0;better=0;
int cnt=Sort(3);
while(fread(&stu,sizeof(stu),1,fp)==1)
{
total+=stu.sub.english;
if(stu.sub.english>=60)
{
pass++;
if(stu.sub.english>=80)
better++;
}
else
fail++;
}
double ave=total/cnt;
puts("英语成绩成绩分析:");
printf("共有%d学生\n优秀:%d人\n及格:%d人\n不及格:%d人\n及格率:%.2lf%%\n",cnt,better,pass,fail,pass*100.0/cnt);
printf(H3);
printf(" %s %s %s %s\n","名次","姓名","学号","英语成绩");
printf(H3);
for(int i=0;i<cnt;i++)
{
printf(" 第%2d名\t\t %-17s%-23s%-8.2lf\n",i+1,students[i].name,students[i].num,students[i].sub.english);
printf(H3);
}
printf("平均分:%.2lf\n",ave);
}
if(flag1==4)
{
total=0;int cnt=Sort(4);
puts("总成绩排名:");
printf(H0);printf(H4);printf(H5);
for(int i=0;i<cnt;i++)
{
printf("|%-11s|%-8s|%-7s|%-10s|%-7.2lf|%-7.2lf|%-7.2lf|%-8.2lf| %d\n",students[i].num,students[i].name,students[i].dept,students[1].cla,students[i].sub.chinese,students[i].sub.math,students[i].sub.english,students[i].sum,i+1);
printf(H5);
}
}
}
if(flag==2)
{
int yu1,sh1,yi1,yu2,sh2,yi2;
printf("请输入您想要查询的班级:");
gets(ch);//printf("%s\n",ch);system("pause");
int count=0;yu1=0;yu2=0;sh1=0;sh2=0;yi1=0;yi2=0;
double yu=0,sh=0,yi=0;
while(fread(&stu,sizeof(stu),1,fp)==1)
if(strcmp(ch,stu.cla)==0)
{
count++;
yu+=stu.sub.chinese;
if(stu.sub.chinese>=60)
{
yu1++;
if(stu.sub.chinese>=80)
yu2++;
}
sh+=stu.sub.math;
if(stu.sub.math>=60)
{
sh1++;
if(stu.sub.math>=80)
sh2++;
}
yi+=stu.sub.english;
if(stu.sub.english>=60)
{
yi1++;
if(stu.sub.english>=80)
yi2++;
}
}
printf(H3);
printf(" %s班的语文 平均分: %.2lf 及格率: %.2lf%% 优秀率:%.2lf%%\n",ch,yu/count,yu1*100.0/count,yu2*100.0/count);
printf(H3);
printf(" %s班的高数 平均分: %.2lf 及格率: %.2lf%% 优秀率:%.2lf%%\n",ch,sh/count,sh1*100.0/count,sh2*100.0/count);
printf(H3);
printf(" %s班的英语 平均分: %.2lf 及格率: %.2lf%% 优秀率:%.2lf%%\n",ch,yi/count,yi1*100.0/count,yi2*100.0/count);
printf(H3);
}
if(flag==3)
{int yu1,sh1,yi1,yu2,sh2,yi2;
printf("请输入您想要查询的系别:");
gets(ch);
int count=0;yu1=0;yu2=0;sh1=0;sh2=0;yi1=0;yi2=0;
double yu=0,sh=0,yi=0;
while(fread(&stu,sizeof(stu),1,fp)==1)
if(strcmp(ch,stu.dept)==0)
{
count++;
yu+=stu.sub.chinese;
if(stu.sub.chinese>=60)
{
yu1++;
if(stu.sub.chinese>=80)
yu2++;
}
sh+=stu.sub.math;
if(stu.sub.math>=60)
{
sh1++;
if(stu.sub.math>=80)
sh2++;
}
yi+=stu.sub.english;
if(stu.sub.english>=60)
{
yi1++;
if(stu.sub.english>=80)
yi2++;
}
}
printf(H3);
printf(" %s系的语文 平均分: %.2lf 及格率: %.2lf%% 优秀率:%.2lf%%\n",ch,yu/count,yu1*100.0/count,yu2*100.0/count);
printf(H3);
printf(" %s系的高数 平均分: %.2lf 及格率: %.2lf%% 优秀率:%.2lf%%\n",ch,sh/count,sh1*100.0/count,sh2*100.0/count);
printf(H3);
printf(" %s系的英语 平均分: %.2lf 及格率: %.2lf%% 优秀率:%.2lf%%\n",ch,yi/count,yi1*100.0/count,yi2*100.0/count);
printf(H3);
}
printf("\nSearch any more?(Y/N):[ ]\b\b");
f=getchar();getchar();
}while(f=='Y'||f=='y');
fclose(fp);
}
int main()
{
system("color 0b");
while(1)
{
switch(menu())
{
case '1':Append();break;
case '2':Display();break;
case '3':Search();break;
case '4':Modify();break;
case '5':Delete();break;
case '6':Analyse();break;
case '7':exit(0);break;
}
}
return 0;
}
C语言实现课程设计
最新推荐文章于 2023-01-07 12:47:36 发布