有点错误,麻烦大家帮忙解决下,谢谢!
#include<stdio.h>#include<string.h>
#define MAX 2
#define PAGE 4
#define PRINT1 printf("************************************************************\n")
#define PRINT2 printf("学号--姓名--性别--年级--系别--专业--班级--数学--英语--C语言--物理--总分--平均分\n")
#define PRINT3 printf("%2d%6s%7s%6d%8s%6s%5d",s[i].num,s[i].name,s[i].sex,s[i].grade,s[i].xibie,s[i].zhuanye,s[i].clas)
#define PRINT4 printf("%6d%6d%6d%7d%6d%7d",s[i].sco.m, s[i].sco.eng,s[i].sco.C,s[i].sco.phy,s[i].sco.totel,s[i].sco.ave)
int sum; /*用来记录学生个数*/
TOTEL(int x,int y,int p,int q)
{ int m;
m=x+y+p+q;
return(m);
}
AVE(int x,int y,int p,int q)
{ int m;
m=(x+y+p+q)/4;
return(m);
}
struct score /*成绩信息结构体*/
{ int m;
int eng;
int C;
int phy;
int mao;
int totel;
int ave;
} ;
/*学生信息结构体*/
struct student
{int num;
char name[10];
char sex[6];
int grade;
char xibie[30];
char zhuanye[30];
int clas;
struct score sco;
}s[35];
/*保存信息*/
save_message(int sum)
{ FILE *fp;
int i;
if ((fp=fopen("student information.txt","wb"))==NULL)
{ printf("读文件错误!\n");
return ;
}
for(i=0;i<sum;i++)
if(fwrite(&s[i],sizeof(struct student),1,fp)!=1)
printf("写文件错误!\n");
fclose(fp);
}
/*输入模块*/
input_message()
{ int i=0,flag;
int m=MAX;
int a,b,c,d;
char choice;
clrscr(); /*清屏*/
printf("\t** 录入学生信息(最多%d个) **\n",m);
printf("***************************************************\n");
do
{ printf(" 第%d个学生",i+1);
printf("\n 学号:");
scanf("%d",&s[i].num);
printf("\n 姓名:");
scanf("%s",s[i].name);
printf("\n 性别:");
scanf("%s",s[i].sex);
printf("\n 年级:");
scanf("%d",&s[i].grade);
printf("\n 系别:");
scanf("%s",s[i].xibie);
printf("\n 专业:");
scanf("%s",s[i].zhuanye);
printf("\n 班级:");
scanf("%d",&s[i].clas);
printf("\n 数学:");
scanf("%d",&s[i].sco.m);
a=s[i].sco.m;
printf("\n 英语:");
scanf("%d",&s[i].sco.eng);
b=s[i].sco.eng;
printf("\n C语言:");
scanf("%d",&s[i].sco.C);
c=s[i].sco.C;
printf("\n 物理:");
scanf("%d",&s[i].sco.phy);
d=s[i].sco.phy;
printf("\n 总分:");
s[i].sco.totel=TOTEL(a,b,c,d);
printf("%d",s[i].sco.totel);
printf("\n 平均分:");
s[i].sco.ave=AVE(a,b,c,d);
printf("%d",s[i].sco.ave);
i++;
printf("\n");
} while(i<MAX) ;
sum=i;
printf("\n --%d学生信息输入完毕!--\n");
printf("\n 按任意键返回主菜单!");
bioskey(0);
}
/*读取学生信息*/
int read_message()
{ FILE *fp;
int i=0;
if((fp=fopen("stuent information.txt","rb"))==NULL)
{ printf("\n\n *****暂时还没有任何学生信息,按任意键进入主菜单选择基本信息的录入!******\n");
return;
}
while(!feof(fp))
{ fread(&s[i],sizeof(struct student),1,fp);
if(s[i].num==0) break;
else i++;
}
fclose(fp);
return(i);
}
/*查询学生信息*/
/*按姓名查询*/
inquire_nam()
{ int i,j=0;
char inquire_name[15];
clrscr();
printf("\n 输入要查询的学生姓名:");
scanf("%s",inquire_name);
for(i=0;i<sum;i++)
if (strcmp(s[i].name,inquire_name)==0&&s[i].num!=0)
{ printf("\n ****学生成绩信息**** \n");
PRINT1;
PRINT2;
PRINT3;
PRINT4;
}
else j++;
if(j==sum)
printf("\n ***对*不*起*该*学*生*不*存*在*** ");
bioskey(0);
}
/*按学号查询*/
inquire_number()
{int i,j=0;
int inquire_num;
clrscr();
printf("\n 输入要查询的学生学号:");
scanf("%d",&inquire_num);
for(i=0;i<sum;i++)
if (s[i].num==inquire_num)
{ printf("\n ****学生成绩信息**** \n");
PRINT1;
PRINT2;
PRINT3;
PRINT4;
}
else j++;
if(j==sum) printf("\n ***对*不*起*该*学*生*不*存*在*** ");
bioskey(0);
}
/*查询学生信息*/
inquire_message()
{ int choice;
do
{ clrscr();
printf("\n\n ******查询学生信息****** \n\n");
printf(" 1、按学生姓名查询\n");
printf(" 2、按学生学号查询\n");
printf(" 0、返回主菜单 \n");
printf(" 请选择(0-2): \n");
scanf("%d",&choice);
switch(choice)
{ case 1: inquire_nam(); break;
case 2: inquire_number(); break;
case 0: break;
}
}while(choice!=0);
}
/*输出模块*/
output_message()
{ int i=0,page=1,j=0;
clrscr();
printf("\n\n --学生成绩信息表-- 第%d页\n\n",page);
PRINT2;
PRINT1;
do
{ if(s[i].num!=0)
{ j++;
if(j%PAGE!=0)
{PRINT3;
PRINT4;
PRINT1;
}
else
{PRINT3;
PRINT4;
PRINT1;
if(s[i+1].num!=0)
{ printf("按任意键继续");
bioskey(0);
clrscr();
printf("\n\n --学生成绩信息表-- 第%d页\n\n",++page);
PRINT2;
PRINT1;
}
}
}
i++;
}while(s[i].num!=0);
printf("按任意键返回主菜单!");
bioskey(0);
}
/*添加学生信息*/
insert_message()
{ int i=sum,j;
int a,b,c,d;
char choice;
clrscr();
printf("\t** 添加一个学生信息 **\n");
printf("**--**--**--**--**--**--**--**--**--**\n");
do
{ s[i].num=++sum;
printf("\n 学号:");
scanf("%d",&s[i].num);
printf("\n 姓名:");
scanf("%s",s[i].name);
printf("\n 性别:");
scanf("%s", s[i].sex);
printf("\n 年级:");
scanf("%d", &s[i].grade);
printf("\n 系别:");
scanf("%s", &s[i].xibie);
printf("\n 专业:");
scanf("%s", &s[i].zhuanye);
printf("\n 班级:");
scanf("%d", &s[i].clas);
printf("\n 数学:");
scanf("%d", &s[i].sco.m);
a=s[i].sco.m;
printf("\n 英语:");
scanf("%d", &s[i].sco.eng);
b=s[i].sco.eng;
printf("\n C语言:");
scanf("%d", &s[i].sco.C);
c=s[i].sco.C;
printf("\n 物理:");
scanf("%d", &s[i].sco.phy);
d=s[i].sco.phy;
printf("\n 总分:");
s[i].sco.totel=TOTEL(a,b,c,d);
printf("%d",s[i].sco.totel);
printf("\n 平均分:");
s[i].sco.ave=AVE(a,b,c,d);
printf("%d",s[i].sco.ave);
printf("\n 要继续吗?(Y/N)");
getchar();
scanf("%c",&choice);
}while (choice=='y'||choice=='Y');
printf("\n 按任意键返回主菜单!");
bioskey(0);
}
/*按姓名删除*/
del_nam()
{ int i,j;
char del_nam[10];
char choice;
do
{ clrscr();
printf("\n 输入要删除的学生姓名:");
scanf("%s",&del_nam);
for(i=0;i<sum;i++)
if(strcmp(s[i].name,del_nam)==0&&s[i].name!=0)
{ for(j=i;j<sum;j++)
s[j]=s[j+1];
printf("\n 删除成功!");
break;
}
if(i==sum)
{ printf("\n 该学生不存在");
bioskey(0);
}
printf("\n\n 要继续修改吗?(Y/N)");
getchar();
scanf("%c",&choice);
}while(choice=='y'||choice=='Y');
printf("\n 按任意键返回主菜单!");
bioskey(0);
}
/*按学号删除*/
del_number()
{ int i,j,flag;
char del_num;
char choice;
do
{ clrscr();
printf("\n 输入要删除的学生学号:");
scanf("%d",&del_num);
for(i=0;i<sum;i++)
if(s[i].num==del_num)
{ for(j=i;j<sum;j++)
s[j]=s[j+1];
printf("\n 删除成功!");
break;
}
if(i==sum)
{ printf("\n 该学生不存在");
bioskey(0);
}
printf("\n\n 要继续修改吗?(Y/N)");
getchar();
scanf("%c",&choice);
}while(choice=='y'||choice=='Y');
printf("\n 按任意键返回主菜单!");
bioskey(0);
}
/*删除学生信息*/
del_message()
{ int choice;
do
{ clrscr();
printf("\n\n ******删除学生信息****** \n\n");
printf(" 1、按学生姓名删除\n");
printf(" 2、按学生学号删除\n");
printf(" 0、返回主菜单 \n");
printf(" 请选择(0-2): \n");
scanf("%d",&choice);
switch(choice)
{ case 1: del_nam(); break;
case 2: del_number(); break;
case 0: break;
}
}while(choice!=0);
}
/*修改学生信息*/
revise_message()
{ int i=0;
char choice;
char revise_name[10];
do
{ clrscr();
printf("\n 输入要修改的学生的姓名:");
scanf("%s",&revise_name);
for (i=0;i<sum;i++)
if(strcmp(s[i].name,revise_name)==0&&s[i].name!=0)
{ printf("\n --学生成绩信息--\n");
PRINT1;
PRINT2;
PRINT3;
PRINT4;
printf("\n 您要修改哪一项?\n");
printf("\n 1、数学\n");
printf("\n 2、英语\n");
printf("\n 3、C语言\n");
printf("\n 4、物理\n");
printf("\n 请选择(1-4):");
scanf("%d",&choice);
switch(choice)
{ case 1: printf("\n 输入修改后的数学成绩:");
scanf("%d", &s[i].sco.m); break;
case 2: printf("\n 输入修改后的英语成绩:");
scanf("%d", &s[i].sco.eng);break;
case 3: printf("\n 输入修改后的C语言成绩:");
scanf("%d", &s[i].sco.C);break;
case 4: printf("\n 输入修改后的物理成绩:");
scanf("%d", &s[i].sco.phy);break;
}
PRINT1;
PRINT2;
PRINT3;
PRINT4;
break;
}
if (i==sum)
{ printf("\n 该学生不存在!");
bioskey(0);
}
printf("\n 要继续修改吗?(Y/N)");
getchar();
scanf("%c",&choice);
}while(choice=='y'||choice=='Y');
printf("\n 按任意键返回主菜单!");
bioskey(0);
}
/*按总分排名次*/
order_totel()
{ struct student *p,*q[MAX];
int i,j,t;
clrscr();
printf("\n\n 总分成绩名次 \n");
printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
printf("\n 学号--姓名--性别--年级--系别--专业--班级--数学--英语--C语言--物理--总分--平均分\n ");
p=s;
for(i=0;i<sum;i++)
q[i]=p++;
for(i=0;i<sum;i++)
{ t=i;
for(j=i+1;j<sum;j++)
if(q[t]->sco.totel<q[j]->sco.totel) t=j;
if(t!=i)
{ p=q[i];q[i]=q[t];q[t]=p;}
}
for(i=0;i<sum;i++)
{ printf("%5d%8s%8s%8d%8s%8s%8d",q[i]->num,q[i]->name,q[i]->sex,q[i]->grade,q[i]->xibie,q[i]->zhuanye,q[i]->clas);
printf("%4d%4d%4d%4d%4d%4d",q[i]->sco.m, q[i]->sco.eng,q[i]->sco.C,q[i]->sco.phy,q[i]->sco.totel,q[i]->sco.ave);
}
bioskey(0);
}
/*按平均分排名次*/
order_average()
{ struct student *p,*q[MAX];
int i,j,t;
clrscr();
printf("\n\n 平均分成绩名次 \n");
printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
printf("\n 学号--姓名--性别--年级--系别--专业--班级--数学--英语--C语言--物理--总分--平均分\n ");
p=s;
for(i=0;i<sum;i++)
q[i]=p++;for(i=0;i<sum;i++)
{ t=i;
for(j=i+1;j<sum;j++)
if(q[t]->sco.ave<q[j]->sco.ave) t=j;
if(t!=i)
{ p=q[i];q[i]=q[t];q[t]=p;
}
}
for(i=0;i<sum;i++)
{printf("%5d%8s%8s%8d%8s%8s%8d",q[i]->num,q[i]->name,q[i]->sex,q[i]->grade,q[i]->xibie,q[i]->zhuanye,q[i]->clas);
printf("%4d%4d%4d%4d%4d%4d",q[i]->sco.m, q[i]->sco.eng,q[i]->sco.C,q[i]->sco.phy,q[i]->sco.totel,q[i]->sco.ave);
printf("\n");
}
bioskey(0);
}
/*成绩名次信息*/
order_message()
{ int choice;
do
{ clrscr();
printf("\n\n ******成绩名次信息****** \n\n");
printf(" 1、按总分排名次\n");
printf(" 2、按平均分排名次\n");
printf(" 0、返回主菜单 \n");
printf(" 请选择(0-2): \n");
scanf("%d",&choice);
switch(choice)
{ case 1: order_totel(); break;
case 2: order_average(); break;
case 0: break;
}
}while(choice!=0);
}
/*未及格学生信息*/
ave_message()
{ int m=60;
int i=0;
do
{ clrscr();
printf(" 未及格的学生信息!\n");
PRINT1;
PRINT2;
for(i=0;i<sum;i++)
{if(s[i].sco.ave<m)
{ PRINT3;
PRINT4;
printf("\n");
}
else continue;
}
i++;
} while(i<MAX);
printf(" 按任意键返回主菜单!\n");
bioskey(0);
}
/*数学成绩未及格学生信息*/
math_message()
{ int m=60;
int i=0;
do
{ clrscr();
printf(" 未及格的学生信息!\n");
PRINT1;
PRINT2;
for(i=0;i<sum;i++)
{ if(s[i].sco.m<m)
{
PRINT3;
PRINT4;
printf("\n");
}
else continue;
}
i++;
} while(i<MAX);
printf(" 按任意键返回主菜单!\n");
bioskey(0);
}
/*英语成绩未及格学生信息*/
english_message()
{ int m=60;
int i=0;
do
{ clrscr();
printf(" 未及格的学生信息!\n");
PRINT1;
PRINT2;
for(i=0;i<sum;i++)
{ if(s[i].sco.eng<m)
{
PRINT3;
PRINT4;
printf("\n");
}
else continue;
}
i++;
} while(i<MAX);
printf(" 按任意键返回主菜单!\n");
bioskey(0);
}
/*C语言成绩未及格学生信息*/
Cprogramming_message()
{ int m=60;
int i=0;
do
{ clrscr();
printf(" 未及格的学生信息!\n");
PRINT1;
PRINT2;
for(i=0;i<sum;i++)
{ if(s[i].sco.C<m)
{
PRINT3;
PRINT4;
printf("\n");
}
else continue;
}
i++;
} while(i<MAX);
printf(" 按任意键返回主菜单!\n");
bioskey(0);
}
/*物理成绩未及格学生信息*/
physic_message()
{ int m=60;
int i=0;
do
{ clrscr();
printf(" 未及格的学生信息!\n");
PRINT1;
PRINT2;
for(i=0;i<sum;i++)
{ if(s[i].sco.phy<m)
{
PRINT3;
PRINT4;
printf("\n");
}
else continue;
}
i++;
} while(i<MAX);
printf(" 按任意键返回主菜单!\n");
bioskey(0);
}
/*统计模块*/
tongji_message()
{ int choice;
do
{ clrscr();
printf("******** 1、未及格的学生信息 ********\n");
printf("******** 2、数学未及格的学生信息 ********\n");
printf("******** 3、英语未及格的学生信息 ********\n");
printf("******** 4、C语言未及格的学生信息 ********\n");
printf("******** 5、物理未及格的学生信息 ********\n");
printf("******** 0、返回主菜单 ********\n");
printf("******** 请选择(0-5) ********\n");
scanf("%d",&choice);
switch(choice)
{ case 1:ave_message();break;
case 2:math_message();break;
case 3:english_message();break;
case 4:Cprogramming_message();break;
case 5:physic_message();break;
case 0:break;
}
} while(choice!=0);
}
main()
{int choice;
sum=read_message();
printf(" ********************************************************\n");
printf(" **********************欢 迎 进 入********************\n");
printf(" *** ***\n");
printf(" *** ***\n");
printf(" *** ***\n");
printf(" ********************学生成绩信息管理系统****************\n");
printf(" *** ***\n");
printf(" *** ***\n");
printf(" *** ***\n");
printf(" ******************PRESS ANYKEY TO CONTINUE**************\n");
printf(" *** ***\n");
printf(" *** ***\n");
printf(" *** ***\n");
printf(" ********************************************************\n");
bioskey(0);
do
{clrscr();
printf(" ********************************************************\n");
printf(" ********************************************************\n");
printf(" ***************** 1、录入学生信息 ****************\n");
printf(" ***************** 2、打印学生信息 ****************\n");
printf(" ***************** 3、查询学生信息 ****************\n");
printf(" ***************** 4、添加学生信息 ****************\n");
printf(" ***************** 5、修改学生信息 ****************\n");
printf(" ***************** 6、删除学生信息 ****************\n");
printf(" ***************** 7、成绩名次信息 ****************\n");
printf(" ***************** 8、成绩统计信息 ****************\n");
printf(" ***************** 0、退出系统 ****************\n");
printf(" ********************************************************\n");
printf(" ********************************************************\n");
scanf("%d",&choice);
switch(choice)
{case 1: input_message();break;
case 2: output_message();break;
case 3: inquire_message();break;
case 4: insert_message();break;
case 5: revise_message();break;
case 6: del_message();break;
case 7: order_message();break;
case 8: tongji_message();break;
case 0: break;
}
}while(choice!=0);
save_message(sum);
printf("\n\n 以上学生信息已保存!(文件名:student information.txt)\n\n");
}