#include "stdio.h"
/*定義學(xué)生結(jié)構(gòu)體*/
struct Student
{
char ID[20];
char Name[20];
float Mark1;
float Mark2;
float Mark3;
float Average;
};
/*聲明學(xué)生數(shù)組及學(xué)生數(shù)量*/
struct Student students[1000];
int num=0;
/*求平均值*/
float Avg(struct Student stu)
{
return (stu.Mark1+stu.Mark2+stu.Mark3)/3;
}
/*通過學(xué)號返回數(shù)組下標(biāo)*/
int Student_SearchByIndex(char id[])
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(students[i].ID,id)==0)
{
return i;
}
}
return -1;
}
/*通過姓名返回數(shù)組下標(biāo)*/
int Student_SearchByName(char name[])
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(students[i].Name,name)==0)
{
return i;
}
}
return -1;
}
/*顯示單條學(xué)生記錄*/
void Student_DisplaySingle(int index)
{
printf("%10s%10s%8s%8s%8s%10s\n","學(xué)號","姓名","成績","成績","成績","平均成績");
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);
}
/*插入學(xué)生信息*/
void Student_Insert()
{
while(1)
{
printf("請輸入學(xué)號:");
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();
students[num].Average=Avg(students[num]);
num++;
printf("是否繼續(xù)?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
/*修改學(xué)生信息*/
void Student_Modify()
{
float mark1,mark2,mark3;
while(1)
{
char id[20];
int index;
printf("請輸入要修改的學(xué)生的學(xué)號:");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if (index==-1)
{
printf("學(xué)生不存在!\n");
}
else
{
printf("你要修改的學(xué)生信息為:\n");
Student_DisplaySingle(index);
printf("-- 請輸入新值--\n");
printf("請輸入學(xué)號:");
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();
students[index].Average=Avg(students[index]);
}
printf("是否繼續(xù)?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
/*刪除學(xué)生信息*/
void Student_Delete()
{
int i;
while(1)
{
char id[20];
int index;
printf("請輸入要刪除的學(xué)生的學(xué)號:");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if (index==-1)
{
printf("學(xué)生不存在!\n");
}
else
{
printf("你要刪除的學(xué)生信息為:\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("是否繼續(xù)?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
/*按姓名查詢*/
void Student_Select()
{
while(1)
{
char name[20];
int index;
printf("請輸入要查詢的學(xué)生的姓名:");
scanf("%s",&name);
getchar();
index=Student_SearchByName(name);
if (index==-1)
{
printf("學(xué)生不存在!\n");
}
else
{
printf("你要查詢的學(xué)生信息為:\n");
Student_DisplaySingle(index);
}
printf("是否繼續(xù)?(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;
}
}
}
}
/*顯示學(xué)生信息*/
void Student_Display()
{
int i;
printf("%10s%10s%8s%8s%8s%10s\n","學(xué)號","姓名","成績","成績","成績","平均成績");
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);
}
}
/*將學(xué)生信息從文件讀出*/
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);
}
/*將學(xué)生信息寫入文件*/
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);
}
/*主程序*/
main()
{
int choice;
IO_ReadInfo();
while(1)
{
/*主菜單*/
printf("\n------ 學(xué)生成績管理系統(tǒng)------\n");
printf("1. 增加學(xué)生記錄\n");
printf("2. 修改學(xué)生記錄\n");
printf("3. 刪除學(xué)生記錄\n");
printf("4. 按姓名查詢學(xué)生記錄\n");
printf("5. 按平均成績排序\n");
printf("6. 退出\n");
printf("請選擇(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