下列给定程序中,函数fun()的功能是:对N名学生的学习成绩,按从高到低的顺序找出前m(m≤10)名学生来,并将这些学生数据存放在一个动态分配的连续存储区中,此存储区的首地址作为函数值返回。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
# include<conio.h>
#define N 10
typedef struct ss
char num[10];
int order;
STU;
STU *fun(STU a[], iht m)
STU b[N], *tt;
int i,j,k;
【1】 ;
for(i=0; i<N; i++)
b[i]=a[i];
for (k=0;k<m;k++)
for (i=j=0;i<N;i++)
if ( 【2】 )
j=i;
tt [k]=b[j];
b[j].order=0;
return 【3】 ;
outresult (STU a[],FILE *pr)
int i;
for(i=0;i<N; i++)
fprintf(pf,"No=%s Mark=%d\n",
a[i].num,a[i].order);
fprintf(pf,"\n\n");
main()
STU [N]="A01",80,"A02",79,
"A03",66,"A04",82,"A05",87,
"A06",93,"A07",78,"A08",60,
"A09",85,"A10",73;
STU *p_order;
int i,m;
clrscr();
printf("*** The Origial data ***\n");
outresult(a, stdout);
printf("\nGive the numeber of the
students who have better score:");
scanf("%d",&m);
while (m>10)
printf("\nGive the number of the
studets who have better score:");
scanf("%d",&m);
p_order=fun(a,m);
printf("*** THE RESULT ***\n");
printf("*** The top students ***\n");
for(i=0; i<m; i++)
printf (" %s %d\n",
p_order[i].num,p_order[i].order);
free(p_order);
参考答案:[1] tt=(STU*)malloc(sizeof(STU)*m) [2] b[i].order>b[j].order [3] tt
解析: 填空1:tt是结构体STU型指针,声明时并没有进行初始化,所以,需要使用malloc()函数动态申请存储空间。动态申请存储空间的大小取决于函数的形参m。填空2:按成绩高低找出前m名学生的方法是,先假设第一名学生成绩最高,依次与其他学生的成绩进行比较,如果出现成绩更高者,则认为这名学生成绩最高,全部比较完后找到成绩第一名的学生,存入指针tt所指的结构体空间中,同时将这名同学的成绩置0,这样在其他同学中找第一名就相当于找到所有同学中的第二名,依此类推,进行m次循环,找到前m名学生。填空3:题目要求函数值返回动态分配的存储区的首地址,所以返回指针tt。