|
正好今天公司没活干~遇到我算你幸运~这种题应该发到开发群里去...
#include <stdio.h>
#include <time.h>
#define STD_NUM 3
#define SBJ_NUM 6
void SetScore();
void PrintScore();
void PrintAVG();
typedef struct Student{
double allscore;
float score[6];
}Student;
int main()
{
Student s[STD_NUM];
char *objarr[SBJ_NUM] = {"eng","chs","math","phy","gov", "gym"};
/*设置成绩*/
SetScore(s);
/*输出成绩*/
PrintScore(s, objarr);
/*输出平均成绩*/
PrintAVG(s);
getch();
}
void SetScore(Student s[STD_NUM])
{
int i;
int j;
for (i = 0; i < STD_NUM; i++)
{
s.allscore = 0;
srand( (unsigned)(i+1)); /*设置随机种子*/
for (j = 0; j < SBJ_NUM; j++)
{
s.score[j] = rand()%100; /*0-100随机数*/
s.allscore += s.score[j];
}
}
getch();
}
void PrintScore(Student s[STD_NUM], char *p[SBJ_NUM])
{
int i;
int j;
Student stutemp;
/*排序*/
for (i = 0; i < STD_NUM-1; i++)
{
for (j = i+1; j < STD_NUM; j++)
if (s.allscore < s[j].allscore)
{
stutemp = s;
s = s[j];
s[j] = stutemp;
}
}
for (i = 0; i < STD_NUM; i++)
{
/*打印表头*/
if (i == 0)
{
printf("\t");
for (j = 0; j < SBJ_NUM; j++)
{
printf("%s\t", p[j]);
}
printf("all\n");
}
printf("%d:\t", i+1);
/*开始打印成绩数据 */
for (j = 0; j < SBJ_NUM; j++)
{
printf("%2.2f\t", s.score[j]);
}
printf("%f\n", s.allscore);
}
}
void PrintAVG(Student s[STD_NUM])
{
int i;
int j;
double all = 0;
float avg = 0;
printf("平均成绩");
for (i = 0; i < SBJ_NUM; i++)
{
all = 0;
for (j = 0; j < STD_NUM; j++)
{
all += s[j].score;
}
avg = all/STD_NUM;
printf("%2.2f\t", avg);
}
} |
|