|
|
|
@ -116,3 +116,141 @@ int Menu(void)
|
|
|
|
|
scanf("%d", &itemSelected); // 读入用户输入
|
|
|
|
|
return itemSelected;
|
|
|
|
|
}
|
|
|
|
|
//1 函数功能:输入n个学生的m门课成绩
|
|
|
|
|
void ReadScore(STU stu[], int n, int m)
|
|
|
|
|
{
|
|
|
|
|
int i, j;
|
|
|
|
|
printf("Input student's ID, name and score:\n");
|
|
|
|
|
/* ---------- begain ---------- */
|
|
|
|
|
for(i=0;i<n;i++)
|
|
|
|
|
{scanf("%ld%s",&stu[i].num,stu[i].name);
|
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
|
scanf("%f",&stu[i].score[j]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ----------- end ----------- */
|
|
|
|
|
}
|
|
|
|
|
//2
|
|
|
|
|
void AverSumofEveryStudent(STU stu[], int n, int m)
|
|
|
|
|
{
|
|
|
|
|
int i, j;
|
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
|
{
|
|
|
|
|
stu[i].sum = 0;
|
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
|
{
|
|
|
|
|
stu[i].sum = stu[i].sum + stu[i].score[j];
|
|
|
|
|
}
|
|
|
|
|
stu[i].aver = m>0 ? stu[i].sum / m : -1;
|
|
|
|
|
printf("student %d: sum = %.0f, aver = %.0f\n",
|
|
|
|
|
i+1, stu[i].sum, stu[i].aver);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5使数据按升序排序
|
|
|
|
|
int Ascending(float a, float b)
|
|
|
|
|
{
|
|
|
|
|
return a < b; // 这样比较决定了按升序排序,如果a<b,则交换
|
|
|
|
|
}
|
|
|
|
|
//6
|
|
|
|
|
int Descending(float a, float b)
|
|
|
|
|
{
|
|
|
|
|
return a > b;
|
|
|
|
|
}
|
|
|
|
|
// 9交换两个字符串
|
|
|
|
|
void SwapChar(char x[], char y[])
|
|
|
|
|
{
|
|
|
|
|
char temp[MAX_LEN];
|
|
|
|
|
strcpy(temp, x);
|
|
|
|
|
strcpy(x, y);
|
|
|
|
|
strcpy(y, temp);
|
|
|
|
|
}
|
|
|
|
|
//10
|
|
|
|
|
void AsSortbyNum(STU stu[], int n, int m)
|
|
|
|
|
{
|
|
|
|
|
int i, j, k, t;
|
|
|
|
|
for (i=0; i<n-1; i++)
|
|
|
|
|
{
|
|
|
|
|
k = i;
|
|
|
|
|
for (j=i+1; j<n; j++)
|
|
|
|
|
{
|
|
|
|
|
if (stu[j].num < stu[k].num) k = j;
|
|
|
|
|
}
|
|
|
|
|
if (k != i)
|
|
|
|
|
{
|
|
|
|
|
for (t=0; t<m; t++)
|
|
|
|
|
{
|
|
|
|
|
SwapFloat(&stu[k].score[t], &stu[i].score[t]);
|
|
|
|
|
}
|
|
|
|
|
SwapFloat(&stu[k].sum, &stu[i].sum);
|
|
|
|
|
SwapFloat(&stu[k].aver, &stu[i].aver);
|
|
|
|
|
SwapLong(&stu[k].num, &stu[i].num);
|
|
|
|
|
SwapChar(stu[k].name, stu[i].name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 15函数功能: 打印学生成绩
|
|
|
|
|
void PrintScore(STU stu[], int n, int m)
|
|
|
|
|
{
|
|
|
|
|
int i, j;
|
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
|
{
|
|
|
|
|
printf("%ld\t%s\t", stu[i].num, stu[i].name);
|
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
|
{
|
|
|
|
|
printf("%.0f\t", stu[i].score[j]);
|
|
|
|
|
}
|
|
|
|
|
printf("%.0f\t%.0f\n", stu[i].sum, stu[i].aver);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//16
|
|
|
|
|
void StatisticAnalysis(STU stu[], int n, int m)
|
|
|
|
|
{
|
|
|
|
|
int i, j, t[6];
|
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
|
{
|
|
|
|
|
printf("For course %d:\n", j+1);
|
|
|
|
|
memset(t, 0, sizeof(t));
|
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
|
{
|
|
|
|
|
if (stu[i].score[j]>=0 && stu[i].score[j]<60)t[0]++;
|
|
|
|
|
else if (stu[i].score[j]<70) t[1]++;
|
|
|
|
|
else if (stu[i].score[j]<80) t[2]++;
|
|
|
|
|
else if (stu[i].score[j]<90) t[3]++;
|
|
|
|
|
else if (stu[i].score[j]<100) t[4]++;
|
|
|
|
|
else if (stu[i].score[j] == 100) t[5]++;
|
|
|
|
|
}
|
|
|
|
|
for (i=0; i<=5; i++)
|
|
|
|
|
{
|
|
|
|
|
if (i==0) printf("<60\t%d\t%.2f%%\n",t[i],(float)t[i]/n*100);
|
|
|
|
|
else if (i==5) printf("%d\t%d\t%.2f%%\n",
|
|
|
|
|
(i+5)*10,t[i],(float)t[i]/n*100);
|
|
|
|
|
else printf("%d-%d\t%d\t%.2f%%\n",
|
|
|
|
|
(i+5)*10, (i+5)*10+9, t[i], (float)t[i]/n*100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//17从文件中读取学生的学号、姓名及成绩等信息写入到结构体数组stu中
|
|
|
|
|
void ReadfromFile(STU stu[],int *n, int *m)
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
int i, j;
|
|
|
|
|
if ((fp = fopen("student.txt","r")) == NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("Failure to open score.txt!\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
fscanf(fp, "%d\t%d", n, m); // 从文件中读出学生人数和课程门数
|
|
|
|
|
for (i=0; i<*n; i++) //学生人数保存在n指向的存储单元
|
|
|
|
|
{
|
|
|
|
|
fscanf(fp, "%10ld", &stu[i].num);
|
|
|
|
|
fscanf(fp, "%10s", stu[i].name);
|
|
|
|
|
for (j=0; j<*m; j++)//课程门数保存在m指向的存储单元
|
|
|
|
|
{
|
|
|
|
|
fscanf(fp, "%10f", &stu[i].score[j]); //不能用%10.0f
|
|
|
|
|
}
|
|
|
|
|
fscanf(fp, "%10f%10f", &stu[i].sum, &stu[i].aver);//不能用%10.0f
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
````
|