#include<stdio.h> struct student{
char name[];
int No;
int sub[];
int sum;
}; int strcmp(char *s, char *t)
{
int i; for(i = ; s[i] == t[i]; i ++)
if(s[i] == '\0')
return ;
return s[i] - t[i];
} int main()
{
struct student stu[];
struct student sort_byname[], sort_byNo[], sort_bysum[];
int i, j, a, b, c;
int last_three; // open file
FILE *fp;
fp = fopen("info.dat" , "r"); // operate file
i = a = b = c = ;
while(!feof(fp)){
fscanf(fp, "%s %d %d %d %d %d", &stu[i].name, &stu[i].No, &stu[i].sub[], &stu[i].sub[], &stu[i].sub[], &stu[i].sub[]);
// printf("%s %d %d %d %d %d\n", stu[i].name, stu[i].No, stu[i].sub[0], stu[i].sub[1], stu[i].sub[2], stu[i].sub[3]);
last_three = stu[i].No % ;
// printf("%d\n", last_three);
if(last_three% == )
sort_byname[a++] = stu[i];
else if(last_three% == )
sort_byNo[b++] = stu[i];
else if(last_three% == )
sort_bysum[c++] = stu[i];
i++;
} for(int k = ; k < i; k++)
for(int l = ; l < ; l++)
stu[k].sum += stu[k].sub[l]; //printf("%s\n", sort_byname[1].name);
// printf("x = %d\n", strcmp("abcd" , "zabcd"));
// close file
fclose(fp); struct student temp;
for(i = ; i < a; i++)
for(j = ; j < a-i-; j++)
if(strcmp(sort_byname[j].name , sort_byname[j+].name) > ){
temp = sort_byname[j];
sort_byname[j] = sort_byname[j+];
sort_byname[j+] = temp;
} for(i = ; i < b; i++)
for(j = ; j < b-i-; j++)
if(sort_byNo[j].No > sort_byNo[j].No){
temp = sort_byname[j];
sort_byname[j] = sort_byname[j+];
sort_byname[j+] = temp;
} for(i = ; i < c; i++)
for(j = ; j < c-i-; j++)
if(sort_bysum[j].sum > sort_bysum[j].sum){
temp = sort_byname[j];
sort_byname[j] = sort_byname[j+];
sort_byname[j+] = temp;
} // open file
FILE *fp2;
fp2 = fopen("infosort.dat" , "w"); // operate file
fprintf(fp2, "sort_byname:\n");
for(i = ; i < a; i++)
fprintf(fp2, "%s %d %d %d %d %d\n", sort_byname[i].name, sort_byname[i].No, sort_byname[i].sub[], sort_byname[i].sub[], sort_byname[i].sub[], sort_byname[i].sub[]); fprintf(fp2, "sort_byNo:\n");
for(i = ; i < b; i++)
fprintf(fp2, "%s %d %d %d %d %d\n", sort_byNo[i].name, sort_byNo[i].No, sort_byNo[i].sub[], sort_byNo[i].sub[], sort_byNo[i].sub[], sort_byNo[i].sub[]); fprintf(fp2, "sort_bysum:\n");
for(i = ; i < c; i++)
fprintf(fp2, "%s %d %d %d %d %d\n", sort_bysum[i].name, sort_bysum[i].No, sort_bysum[i].sub[], sort_bysum[i].sub[], sort_bysum[i].sub[], sort_bysum[i].sub[]); // close file
fclose(fp2);
return ;
}
注释是在写的中途的测试
以下是由于没写测试写崩的代码(没有正确结果):
#include<stdio.h>
#define MAX 100 struct student{
char name[];
int num;
int score[];
int sum;
}; int main()
{
struct student stu[MAX];
int i, j, k;
struct student sort_byname[MAX], sort_bynum[MAX], sort_bysum[MAX];
int a, b, c; // 统计各类学生的数目 // 1-从文件info.dat读入数据并分类
FILE *fp;
fp = fopen("info.dat" , "r"); i = a = b = c = ;
while(!feof){
fscanf(fp , "%s", &stu[i].name);
fscanf(fp , "%d", &stu[i].num);
for(k = ; k < ; k++){
fscanf(fp, "%d", &stu[i].score[k]);
stu[i].sum += stu[i].score[k]; // 统计总分
}
// 分类
if(stu[i].num% == )
sort_byname[a++] = stu[i];
if(stu[i].num% == )
sort_bynum[b++] = stu[i];
if(stu[i].num% == )
sort_bysum[c++] = stu[i];
i++;
} fclose(fp); // 2-对数据进行排序
int strcmp(char *s, char *t);
void swap(struct student *s, struct student *t); for(i = ; i < a; i ++)
for(j = ; j < a-i-; j ++)
if(strcmp(sort_byname[j].name , sort_byname[j+].name) > )
swap(&sort_byname[j] , &sort_byname[j+]); for(i = ; i < b; i ++)
for(j = ; j < b-i-; j ++)
if(sort_bynum[j].num > sort_bynum[j+].num)
swap(&sort_bynum[j] , &sort_bynum[j+]); for(i = ; i < c; i ++)
for(j = ; j < c-i-; j ++)
if(sort_bynum[j].sum > sort_bynum[j+].sum)
swap(&sort_bysum[j] , &sort_bysum[j+]); // 3-将数据写入 infosort.dat 以下可以用函数实现。。。。。。
fp = fopen("infosort.dat" , "w"); fprintf(fp, "sort_byname:\n");
for(i = ; i < a; i ++){
fprintf(fp , "%s ", sort_byname[i].name);
fprintf(fp , "%d ", sort_byname[i].num);
for(k = ; k < ; k++)
fprintf(fp, "%d ", sort_byname[i].score[k]);
}
fprintf(fp, "sort_bynum:\n");
for(i = ; i < b; i ++){
fprintf(fp , "%s ", sort_bynum[i].name);
fprintf(fp , "%d ", sort_bynum[i].num);
for(k = ; k < ; k++)
fprintf(fp, "%d ", sort_bynum[i].score[k]);
}
fprintf(fp, "sort_bysum:\n");
for(i = ; i < c; i ++){
fprintf(fp , "%s ", sort_bysum[i].name);
fprintf(fp , "%d ", sort_bysum[i].num);
for(k = ; k < ; k++)
fprintf(fp, "%d ", sort_bysum[i].score[k]);
} fclose(fp);
return ;
} int strcmp(char *s, char *t)
{
int i; for(i = ; s[i] == t[i]; i ++)
if(s[i] == '\0')
return ;
return s[i] - t[i];
} void swap(struct student *s, struct student *t)
{
struct student temp; temp = *s;
*s = *t;
*t = temp;
}
测试文件:
wang 67.5 78.9 88.0 99.2
zhang 89.7 45.6 78.8 85.7
tang 56.6 77.7 89.9 85.7
li 56.6 45.6 56.6 85.7
ren 99.9 77.7 89.9 85.7
fa 44.4 55.5 56.6 85.7
compu 56.6 77.7 89.9 85.7