|
|
|
@ -657,7 +657,7 @@ function ReadScore(STU stu[],int n,int m) do
|
|
|
|
|
int i,j;
|
|
|
|
|
printf "Input student's ID ,name and score:\n"
|
|
|
|
|
for from i=0 to n-1 do //输入n个同学
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
input stu[i].num,stu[i].name
|
|
|
|
|
for form j=0 to m-1 do //输入m门成绩
|
|
|
|
|
input stu[i].score[j]
|
|
|
|
@ -669,191 +669,162 @@ end
|
|
|
|
|
|
|
|
|
|
//函数功能:计算每个学生各门课程的总分和平均分
|
|
|
|
|
```
|
|
|
|
|
void AverSumofEveryStudent(STU stu[],int n,int m)
|
|
|
|
|
{
|
|
|
|
|
function AverSumofEveryStudent(STU stu[],int n,int m) do
|
|
|
|
|
int i,j;
|
|
|
|
|
for(i=0;i<n;i++)
|
|
|
|
|
{
|
|
|
|
|
for form i=0 to n-1 do
|
|
|
|
|
stu[i].sum = 0; //初始化每个学生总分为0
|
|
|
|
|
for(j=0;j<m;j++)
|
|
|
|
|
{
|
|
|
|
|
for from j=0 to m-1 do
|
|
|
|
|
stu[i].sum = stu[i].sum + stu[i].score[j]; //累加分数
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
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"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
//函数功能:计算每门课程的总分和平均分
|
|
|
|
|
```
|
|
|
|
|
void AverSumofEveryCourse(STU stu[],int n,int m)
|
|
|
|
|
{
|
|
|
|
|
function AverSumofEveryCourse(STU stu[],int n,int m) do
|
|
|
|
|
int i,j;
|
|
|
|
|
float sum[COURSE_NUM],aver[COURSE_NUM];
|
|
|
|
|
for(j=0;j<m;j++)
|
|
|
|
|
{
|
|
|
|
|
for from j=0 to m-1 do
|
|
|
|
|
sum[j] = 0;
|
|
|
|
|
for(i=0;i<n;i++)
|
|
|
|
|
{
|
|
|
|
|
for from i=0 to n-1 do
|
|
|
|
|
sum[j]+= stu[i].score[j]; //计算n个同学每门课程的总分
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
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"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
//函数功能:按选择法将数组 sum 的元素值排序
|
|
|
|
|
```
|
|
|
|
|
void SortbyScore(STU stu[],int n,int m,int(*compare)(float a,float b))
|
|
|
|
|
{
|
|
|
|
|
function SortbyScore(STU stu[],int n,int m,int(*compare)(float a,float b)) do
|
|
|
|
|
int i, j, k, t;
|
|
|
|
|
for (i=0; i<n-1; i++)
|
|
|
|
|
{
|
|
|
|
|
for from i=0 to n-2 do
|
|
|
|
|
k = i;
|
|
|
|
|
for (j=i+1; j<n; j++)
|
|
|
|
|
{
|
|
|
|
|
if ((*compare)(stu[j].sum, stu[k].sum)) k = j;
|
|
|
|
|
}
|
|
|
|
|
if (k != i)
|
|
|
|
|
{
|
|
|
|
|
for (t=0; t<m; t++) //交换m门课程的成绩
|
|
|
|
|
{
|
|
|
|
|
for from i+1 to n-1 do
|
|
|
|
|
if (*compare)(stu[j].sum, stu[k].sum) set k = j;
|
|
|
|
|
end
|
|
|
|
|
if k != i
|
|
|
|
|
for from t=0 to m-1 do //交换m门课程的成绩
|
|
|
|
|
SwapFloat(&stu[k].score[t], &stu[i].score[t]);
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
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); //交换姓名
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
//使数据按升序排序
|
|
|
|
|
```
|
|
|
|
|
int Ascending(float a,float b)
|
|
|
|
|
{
|
|
|
|
|
function Ascending(float a,float b) do
|
|
|
|
|
return a < b; //这样比较决定了按升序排序,如果a<b,则交换
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
//使数据按降序排序
|
|
|
|
|
```
|
|
|
|
|
int Descending(float a,float b)
|
|
|
|
|
{
|
|
|
|
|
function Descending(float a,float b) do
|
|
|
|
|
return a > b; // 这样比较决定了按降序排序,如果 a>b,则交换
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
//交换两个单精度浮点型数据
|
|
|
|
|
```
|
|
|
|
|
void SwapFloat(float *x,float *y)
|
|
|
|
|
{
|
|
|
|
|
function SwapFloat(float *x,float *y) do
|
|
|
|
|
float temp;
|
|
|
|
|
temp = *x;
|
|
|
|
|
*x = *y;
|
|
|
|
|
*y = temp;
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
//交换两个长整型数据
|
|
|
|
|
```
|
|
|
|
|
void SwapLong(long *x,long *y)
|
|
|
|
|
{
|
|
|
|
|
function SwapLong(long *x,long *y) do
|
|
|
|
|
long temp;
|
|
|
|
|
temp = *x;
|
|
|
|
|
*x = *y;
|
|
|
|
|
*y = temp;
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
//交换两个字符串
|
|
|
|
|
```
|
|
|
|
|
void SwapChar(char x[],char y[])
|
|
|
|
|
{
|
|
|
|
|
function SwapChar(char x[],char y[]) do
|
|
|
|
|
char temp[MAX_LEN];
|
|
|
|
|
strcpy(temp, x);
|
|
|
|
|
strcpy(x, y);
|
|
|
|
|
strcpy(y, temp);
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
//函数功能:按选择法将数组 num 的元素从低到高排序
|
|
|
|
|
```
|
|
|
|
|
void AsSortbyNum(STU stu[],int n,int m)
|
|
|
|
|
{
|
|
|
|
|
function AsSortbyNum(STU stu[],int n,int m) do
|
|
|
|
|
int i, j, k, t;
|
|
|
|
|
for (i=0; i<n-1; i++)
|
|
|
|
|
{
|
|
|
|
|
for from i=0 to n-2 do
|
|
|
|
|
int i, j, k, t;
|
|
|
|
|
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++) //交换m门课程的成绩
|
|
|
|
|
{
|
|
|
|
|
set k = i;
|
|
|
|
|
for from j=i+1 to n-1 do
|
|
|
|
|
if stu[j].num < stu[k].num set k = j;
|
|
|
|
|
end
|
|
|
|
|
if k != i do
|
|
|
|
|
for from t=0 to m-1 do //交换m门课程的成绩
|
|
|
|
|
SwapFloat(&stu[k].score[t], &stu[i].score[t]);
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
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); //交换姓名
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
//函数功能:交换法实现字符串按字典顺序排序
|
|
|
|
|
```
|
|
|
|
|
void SortbyName(STU stu[],int n,int m)
|
|
|
|
|
{
|
|
|
|
|
function SortbyName(STU stu[],int n,int m) do
|
|
|
|
|
int i, j, t;
|
|
|
|
|
for (i=0; i<n-1; i++)
|
|
|
|
|
{
|
|
|
|
|
for (j = i+1; j<n; j++)
|
|
|
|
|
{
|
|
|
|
|
if (strcmp(stu[j].name, stu[i].name) < 0)
|
|
|
|
|
{
|
|
|
|
|
for (t=0; t<m; t++) //交换m门课程的成绩
|
|
|
|
|
{
|
|
|
|
|
for from i=0 to n-2 do
|
|
|
|
|
for from j = i+1 to n-1 do
|
|
|
|
|
if strcmp(stu[j].name, stu[i].name) < 0 do
|
|
|
|
|
for from t=0 to m-1 do //交换m门课程的成绩
|
|
|
|
|
SwapFloat(&stu[i].score[t], &stu[j].score[t]);
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
SwapFloat(&stu[i].sum, &stu[j].sum); //交换总分
|
|
|
|
|
SwapFloat(&stu[i].aver, &stu[j].aver); //交换平均分
|
|
|
|
|
SwapLong(&stu[i].num, &stu[j].num); //交换学号
|
|
|
|
|
SwapChar(stu[i].name, stu[j].name); //交换姓名
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
//函数功能:按学号查找学生成绩并显示查找结果
|
|
|
|
|
```
|
|
|
|
|
void SearchbyNum(STU stu[],int n,int m)
|
|
|
|
|
{
|
|
|
|
|
function SearchbyNum(STU stu[],int n,int m) do
|
|
|
|
|
long number;
|
|
|
|
|
int i,j;
|
|
|
|
|
printf("Input the number you want to search:");
|
|
|
|
|
scanf("%ld",&number);
|
|
|
|
|
for(i=0;i<n;i++)
|
|
|
|
|
if(stu[i].num==number)
|
|
|
|
|
{
|
|
|
|
|
printf("\n");
|
|
|
|
|
for(j=0;j<m;j++)
|
|
|
|
|
printf("%d\t",stu[i].score[j]);
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf"Input the number you want to search:"
|
|
|
|
|
input number
|
|
|
|
|
forfrom i=0 to n-1 do
|
|
|
|
|
if stu[i].num==number do
|
|
|
|
|
printf"\n"
|
|
|
|
|
for from j=0 to m-1 do
|
|
|
|
|
printf "%d\t"
|
|
|
|
|
printf "\n"
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(i==n)
|
|
|
|
|
printf("\nNot found!\n");
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
if i==n
|
|
|
|
|
printf "\nNot found!\n"
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
//函数功能:按姓名查找学生成绩并显示查找结果
|
|
|
|
|