#include
#include
#include
#define MALE 1
#define FEMALE 0
#define PASS_SCORE 60
typedef struct Student
{
int studentID; //学号
char name[20]; //姓名
int gender; //性别
int score; //成绩
}Student;
typedef struct
{
Student array[20000];
int femaleNum; //女生数量
int maleNum; //男生数量
int malePassNum; //男生及格数量
int femalePassNum; //女生及格数量
double averageScore; //平均成绩
double malePercent; //男生及格比率
double femalePercent; //女生及格比率
}StudentList;
//排序
int sort(Student* list,int left,int right)
{
Student mid=list[left];
while(left
{
for(;left
list[left]=list[right];
for(;left=mid.score;left++);
list[right]=list[left];
}
list[left]=mid;
return left;
}
void quick_sort(Student*list,int left,int right)
{
if(left>=right)
return;
int mid;
mid=sort(list,left,right);
quick_sort(list,left,mid-1);
quick_sort(list,mid+1,right);
}
void merge(StudentList *result,Student *male,Student *female)
{
int i,num=0;
for(i=0;imaleNum;i++)
result->array[num++]=male[i];
for(i=0;ifemaleNum;i++)
result->array[num++]=female[i];
}
//打印学生信息
void Print(StudentList *pStudentList)
{
FILE *fp;
fp = fopen("strname.txt","w");
if (fp == NULL)
{
printf("can't open the file!\n");
exit(0);
}
for(int i=0;imaleNum+pStudentList->femaleNum;i++)
{
fprintf(fp,"%u %s %4u %d\n",pStudentList->array[i].studentID,pStudentList->array[i].name,pStudentList->array
[i].gender,pStudentList->array[i].score);
}
fprintf(fp,"Male pass percent:%f%%\n",pStudentList->malePercent);
fprintf(fp,"Female pass percent:%f%%\n",pStudentList->femalePercent);
fprintf(fp,"Average score:%f\n",pStudentList->averageScore);
fclose(fp);
}
void freeAllData( StudentList* pStudentList )
{
free(pStudentList);
}
int main()
{
StudentList pStudentList;
Student Info;
Student male[10000],female[10000];
double sumScore = 0;
//学生列表初始化
pStudentList.maleNum=0;
pStudentList.femaleNum=0;
pStudentList.femalePercent = 0;
pStudentList.malePercent = 0;
pStudentList.malePassNum = 0;
pStudentList.femalePassNum = 0;
pStudentList.averageScore = 0;
//读取文件
FILE *fp;
fp = fopen("records.txt","r");
if (fp == NULL)
{
printf("can't open the file!\n");
exit(0);
}
while (!feof(fp))
{
fscanf(fp,"%d%s%d%d",&(Info.studentID),Info.name,&(Info.gender),&(Info.score));
if (MALE == Info.gender)
{
male[pStudentList.maleNum++]=Info;
if (Info.score >= PASS_SCORE)
{
pStudentList.malePassNum++;
}
}
else
{
female[pStudentList.femaleNum++]=Info;
if(Info.score >= PASS_SCORE)
{
pStudentList.femalePassNum++;
}
}
sumScore += Info.score;
}
quick_sort(male,0,pStudentList.maleNum-1);
quick_sort(female,0,pStudentList.femaleNum-1);
merge(&pStudentList,male,female);
pStudentList.averageScore = sumScore/(pStudentList.femaleNum+pStudentList.maleNum);
pStudentList.malePercent = pStudentList.malePassNum*1.0/pStudentList.maleNum*100;
pStudentList.femalePercent = pStudentList.femalePassNum*1.0/pStudentList.femaleNum*100;
Print(&pStudentList);
//清理申请内存
freeAllData(&pStudentList);
return 0;
}