|
|
@ -1,5 +1,3 @@
|
|
|
|
// 头文件的使用
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include<stdio.h>
|
|
|
|
#include<stdio.h>
|
|
|
|
#include<stdlib.h>
|
|
|
|
#include<stdlib.h>
|
|
|
|
#include<string.h>
|
|
|
|
#include<string.h>
|
|
|
@ -12,7 +10,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
// 结构体的定义
|
|
|
|
// 结构体的定义
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct student{
|
|
|
|
typedef struct student {
|
|
|
|
long num; //每个学生的学号
|
|
|
|
long num; //每个学生的学号
|
|
|
|
char name[MAX_LEN]; //每个学生的姓名
|
|
|
|
char name[MAX_LEN]; //每个学生的姓名
|
|
|
|
float score[COURSE_NUM]; //每个学生COURSE_NUM门功课的成绩
|
|
|
|
float score[COURSE_NUM]; //每个学生COURSE_NUM门功课的成绩
|
|
|
@ -23,75 +21,75 @@ typedef struct student{
|
|
|
|
// 函数声明
|
|
|
|
// 函数声明
|
|
|
|
|
|
|
|
|
|
|
|
int Menu(void); //菜单显示、用户输入函数
|
|
|
|
int Menu(void); //菜单显示、用户输入函数
|
|
|
|
void ReadScore(STU stu[],int n,int m); //学生成绩录入函数
|
|
|
|
void ReadScore(STU stu[], int n, int m); //学生成绩录入函数
|
|
|
|
void AverSumofEveryStudent(STU stu[],int n,int m); //求每个学生总分和平均分的函数
|
|
|
|
void AverSumofEveryStudent(STU stu[], int n, int m); //求每个学生总分和平均分的函数
|
|
|
|
void AverSumofEveryCourse(STU stu[],int n,int m); //求每门课程总分和平均分的函数
|
|
|
|
void AverSumofEveryCourse(STU stu[], int n, int m); //求每门课程总分和平均分的函数
|
|
|
|
void SortbyScore(STU stu[],int n,int m,int (*compare)(float a,float b)); //选择法将数组 sum 的元素排序的函数
|
|
|
|
void SortbyScore(STU stu[], int n, int m, int (*compare)(float a, float b)); //选择法将数组 sum 的元素排序的函数
|
|
|
|
int Ascending(float a,float b); //使数据按升序排序的函数
|
|
|
|
int Ascending(float a, float b); //使数据按升序排序的函数
|
|
|
|
int Descending(float a,float b); //使数据按降序排序的函数
|
|
|
|
int Descending(float a, float b); //使数据按降序排序的函数
|
|
|
|
void SwapFloat(float *x,float *y); //用于交换两个单精度浮点数的函数
|
|
|
|
void SwapFloat(float* x, float* y); //用于交换两个单精度浮点数的函数
|
|
|
|
void SwapLong(long *x,long *y); //用于交换两个长整型数据的函数
|
|
|
|
void SwapLong(long* x, long* y); //用于交换两个长整型数据的函数
|
|
|
|
void SwapChar(char x[],char y[]); //用于交换两个字符串的函数
|
|
|
|
void SwapChar(char x[], char y[]); //用于交换两个字符串的函数
|
|
|
|
void AsSortbyNum(STU stu[],int n,int m); //选择法将数组 num 的元素值从低到高排序的函数
|
|
|
|
void AsSortbyNum(STU stu[], int n, int m); //选择法将数组 num 的元素值从低到高排序的函数
|
|
|
|
void SortbyName(STU stu[],int n,int m); //交换法实现字符串按字典顺序排序的函数
|
|
|
|
void SortbyName(STU stu[], int n, int m); //交换法实现字符串按字典顺序排序的函数
|
|
|
|
void SearchbyNum(STU stu[],int n,int m); //按学号查找学生成绩并显示查找结果的函数
|
|
|
|
void SearchbyNum(STU stu[], int n, int m); //按学号查找学生成绩并显示查找结果的函数
|
|
|
|
void SearchbyName(STU stu[],int n,int m); //按姓名的字典顺序排出成绩表的函数
|
|
|
|
void SearchbyName(STU stu[], int n, int m); //按姓名的字典顺序排出成绩表的函数
|
|
|
|
void StatisticAnalysis(STU stu[],int n,int m); //统计各分数段的学生人数及所占的百分比的函数
|
|
|
|
void StatisticAnalysis(STU stu[], int n, int m); //统计各分数段的学生人数及所占的百分比的函数
|
|
|
|
void PrintScore(STU stu[],int n,int m); //打印学生成绩的函数
|
|
|
|
void PrintScore(STU stu[], int n, int m); //打印学生成绩的函数
|
|
|
|
void WritetoFile(STU record[],int n,int m); //输出学生信息到文件中的函数
|
|
|
|
void WritetoFile(STU record[], int n, int m); //输出学生信息到文件中的函数
|
|
|
|
void ReadfromFile(STU record[],int *n,int *m); //从文件中读取学生信息的函数
|
|
|
|
void ReadfromFile(STU record[], int* n, int* m); //从文件中读取学生信息的函数
|
|
|
|
|
|
|
|
|
|
|
|
// 主函数
|
|
|
|
// 主函数
|
|
|
|
int main(void)
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
char ch;
|
|
|
|
char ch;
|
|
|
|
int n = 0,m = 0;
|
|
|
|
int n = 0, m = 0;
|
|
|
|
STU stu[STU_NUM];
|
|
|
|
STU stu[STU_NUM];
|
|
|
|
printf("Input student number(n<%d):",STU_NUM);
|
|
|
|
printf("Input student number(n<%d):", STU_NUM);
|
|
|
|
scnaf("%d",&n);
|
|
|
|
scanf("%d", &n);
|
|
|
|
printf("Input course number(m<=%d):",COURSE_NUM);
|
|
|
|
printf("Input course number(m<=%d):", COURSE_NUM);
|
|
|
|
scanf("%d",&m);
|
|
|
|
scanf("%d", &m);
|
|
|
|
while(1)
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ch = Menu(); //显示菜单,并读取用户输入
|
|
|
|
ch = Menu(); //显示菜单,并读取用户输入
|
|
|
|
switch(ch)
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
case 1:ReadScore(stu,n,m);
|
|
|
|
case 1:ReadScore(stu, n, m);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 2:AverSumofEveryCourse(stu,n,m);
|
|
|
|
case 2:AverSumofEveryCourse(stu, n, m);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 3:AverSumofEveryStudent(stu,n,m);
|
|
|
|
case 3:AverSumofEveryStudent(stu, n, m);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 4:SortbyScore(stu,n,m,Descending);
|
|
|
|
case 4:SortbyScore(stu, n, m, Descending);
|
|
|
|
printf("\nSort in descending order by score:\n");
|
|
|
|
printf("\nSort in descending order by score:\n");
|
|
|
|
PrintScore(stu,n,m);
|
|
|
|
PrintScore(stu, n, m);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 5:SortbyScore(stu,n,m,Ascending);
|
|
|
|
case 5:SortbyScore(stu, n, m, Ascending);
|
|
|
|
printf("\nSort in ascending order by number:\n");
|
|
|
|
printf("\nSort in ascending order by number:\n");
|
|
|
|
PrintScore(stu,n,m);
|
|
|
|
PrintScore(stu, n, m);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 6:AsSortbyNum(stu,n,m);
|
|
|
|
case 6:AsSortbyNum(stu, n, m);
|
|
|
|
printf("\nSort in ascending order by number:\n");
|
|
|
|
printf("\nSort in ascending order by number:\n");
|
|
|
|
PrintScore(stu,n,m);
|
|
|
|
PrintScore(stu, n, m);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 7:SortbyName(stu,n,m);
|
|
|
|
case 7:SortbyName(stu, n, m);
|
|
|
|
printf("\nSort in dictionary order by name:\n");
|
|
|
|
printf("\nSort in dictionary order by name:\n");
|
|
|
|
PrintScore(stu,n,m);
|
|
|
|
PrintScore(stu, n, m);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 8:SearchbyNum(stu,n,m);
|
|
|
|
case 8:SearchbyNum(stu, n, m);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 9:SearchbyName(stu,n,m);
|
|
|
|
case 9:SearchbyName(stu, n, m);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 10:StatisticAnalysis(stu,n,m);
|
|
|
|
case 10:StatisticAnalysis(stu, n, m);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 11:PrintScore(stu,n,m);
|
|
|
|
case 11:PrintScore(stu, n, m);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 12:WritetoFile(stu,n,m);
|
|
|
|
case 12:WritetoFile(stu, n, m);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 13:ReadfromFile(stu,&n,&m);
|
|
|
|
case 13:ReadfromFile(stu, &n, &m);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 0: printf("Are you sure you want to exit?(Y/N):");
|
|
|
|
case 0: printf("Are you sure you want to exit?(Y/N):");
|
|
|
|
printf("End of program!);
|
|
|
|
printf("End of program!");
|
|
|
|
exit(0);
|
|
|
|
exit(0);
|
|
|
|
default:printf("Input error!");
|
|
|
|
default:printf("Input error!");
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -119,96 +117,96 @@ int Menu(void)
|
|
|
|
printf("13 Read from a file\n");
|
|
|
|
printf("13 Read from a file\n");
|
|
|
|
printf("0 Exit\n");
|
|
|
|
printf("0 Exit\n");
|
|
|
|
printf("Please Input your choice:");
|
|
|
|
printf("Please Input your choice:");
|
|
|
|
scanf("%d",&itemSelected); //读入用户输入
|
|
|
|
scanf("%d", &itemSelected); //读入用户输入
|
|
|
|
return itemSelected;
|
|
|
|
return itemSelected;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 函数功能:输入 n 个学生的 m 门课成绩
|
|
|
|
// 函数功能:输入 n 个学生的 m 门课成绩
|
|
|
|
void ReadScore(STU stu[],int n,int m)
|
|
|
|
void ReadScore(STU stu[], int n, int m)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
int i, j;
|
|
|
|
printf("Input student's ID ,name and score:\n");
|
|
|
|
printf("Input student's ID ,name and score:\n");
|
|
|
|
for(i=0,i<n;i++) //输入n个同学
|
|
|
|
for (i = 0; i < n; i++) //输入n个同学
|
|
|
|
{
|
|
|
|
{
|
|
|
|
scanf("%ld%s",&stu[i].num,stu[i].name);
|
|
|
|
scanf("%ld%s", &stu[i].num, stu[i].name);
|
|
|
|
for(j=0;j<m;j++) //输入m门成绩
|
|
|
|
for (j = 0; j < m; j++) //输入m门成绩
|
|
|
|
scanf("%f",&stu[i].score[j]);
|
|
|
|
scanf("%f", &stu[i].score[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//函数功能:计算每个学生各门课程的总分和平均分
|
|
|
|
//函数功能:计算每个学生各门课程的总分和平均分
|
|
|
|
void AverSumofEveryStudent(STU stu[],int n,int m)
|
|
|
|
void AverSumofEveryStudent(STU stu[], int n, int m)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
int i, j;
|
|
|
|
for(i=0;i<n;i++)
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
stu[i].sum = 0; //初始化每个学生总分为0
|
|
|
|
stu[i].sum = 0; //初始化每个学生总分为0
|
|
|
|
for(j=0;j<m;j++)
|
|
|
|
for (j = 0; j < m; j++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
stu[i].sum = stu[i].sum + stu[i].score[j]; //累加分数
|
|
|
|
stu[i].sum = stu[i].sum + stu[i].score[j]; //累加分数
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stu[i].aver = m>0 ? stu[i].sum /m : -1;
|
|
|
|
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);
|
|
|
|
printf("student %d: sum = %.0f,aver = %.0f\n", i + 1, stu[i].sum, stu[i].aver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//函数功能:计算每门课程的总分和平均分
|
|
|
|
//函数功能:计算每门课程的总分和平均分
|
|
|
|
void AverSumofEveryCourse(STU stu[],int n,int m)
|
|
|
|
void AverSumofEveryCourse(STU stu[], int n, int m)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
int i, j;
|
|
|
|
float sum[COURSE_NUM],aver[COURSE_NUM];
|
|
|
|
float sum[COURSE_NUM], aver[COURSE_NUM];
|
|
|
|
for(j=0;j<m;j++)
|
|
|
|
for (j = 0; j < m; j++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
sum[j] = 0;
|
|
|
|
sum[j] = 0;
|
|
|
|
for(i=0;i<n;i++)
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
sum[j]+= stu[i].score[j]; //计算n个同学每门课程的总分
|
|
|
|
sum[j] += stu[i].score[j]; //计算n个同学每门课程的总分
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aver[j] = sum[j]/i;
|
|
|
|
aver[j] = sum[j] / i;
|
|
|
|
printf("course %d: sum = %.0f,aver = %.0f\n",j+1, sum[j], aver[j]);
|
|
|
|
printf("course %d: sum = %.0f,aver = %.0f\n", j + 1, sum[j], aver[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//函数功能:按选择法将数组 sum 的元素值排序
|
|
|
|
//函数功能:按选择法将数组 sum 的元素值排序
|
|
|
|
voidSortbyScore(STU stu[],int n,int m,int(*compare)(float a,float b))
|
|
|
|
void SortbyScore(STU stu[], int n, int m, int(*compare)(float a, float b))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int i, j, k, t;
|
|
|
|
int i, j, k, t;
|
|
|
|
for (i=0; i<n-1; i++)
|
|
|
|
for (i = 0; i < n - 1; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
k = i;
|
|
|
|
k = i;
|
|
|
|
for (j=i+1; j<n; j++)
|
|
|
|
for (j = i + 1; j < n; j++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if ((*compare)(stu[j].sum, stu[k].sum)) k = j;
|
|
|
|
if ((*compare)(stu[j].sum, stu[k].sum)) k = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (k != i)
|
|
|
|
if (k != i)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
for (t=0; t<m; t++) //交换m门课程的成绩
|
|
|
|
for (t = 0; t < m; t++) //交换m门课程的成绩
|
|
|
|
{
|
|
|
|
{
|
|
|
|
SwapFloat(&stu[k].score[t], &stu[i].score[t]);
|
|
|
|
SwapFloat(&stu[k].score[t], &stu[i].score[t]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SwapFloat(&stu[k].sum, &stu[i].sum); //交换总分
|
|
|
|
SwapFloat(&stu[k].sum, &stu[i].sum); //交换总分
|
|
|
|
SwapFloat(&stu[k].aver, &stu[i].aver); //交换平均分
|
|
|
|
SwapFloat(&stu[k].aver, &stu[i].aver); //交换平均分
|
|
|
|
SwapLong(&stu[k].num, &stu[i].num); //交换学号
|
|
|
|
SwapLong(&stu[k].num, &stu[i].num); //交换学号
|
|
|
|
SwapChar(stu[k].name, stu[i].name); //交换姓名
|
|
|
|
SwapChar(stu[k].name, stu[i].name); //交换姓名
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//使数据按升序排序
|
|
|
|
//使数据按升序排序
|
|
|
|
int Ascending(float a,float b)
|
|
|
|
int Ascending(float a, float b)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return a < b; //这样比较决定了按升序排序,如果a<b,则交换
|
|
|
|
return a < b; //这样比较决定了按升序排序,如果a<b,则交换
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//使数据按降序排序
|
|
|
|
//使数据按降序排序
|
|
|
|
int Descending(float a,float b)
|
|
|
|
int Descending(float a, float b)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return a > b; // 这样比较决定了按降序排序,如果 a>b,则交换
|
|
|
|
return a > b; // 这样比较决定了按降序排序,如果 a>b,则交换
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//交换两个单精度浮点型数据
|
|
|
|
//交换两个单精度浮点型数据
|
|
|
|
void SwapFloat(float *x,float *y)
|
|
|
|
void SwapFloat(float* x, float* y)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
float temp;
|
|
|
|
float temp;
|
|
|
|
temp = *x;
|
|
|
|
temp = *x;
|
|
|
@ -217,7 +215,7 @@ void SwapFloat(float *x,float *y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//交换两个长整型数据
|
|
|
|
//交换两个长整型数据
|
|
|
|
void SwapLong(long *x,long *y)
|
|
|
|
void SwapLong(long* x, long* y)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
long temp;
|
|
|
|
long temp;
|
|
|
|
temp = *x;
|
|
|
|
temp = *x;
|
|
|
@ -226,7 +224,7 @@ void SwapLong(long *x,long *y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//交换两个字符串
|
|
|
|
//交换两个字符串
|
|
|
|
void SwapChar(char x[],chary[])
|
|
|
|
void SwapChar(char x[], char y[])
|
|
|
|
{
|
|
|
|
{
|
|
|
|
char temp[MAX_LEN];
|
|
|
|
char temp[MAX_LEN];
|
|
|
|
strcpy(temp, x);
|
|
|
|
strcpy(temp, x);
|
|
|
@ -235,132 +233,131 @@ void SwapChar(char x[],chary[])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//函数功能:按选择法将数组 num 的元素从低到高排序
|
|
|
|
//函数功能:按选择法将数组 num 的元素从低到高排序
|
|
|
|
void AsSortbyNum(STU stu[],int n,int m)
|
|
|
|
void AsSortbyNum(STU stu[], int n, int m)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int i, j, k, t;
|
|
|
|
int i, j, k, t;
|
|
|
|
for (i=0; i<n-1; i++)
|
|
|
|
for (i = 0; i < n - 1; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int i, j, k, t;
|
|
|
|
|
|
|
|
k = i;
|
|
|
|
k = i;
|
|
|
|
for (j=i+1; j<n; j++)
|
|
|
|
for (j = i + 1; j < n; j++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (stu[j].num < stu[k].num) k = j;
|
|
|
|
if (stu[j].num < stu[k].num) k = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (k != i)
|
|
|
|
if (k != i)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
for (t=0; t<m; t++) //交换m门课程的成绩
|
|
|
|
for (t = 0; t < m; t++) //交换m门课程的成绩
|
|
|
|
{
|
|
|
|
{
|
|
|
|
SwapFloat(&stu[k].score[t], &stu[i].score[t]);
|
|
|
|
SwapFloat(&stu[k].score[t], &stu[i].score[t]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SwapFloat(&stu[k].sum, &stu[i].sum); //交换总分
|
|
|
|
SwapFloat(&stu[k].sum, &stu[i].sum); //交换总分
|
|
|
|
SwapFloat(&stu[k].aver, &stu[i].aver); //交换平均分
|
|
|
|
SwapFloat(&stu[k].aver, &stu[i].aver); //交换平均分
|
|
|
|
SwapLong(&stu[k].num, &stu[i].num); //交换学号
|
|
|
|
SwapLong(&stu[k].num, &stu[i].num); //交换学号
|
|
|
|
SwapChar(stu[k].name, stu[i].name); //交换姓名
|
|
|
|
SwapChar(stu[k].name, stu[i].name); //交换姓名
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//函数功能:交换法实现字符串按字典顺序排序
|
|
|
|
//函数功能:交换法实现字符串按字典顺序排序
|
|
|
|
void SortbyName(STU stu[],int n,int m)
|
|
|
|
void SortbyName(STU stu[], int n, int m)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int i, j, t;
|
|
|
|
int i, j, t;
|
|
|
|
for (i=0; i<n-1; i++)
|
|
|
|
for (i = 0; i < n - 1; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
for (j = i+1; j<n; j++)
|
|
|
|
for (j = i + 1; j < n; j++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (strcmp(stu[j].name, stu[i].name) < 0)
|
|
|
|
if (strcmp(stu[j].name, stu[i].name) < 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
for (t=0; t<m; t++) //交换m门课程的成绩
|
|
|
|
for (t = 0; t < m; t++) //交换m门课程的成绩
|
|
|
|
{
|
|
|
|
{
|
|
|
|
SwapFloat(&stu[i].score[t], &stu[j].score[t]);
|
|
|
|
SwapFloat(&stu[i].score[t], &stu[j].score[t]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SwapFloat(&stu[i].sum, &stu[j].sum); //交换总分
|
|
|
|
SwapFloat(&stu[i].sum, &stu[j].sum); //交换总分
|
|
|
|
SwapFloat(&stu[i].aver, &stu[j].aver); //交换平均分
|
|
|
|
SwapFloat(&stu[i].aver, &stu[j].aver); //交换平均分
|
|
|
|
SwapLong(&stu[i].num, &stu[j].num); //交换学号
|
|
|
|
SwapLong(&stu[i].num, &stu[j].num); //交换学号
|
|
|
|
SwapChar(stu[i].name, stu[j].name); //交换姓名
|
|
|
|
SwapChar(stu[i].name, stu[j].name); //交换姓名
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//函数功能:按学号查找学生成绩并显示查找结果
|
|
|
|
//函数功能:按学号查找学生成绩并显示查找结果
|
|
|
|
void SearchbyNum(STU stu[],int n,int m)
|
|
|
|
void SearchbyNum(STU stu[], int n, int m)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
long number;
|
|
|
|
long number;
|
|
|
|
int i,j;
|
|
|
|
int i, j;
|
|
|
|
printf("Input the number you want to search:");
|
|
|
|
printf("Input the number you want to search:");
|
|
|
|
scanf("%ld",&number);
|
|
|
|
scanf("%ld", &number);
|
|
|
|
for(i=0;i<n;i++)
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
if(stu[i].num==number)
|
|
|
|
if (stu[i].num == number)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
printf("\n");
|
|
|
|
printf("\n");
|
|
|
|
for(j=0;j<m;j++)
|
|
|
|
for (j = 0; j < m; j++)
|
|
|
|
printf("%d\t",stu[i].score[j]);
|
|
|
|
printf("%d\t", stu[i].score[j]);
|
|
|
|
printf("\n");
|
|
|
|
printf("\n");
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(i==n)
|
|
|
|
if (i == n)
|
|
|
|
printf("\nNot found!\n");
|
|
|
|
printf("\nNot found!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//函数功能:按姓名查找学生成绩并显示查找结果
|
|
|
|
//函数功能:按姓名查找学生成绩并显示查找结果
|
|
|
|
void SearchbyName(STU stu[],int n,int m)
|
|
|
|
void SearchbyName(STU stu[], int n, int m)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
char x[MAX_LEN];
|
|
|
|
char x[MAX_LEN];
|
|
|
|
int i,j;
|
|
|
|
int i, j;
|
|
|
|
printf("Input the name you want to search:");
|
|
|
|
printf("Input the name you want to search:");
|
|
|
|
scanf("%s", x);
|
|
|
|
scanf("%s", x);
|
|
|
|
for(i=0;i<n;i++)
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(strcmp(stu[i].name,x)==0)
|
|
|
|
if (strcmp(stu[i].name, x) == 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
printf("%ld\t%s\t",stu[i].num,stu[i].name);
|
|
|
|
printf("%ld\t%s\t", stu[i].num, stu[i].name);
|
|
|
|
for(j=0;j<m;j++)
|
|
|
|
for (j = 0; j < m; j++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
printf("%.0f\t",stu[i].score[j]);
|
|
|
|
printf("%.0f\t", stu[i].score[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf(".0f\t%.0f\n",stu[i].sum,stu[i].aver);
|
|
|
|
printf(".0f\t%.0f\n", stu[i].sum, stu[i].aver);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\nNot found!\n);
|
|
|
|
printf("\nNot found!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//函数功能:统计各分数段的学生人数及所占的百分比
|
|
|
|
//函数功能:统计各分数段的学生人数及所占的百分比
|
|
|
|
void StatisticAnalysis(STU stu[],int n,int m)
|
|
|
|
void StatisticAnalysis(STU stu[], int n, int m)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int i,j,t[6];
|
|
|
|
int i, j, t[6];
|
|
|
|
for(j=0;j<m;j++)
|
|
|
|
for (j = 0; j < m; j++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
printf("For course %d:\n",j+1);
|
|
|
|
printf("For course %d:\n", j + 1);
|
|
|
|
memset(t,0,sizeof(t)); //将数组 t 的全部元素初始化为0
|
|
|
|
memset(t, 0, sizeof(t)); //将数组 t 的全部元素初始化为0
|
|
|
|
for(i=0;i<n;i++)
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(stu[i].score[j]>=0&&stu[i].score[j]<60) t[0]++;
|
|
|
|
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] < 70) t[1]++;
|
|
|
|
else if(stu[i].score[j]<80) t[2]++;
|
|
|
|
else if (stu[i].score[j] < 80) t[2]++;
|
|
|
|
else if(stu[i].score[j]<90) t[3]++;
|
|
|
|
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[4]++;
|
|
|
|
else if(stu[i].score[j]==100) t[5]++;
|
|
|
|
else if (stu[i].score[j] == 100) t[5]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i=0;i<=5;i++)
|
|
|
|
for (i = 0; i <= 5; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(i==0) printf("<60\t%d\t%.2f%%\n",t[i],(float)t[i]/n*100);
|
|
|
|
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 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);
|
|
|
|
else printf("%d-%d\t%d\t%.2f%%\n", (i + 5) * 10, (i + 5) * 10 + 9, t[i], (float)t[i] / n * 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//函数功能:打印学生成绩
|
|
|
|
//函数功能:打印学生成绩
|
|
|
|
void PrintScore(STU stu[],int n,int m)
|
|
|
|
void PrintScore(STU stu[], int n, int m)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
int i, j;
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
printf("%ld\t%s\t", stu[i].num, stu[i].name);
|
|
|
|
printf("%ld\t%s\t", stu[i].num, stu[i].name);
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
for (j = 0; j < m; j++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
printf("%.0f\t", stu[i].score[j]);
|
|
|
|
printf("%.0f\t", stu[i].score[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -369,21 +366,21 @@ void PrintScore(STU stu[],int n,int m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//输出 n 个学生的学号、姓名及 m 门课程的成绩到文件 student.txt 中
|
|
|
|
//输出 n 个学生的学号、姓名及 m 门课程的成绩到文件 student.txt 中
|
|
|
|
void WritetoFile(STU stu[],int n,int m)
|
|
|
|
void WritetoFile(STU stu[], int n, int m)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
FILE* fp;
|
|
|
|
int i, j;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
|
|
|
|
if((fp = fopen("student.txt","w")) == NULL)
|
|
|
|
if ((fp = fopen("student.txt", "w")) == NULL)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
printf("Failure to open score.txt!\n");
|
|
|
|
printf("Failure to open score.txt!\n");
|
|
|
|
exit(0);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(fp, "%d\t%d\n", n, m); //将学生人数和课程门数写入文件
|
|
|
|
fprintf(fp, "%d\t%d\n", n, m); //将学生人数和课程门数写入文件
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fprintf(fp, "%10ld%10s", stu[i].num, stu[i].name);
|
|
|
|
fprintf(fp, "%10ld%10s", stu[i].num, stu[i].name);
|
|
|
|
for (j=0; j<m; j++)
|
|
|
|
for (j = 0; j < m; j++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fprintf(fp, "%10.0f", stu[i].score[j]);
|
|
|
|
fprintf(fp, "%10.0f", stu[i].score[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -393,21 +390,21 @@ void WritetoFile(STU stu[],int n,int m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//从文件中读取学生的学号、姓名及成绩等信息写入到结构体 stu 中
|
|
|
|
//从文件中读取学生的学号、姓名及成绩等信息写入到结构体 stu 中
|
|
|
|
void ReadfromFile(STU stu[],int *n,int *m)
|
|
|
|
void ReadfromFile(STU stu[], int* n, int* m)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
FILE* fp;
|
|
|
|
int i, j;
|
|
|
|
int i, j;
|
|
|
|
if ((fp = fopen("student.txt","r")) == NULL)
|
|
|
|
if ((fp = fopen("student.txt", "r")) == NULL)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
printf("Failure to open score.txt!\n");
|
|
|
|
printf("Failure to open score.txt!\n");
|
|
|
|
exit(0);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fscanf(fp, "%d\t%d", n, m); //从文件中读出学生人数,课程门数
|
|
|
|
fscanf(fp, "%d\t%d", n, m); //从文件中读出学生人数,课程门数
|
|
|
|
for (i=0; i<*n; i++) //学生人数保存在n指向的储存单元
|
|
|
|
for (i = 0; i < *n; i++) //学生人数保存在n指向的储存单元
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fscanf(fp, "%10ld", &stu[i].num);
|
|
|
|
fscanf(fp, "%10ld", &stu[i].num);
|
|
|
|
fscanf(fp, "%10s", stu[i].name);
|
|
|
|
fscanf(fp, "%10s", stu[i].name);
|
|
|
|
for (j=0; j<*m; j++) //课程门数保存在m指向的储存单元
|
|
|
|
for (j = 0; j < *m; j++) //课程门数保存在m指向的储存单元
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fscanf(fp, "%10f", &stu[i].score[j]); //不能用%10.0f
|
|
|
|
fscanf(fp, "%10f", &stu[i].score[j]); //不能用%10.0f
|
|
|
|
}
|
|
|
|
}
|
|
|
|