优质解答
#include
#include
typedef struct stu{
int id;
char name[40];
int mark[4];
float total;
} STU;
int id;
int m1,m2,m3,m4;
char str[40];
void main()
{
int i,j,N=0;
FILE *fin, *fout;
STU tmp;
STU *s;
fin = fopen("score.txt","r");
while (1){
if ( fscanf(fin,"%*d %*s %*d %*d %*d %*d") ==EOF) break;
N++;
}
fclose(fin);
s = (STU *) malloc(N * sizeof(STU));
if (!s) {
printf("no enough memory for STU"); exit(0);
}
fin = fopen("score.txt","r");
for (i=0;ifscanf(fin,"%d %s %d %d %d %d",&id,&str[0],&m1,&m2,&m3,&m4);
s[i].id = id;
strcpy(&s[i].name[0],str);
s[i].mark[0]= m1;
s[i].mark[1]= m2;
s[i].mark[2]= m3;
s[i].mark[3] = m4;
s[i].total = 0.2*m1 + 0.6*m2 + 0.5 * m3 + 0.15*m4;
}
fclose(fin);
for (i=0;ifor (j=i+1;jif (s[j].total > s[i].total){
tmp = s[j]; s[j]=s[i];s[i]=tmp;
};
};
};
fout = fopen("score_Q.txt","w");
for(i=0;ifprintf(fout,"%d %s %d %d %d %d",s[i].id,s[i].name,s[i].mark[0],s[i].mark[1],s[i].mark[2],s[i].mark[3]);
}
fclose(fout);
exit(0);
}
#include
#include
typedef struct stu{
int id;
char name[40];
int mark[4];
float total;
} STU;
int id;
int m1,m2,m3,m4;
char str[40];
void main()
{
int i,j,N=0;
FILE *fin, *fout;
STU tmp;
STU *s;
fin = fopen("score.txt","r");
while (1){
if ( fscanf(fin,"%*d %*s %*d %*d %*d %*d") ==EOF) break;
N++;
}
fclose(fin);
s = (STU *) malloc(N * sizeof(STU));
if (!s) {
printf("no enough memory for STU"); exit(0);
}
fin = fopen("score.txt","r");
for (i=0;ifscanf(fin,"%d %s %d %d %d %d",&id,&str[0],&m1,&m2,&m3,&m4);
s[i].id = id;
strcpy(&s[i].name[0],str);
s[i].mark[0]= m1;
s[i].mark[1]= m2;
s[i].mark[2]= m3;
s[i].mark[3] = m4;
s[i].total = 0.2*m1 + 0.6*m2 + 0.5 * m3 + 0.15*m4;
}
fclose(fin);
for (i=0;ifor (j=i+1;jif (s[j].total > s[i].total){
tmp = s[j]; s[j]=s[i];s[i]=tmp;
};
};
};
fout = fopen("score_Q.txt","w");
for(i=0;ifprintf(fout,"%d %s %d %d %d %d",s[i].id,s[i].name,s[i].mark[0],s[i].mark[1],s[i].mark[2],s[i].mark[3]);
}
fclose(fout);
exit(0);
}