还是不行啊,完整代码是这样的。麻烦帮帮忙!
#include "stdio.h"
#include"string"
struct Student
/*定义学生结构体*/
{
char ID[20];
char Name[20];
float Mark1;
float Mark2;
float Mark3;
float Average;
int age;
int classs;
char zy[20];
char xb[20];
char sex[20];
};
struct Student students[1000]; /*声明学生数组及学生数量*/
int num=0;
float Avg(struct Student stu)
/*求平均值*/
{
return (stu.Mark1+stu.Mark2+stu.Mark3)/3;
}
int Student_SearchByIndex(char id[])
/*通过学号返回数组下标*/
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(students[i].ID,id)==0)
{
return i;
}
}
return -1;
}
int Student_SearchByName(char name[])
/*通过姓名返回数组下标*/
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(students[i].Name,name)==0)
{
return i;
}
}
return -1;
}
void Student_DisplaySingle(int index)
/*显示单条学生记录*/
{
printf("%10s%10s%8s%8s%8s%10s%d%d%10s%10s%\n","学号","姓名","语文成绩","数学成绩","英语成绩","平均成绩","年龄","班级","专业","系别","性别");
printf("-------------------------------------------------------------\n");
printf("%10s%10s%8.2f%8.2f%8.2f%10.2f%d%d%10s%10s%10s\n",students[index].ID,students[index].Name,
students[index].Mark1,students[index].Mark2,students[index].Mark3,students[index].Average,students[index].age,students[index].classs,students[index].zy,students[index].xb,students[index].sex);
}
void Student_Insert()
/*插入学生信息*/
{
while(1)
{
printf("请输入学号:");
scanf("%s",&students[num].ID);
getchar();
printf("请输入姓名:");
scanf("%s",&students[num].Name);
getchar();
printf("请输入语文成绩:");
scanf("%f",&students[num].Mark1);
getchar();
printf("请输入数学成绩:");
scanf("%f",&students[num].Mark2);
getchar();
printf("请输入英语成绩:");
scanf("%f",&students[num].Mark3);
getchar();
printf("请输入年龄:");
scanf("%d",&students[num].age);
getchar();
printf("请输入班级:");
scanf("%d",&students[num].classs);
getchar();
printf("请输入专业:");
scanf("%s",&students[num].zy);
getchar();
printf("请输入系别:");
scanf("%s",&students[num].xb);
getchar();
printf("请输入性别:");
scanf("%s",&students[num].sex);
getchar();
students[num].Average=Avg(students[num]);
num++;
printf("是否继续?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
void Student_Modify()
/*修改学生信息*/
{
while(1)
{
char id[20];
int index;
printf("请输入要修改的学生的学号:");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if (index==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要修改的学生信息为:\n");
Student_DisplaySingle(index);
printf("-- 请输入新值--\n");
printf("请输入学号:");
scanf("%s",&students[index].ID);
getchar();
printf("请输入姓名:");
scanf("%s",&students[index].Name);
getchar();
printf("请输入语文成绩:");
scanf("%f",&students[index].Mark1);
getchar();
printf("请输入数学成绩:");
scanf("%f",&students[index].Mark2);
getchar();
printf("请输入英语成绩:");
scanf("%f",&students[index].Mark3);
getchar();
printf("请输入年龄:");
scanf("%d",&students[index].age);
getchar();
printf("请输入班级:");
scanf("%d",&students[index].classs);
getchar();
printf("请输入专业:");
scanf("%s",&students[index].zy);
getchar();
printf("请输入系别:");
scanf("%s",&students[index].xb);
getchar();
printf("请输入性别:");
scanf("%s",&students[index].sex);
getchar();
students[index].Average=Avg(students[index]);
}
printf("是否继续?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
void Student_Delete()
/*删除学生信息*/
{
int i;
while(1)
{
char id[20];
int index;
printf("请输入要删除的学生的学号:");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if (index==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要删除的学生信息为:\n");
Student_DisplaySingle(index);
printf("是否真的要删除?(y/n)");
if (getchar()=='y')
{
for (i=index;i<num-1;i++)
{
students[i]=students[i+1];//把后边的对象都向前移动
}
num--;
}
getchar();
}
printf("是否继续?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
void Student_Select()
/*按学号查询*/
{
while(1)
{
char id[20];
int index;
printf("请输入要查询的学生的学号:");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if (index==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要查询的学生信息为:\n");
Student_DisplaySingle(index);
}
printf("是否继续?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
void Student_Display() /*显示学生信息*/
{
int i;
printf("%10s%10s%8s%8s%8s%10s\n","学号","姓名","语文成绩","数学成绩","英语成绩","平均成绩","年龄","班级","专业","系别","性别");
printf("-------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
printf("%10s%10s%8.2f%8.2f%8.2f%10.2f%d%d%10s%10s%10s\n",students[i].ID,students[i].Name,
students[i].Mark1,students[i].Mark2,students[i].Mark3,students[i].Average,students[i].age,students[i].classs,students[i].zy,students[i].xb,students[i].sex);
}
}
void Student_cjrs() /*统计男女学生人数*/
{
char sex[]={"man"};
int womennum=0,mannum=0;
int i;
for(i=0;i<num;i++)
{
if (strcmp(students[i].sex,sex)==0) {mannum++;}
else womennum++;
}
printf("男生女生的人数分别为mannum=%4d,womennum=%4d\n",mannum,womennum);
}
void IO_ReadInfo()
/*将学生信息从文件读出*/
{
FILE *fp;
int i;
if ((fp=fopen("Database.txt","rb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
if (fread(&num,sizeof(int),1,fp)!=1)
{
num=-1;
}
else
{
for(i=0;i<num;i++)
{
fread(&students[i],sizeof(struct Student),1,fp);
}
}
fclose(fp);
}
void IO_WriteInfo()
/*将学生信息写入文件*/
{
FILE *fp;
int i;
if ((fp=fopen("Database.txt","wb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
if (fwrite(&num,sizeof(int),1,fp)!=1)
{
printf("写入文件错误!\n");
}
for (i=0;i<num;i++)
{
if (fwrite(&students[i],sizeof(struct Student),1,fp)!=1)
{
printf("写入文件错误!\n");
}
}
fclose(fp);
}
void main()
/*主程序*/
{
int choice;
IO_ReadInfo();
while(1)
{
printf("\n*****东莞理工学院学生成绩管理系统*******\n");
/*主菜单*/
printf("*********
1. 输入或增加学生记录 *********\n");
printf("*********
2. 修改学生记录
*********\n");
printf("*********
3. 删除学生记录
*********\n");
printf("*********
4. 按学号查询学生记录 *********\n");
printf("*********
5. 显示所有学生的信息 *********\n");
printf("*********
6. 统计专业男女生人数 *********\n");
printf("*********
7.退出
*********\n");
printf("请选择(1-7):");
scanf("%d",&choice);
getchar();
switch(choice)
{
case 1:
Student_Insert();
break;
case 2:
Student_Modify();
break;
case 3:
Student_Delete();
break;
case 4:
Student_Select();
break;
case 5:
Student_Display();
break;
case 6:
Student_cjrs();
break;
case 7:
exit(0);
break;
}
IO_WriteInfo();
}
}