学生成绩管理系统
一下文件有错,哪里哇,求改,,学生成绩管理系统
用不同的文件分别保存学生信息、课程信息和学生选课成绩信息,任何一门课的成绩由平时成绩(x%)、期中成绩(y%)和期末成绩(z%)的比例关系计算成总成绩(x+y+z=100),均以100分制计算。程序要做到能对课程信息、学生信息和选课成绩信息进行增、删、改和查的功能,并能按总分进行升序或降序排列。系统以菜单方式工作。
改成以上的
#include "stdio.h"
struct Student
{
char ID[20];
char Name[20];
float Mark1;
float Mark2;
float Mark3;
float Average;
};
struct Student students[100];
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\n","Student id," "name," "result", "result", "result", "average grade"
);
printf("-------------------------------------------------------------\n");
printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n",students[index].ID,students[index].Name,
students[index].Mark1,students[index].Mark2,students[index].Mark3,students[index].Average);
}
void Student_Insert()
{
while(1)
{
printf("Please input student id:");
scanf("%s",&students[num].ID);
getchar();
printf("Please input student name:");
scanf("%s",&students[num].Name);
getchar();
printf("Please input student score:");
scanf("%f",&students[num].Mark1);
getchar();
printf("Please input student score:");
scanf("%f",&students[num].Mark2);
getchar();
printf("Please input student score:");
scanf("%f",&students[num].Mark3);
getchar();
students[num].Average=Avg(students[num]);
num++;
printf("Do you wish to continue? (y/n)");
if (getchar()=='n')
{
break;
}
}
}
void Student_Modify()
{
float mark1,mark2,mark3;
while(1)
{
char id[20];
int index;
printf("Please input to change the students' student id: ");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if (index==-1)
{
printf("Students don't exist! \n");
}
else
{
printf("You want to change for the students' information:\n");
Student_DisplaySingle(index);
printf("-- Please enter a new value --\n");
printf("Please input the students' student id:");
scanf("%s",&students[index].ID);
getchar();
printf("Please input student name:");
scanf("%s",&students[index].Name);
getchar();
printf("Please input student score:");
scanf("%f",&students[index].Mark1);
getchar();
printf("Please input student score:");
scanf("%f",&students[index].Mark2);
getchar();
printf("Please input student score:");
scanf("%f",&students[index].Mark3);
getchar();
students[index].Average=Avg(students[index]);
}
printf("Do you wish to continue?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
void Student_Delete()
{
int i;
while(1)
{
char id[20];
int index;
printf("Please enter the students want to delete the student id: ");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if (index==-1)
{
printf("Students don't exist!\n");
}
else
{
printf("You want to delete the information for the students :\n");
Student_DisplaySingle(index);
printf("Really want to delete? (y/n)");
if (getchar()=='y')
{
for (i=index;i<num-1;i++)
{
students[i]=students[i+1];
}
num--;
}
getchar();
}
printf("Do you wish to continue? (y/n)");
if (getchar()=='n')
{
break;
}
}
}
void Student_Select()
{
while(1)
{
char name[20];
int index;
printf("Please input the name of the students to inquires :");
scanf("%s",&name);
getchar();
index=Student_SearchByName(name);
if (index==-1)
{
printf("Students don't exist! \n");
}
else
{
printf("You want the students for information query :\n");
Student_DisplaySingle(index);
}
printf("Do you wish to continue?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
void Student_SortByAverage()
{
int i,j;
struct Student tmp;
for (i=0;i<num;i++)
{
for (j=1;j<num-i;j++)
{
if (students[j-1].Average<students[j].Average)
{
tmp=students[j-1];
students[j-1]=students[j];
students[j]=tmp;
}
}
}
}
void Student_Display()
{
int i;
printf("%10s%10s%8s%8s%8s%10s\n","Student id," "name," "result", "result", "result", "average grade" );
printf("-------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n",students[i].ID,students[i].Name,
students[i].Mark1,students[i].Mark2,students[i].Mark3,students[i].Average);
}
}
void IO_ReadInfo()
{
FILE *fp;
int i;
if ((fp=fopen("Database.txt","rb"))==NULL)
{
printf("Cannot open file! \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("Can't be open files! \n");
return;
}
if (fwrite(&num,sizeof(int),1,fp)!=1)
{
printf("Written to the file error! \n");
}
for (i=0;i<num;i++)
{
if (fwrite(&students[i],sizeof(struct Student),1,fp)!=1)
{
printf("Written to the file error! \n");
}
}
fclose(fp);
}
main()
{
int choice;
IO_ReadInfo();
while(1)
{
Printf (" \ n- - - students' performance management system- - - \ n ");
Printf (" 1. Increase students' records \ n ");
Printf (" 2. Modify students to record \ n ");
Printf (" 3. Delete students to record \ n ");
Printf (" 4. Name inquires according to the students' records \ n ");
Printf (" 5. According to the average grade sequence \ n ");
Printf (" 6. Quit \ n ");
Printf (" please choose (1-6) : ");
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_SortByAverage();
Student_Display();
break;
case 6:
exit();
break;
}
IO_WriteInfo();
}
}