#include
#include
typedef struct {
char* NAME;
float LESSONS[4];
float AVE;
} Student;
const char* Lessons_name[4] = {"Chinese", "Math", "English", "Phisics"};
Student stu[6];
char* filename = "D:\\temp\\stu.txt";
FILE *fp;
void inputData() {
int i, j;
float score;
if((fp = fopen(filename, "w")) == NULL)
{
printf("file open error");
exit(1);
}
for(i = 0; i < 6; i++)
for(j = 0; j < 4; j++) {
scanf("%f", &score);
fprintf(fp, "%f\n", score);
}
fclose(fp);
}
void readFile() {
int i, j;
if((fp = fopen(filename, "r")) == NULL)
{
printf("file open error");
exit(1);
}
for(i = 0; i < 6; i++)
for(j = 0; j < 4; j++)
fscanf(fp, "%f\n", &stu[i].LESSONS[j]);
fclose(fp);
}
void printStudentStat() {
int i, j, min_index[6], max_index[6];
float sum[6] = {0}, max[6] = {0}, min[6] = {1000, 1000, 1000, 1000, 1000, 1000};
for(i = 0; i < 6; i++) {
for(j = 0; j < 4; j++) {
sum[i] += stu[i].LESSONS[j];
if(stu[i].LESSONS[j] > max[i]) {
max[i] = stu[i].LESSONS[j];
max_index[i] = j;
}
if(stu[i].LESSONS[j] < min[i]) {
min[i] = stu[i].LESSONS[j];
min_index[i] = j;
}
}
stu[i].AVE = sum[i] / 4;
}
printf("Students name:\t");
for(i = 0; i < 6; i++)
printf("%s\t", stu[i].NAME);
printf("\nAverage scroe:\t");
for(i = 0; i < 6; i++)
printf("%.1f\t", stu[i].AVE);
printf("\nHighest score:\t");
for(i = 0; i < 6; i++)
printf("%.1f\t", max[i]);
printf("\nLessons' name:\t");
for(i = 0; i < 6; i++)
printf("%s\t", Lessons_name[max_index[i]]);
printf("\nLowest score: \t");
for(i = 0; i < 6; i++)
printf("%.1f\t", min[i]);
printf("\nLessons' name:\t");
for(i = 0; i < 6; i++)
printf("%s\t", Lessons_name[min_index[i]]);
printf("\n");
}
void printLessonStat() {
int i, j, min_index[4], max_index[4];
float sum[4] = {0}, max[4] = {0}, min[4] = {1000, 1000, 1000, 1000};
for(i = 0; i < 6; i++)
for(j = 0; j < 4; j++) {
sum[j] += stu[i].LESSONS[j];
if(stu[i].LESSONS[j] > max[j]) {
max[j] = stu[i].LESSONS[j];
max_index[j] = i;
}
if(stu[i].LESSONS[j] < min[j]) {
min[j] = stu[i].LESSONS[j];
min_index[j] = i;
}
}
printf("\nLessons' name:\t");
for(j = 0; j < 4; j++)
printf("%s\t", Lessons_name[j]);
printf("\nAverage scroe:\t");
for(j = 0; j < 4; j++)
printf("%.1f\t", sum[j] / 6);
printf("\nHighest score:\t");
for(j = 0; j < 4; j++)
printf("%.1f\t", max[j]);
printf("\nStudents name:\t");
for(j = 0; j < 4; j++)
printf("%s\t", stu[max_index[j]].NAME);
printf("\nLowest score: \t");
for(j = 0; j < 4; j++)
printf("%.1f\t", min[j]);
printf("\nStudents name:\t");
for(j = 0; j < 4; j++)
printf("%s\t", stu[min_index[j]].NAME);
printf("\n");
}
void sortAverage() {
int i, j;
for(i = 0; i < 5; i++)
for(j = 0; j < 5 - i; j++)
if(stu[j].AVE < stu[j + 1].AVE) {
Student tmp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = tmp;
}
printf("\n");
for(i = 0; i < 6; i++)
printf("Average No.%d\t%s\t%.1f\n", i + 1, stu[i].NAME, stu[i].AVE);
}
int main() {
stu[0].NAME = "Wang";
stu[1].NAME = "Li";
stu[2].NAME = "Zhang";
stu[3].NAME = "Zhao";
stu[4].NAME = "Yang";
stu[5].NAME = "Wu";
inputData();
readFile();
printStudentStat();
printLessonStat();
sortAverage();
return 0;
}
输入:
98.5 94.6 55.5 52.7
55.7 61.5 73.6 57.3
81.2 56.5 68.3 96.5
93.7 95.9 76 61.7
74.7 77.3 81.3 95
77.6 57.2 84.6 63.5
输出:
Students name: Wang Li Zhang Zhao Yang Wu
Average scroe: 75.3 62.0 75.6 81.8 82.1 70.7
Highest score: 98.5 73.6 96.5 95.9 95.0 84.6
Lessons' name: Chinese English Phisics Math Phisics English
Lowest score: 52.7 55.7 56.5 61.7 74.7 57.2
Lessons' name: Phisics Chinese Math Phisics Chinese Math
Lessons' name: Chinese Math English Phisics
Average scroe: 80.2 73.8 73.2 71.1
Highest score: 98.5 95.9 84.6 96.5
Students name: Wang Zhao Wu Zhang
Lowest score: 55.7 56.5 55.5 52.7
Students name: Li Zhang Wang Wang
Average No.1 Yang 82.1
Average No.2 Zhao 81.8
Average No.3 Zhang 75.6
Average No.4 Wang 75.3
Average No.5 Wu 70.7
Average No.6 Li 62.0