呃,请教下,下面这个代码的数据结构怎样写,麻烦各位了
程序代码:
/*将5位同学3个科目的成绩分别输出,并求出平均分和总分,按总分的大到小排列*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <time.h> /*M必须大于1*/ #define M 5 #define N 5 void input(int scores[][3]); void cal(int scores[][3], double avgR[], double avgC[]); void sort(double avgR[], int idx[]); void printstring(char *s, int n); //打印给定长度的字符串 int main(void) { char *ke[N] = {"语文","数学","英语","平均分","总分"}; char *names[M] = {"张岚", "李风", "王玉", "赵之风", "田玲"}; int scores[M][3] = {0}, idx[M] = {0}, i, j; double avgR[M] = {0}, avgC[3] = {0}; input(scores); cal(scores, avgR, avgC); sort(avgR, idx); for(j=0;j<N;j++) { if(j==0) printstring(" ",8); printstring(ke[j],8); } printf("\n"); for (i = 0; i < M; i++) { printstring(names[i],8); for (j = 0; j < 3; j++) { printf("%3d ", scores[i][j]); //3位数+5个空格 } printf("%3.1lf ", avgR[i]); //4位数+4个空格 printf("%3.0f \n", avgR[i]*3); //总分=平均分*3 } getchar(); return 0; } void input(int score[][3]) { int i, j; srand((unsigned) time(NULL)); for (i = 0; i < M; i++) for (j = 0; j < 3; j++) score[i][j] = 40 + rand() % 61; return; } void cal(int scores[][3], double avgR[], double avgC[]) { int i, j; for (i = 0; i < M; i++) { for (j = 0; j < 3; j++) { avgR[i] += scores[i][j]; avgC[j] += scores[i][j]; } avgR[i] /= 3; } for (j = 0; j < 3; j++) avgC[j] /= M; return; } void sort(double avgR[], int idx[]) { int i, j; for (i = 0; i < M; i++) idx[i] = i; for (i = 0; i < M - 1; i++) { for (j = i + 1; j < M; j++) if (avgR[i] < avgR[j]) avgR[idx[i]] = avgR[idx[j]]; } return; } void printstring(char *s, int n) { int i=strlen(s),j; if(i<=n) { printf("%s",s); for(j=0;j<(n-i);j++) printf(" "); } else printf("%8s",s); }