程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int i, j;
char ch;
struct {
double sco[3];
double sum;
double avg;
} class[10] = {
{{0, 0, 0}, 0, 0},
{{0, 0, 0}, 0, 0},
{{0, 0, 0}, 0, 0},
{{0, 0, 0}, 0, 0},
{{0, 0, 0}, 0, 0},
{{0, 0, 0}, 0, 0},
{{0, 0, 0}, 0, 0},
{{0, 0, 0}, 0, 0},
{{0, 0, 0}, 0, 0},
{{0, 0, 0}, 0, 0}
};
int comp(const void *a, const void*b)
{
return *(int*)b - *(int*)a;
}
void print_stu_sco(void) {
for(i = 0; i < 10; i++) {
for(j = 0; j < 3; j++) {
printf("%.1lf\t", class[i].sco[j]);
}
printf("\n");
}
}
void print_stu_avg(void) {
for(i = 0; i < 10; i++) {
printf("%.1lf\t", class[i].avg);
}
printf("\n");
}
void print_class_avg(void) {
double class_sum0=0, class_sum1=0, class_sum2=0;
for(i = 0; i < 10; i++) {
class_sum0 = class_sum0 + class[i].sco[0];
class_sum1 = class_sum1 + class[i].sco[1];
class_sum2 = class_sum2 + class[i].sco[2];
}
printf("%.1lf\t%.1lf\t%.1lf\n", class_sum0 / 10, class_sum1 / 10, class_sum2 / 10);
printf("\n");
}
void sort_stu_avg(void) {
double avg_sort[10];
for(i = 0; i < 10; i++) {
avg_sort[i] = class[i].avg;
}
qsort(avg_sort, 10, sizeof(double), comp);
for(i = 0; i < 10; i++) {
printf("%.1lf\t", avg_sort[i]);
}
printf("\n");
}
int main(void) {
srand((unsigned int)time(NULL));
for(i = 0; i < 10; i++) {
for(j = 0; j < 3; j++) {
/*不嫌烦你就挨个填
scanf("%lf", &(class[i].sco[j]));
*/
class[i].sco[j]=1 + (100.0 * rand() / (RAND_MAX + 1.0));
class[i].sum = class[i].sum + class[i].sco[j];
class[i].avg = (class[i].sum) / j;
}
}
printf("\n");
scanf("%c", &ch);
switch(ch) {
case'a':
case'A':
print_stu_sco();
break;
case'b':
case'B':
print_stu_avg();
break;
case'c':
case'C':
print_class_avg();
break;
case'd':
case'D':
sort_stu_avg();
break;
default:
puts("error!");
}
return 0;
}