用结构体数组相对简单的了.
#include <stdio.h>
typedef struct student{
char num[20];
char name[10];
char sex;
int score;
}STU;
#define N 2
STU stu[N];
void input();
void output();
void sort();
void main()
{
input(stu);
output(stu);
sort(stu);
output(stu);
}
void input(STU stu1[])
{
int i;
FILE *fp;
fp=fopen("c:\\temp.dat","wb");
if(fp==NULL){
printf("can't open this file");
exit(1);
}
for(i=0;i<N;i++){
printf("please input student number:");
scanf("%s",stu1[i].num);
getchar();
printf("please input student name:");
scanf("%s",stu1[i].name);
getchar();
printf("please input student sex:");
scanf("%c",&stu1[i].sex);
getchar();
printf("please input student score:");
scanf("%d",&stu1[i].score);
fprintf(fp,"%s,%s,%c,%d\n",stu1[i].num,stu1[i].name,stu1[i].sex,stu1[i].score);
}
fclose(fp);
}
void output(STU stu2[])
{
int i;
printf("\n\n num name sex score\n");
for(i=0;i<N;i++){
printf("%20s%10s%5c%10d\n",stu2[i].num,stu2[i].name,stu2[i].sex,stu2[i].score);
}
}
void sort(STU stu3[])
{
STU stud;
int i,j;
for(i=0;i<N-1;i++)
for(j=0;j<N-i-1;j++){
if(stu3[j].score<stu3[j+1].score){
stud=stu3[j];
stu3[j]=stu3[j+1];
stu3[j+1]=stud;
}
}
}