问答题 给定程序中,函数fun的功能是:将形参指针所指结构体数组中的三个元素按num成员进行升序排列。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:源程序存放在 下的BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
#include<stdio.h>
typedef struct
{ int num;
char name[10];
} PERSON;
/**********found**********/
void fun(PERSON______)
{
/**********found**********/
______temp;
if(std[0].num>std[1].num)
{temp=std[0]; std[0]=std[1]; std[1]=temp;}
if(std[0].num>std[2].num)
{ temp=std[0]; std[0]=std[2]; std[2]=temp;}
if(std[1].num>std[2].num)
{temp=std[1]; std[1]=std[2]; std[2]=temp;}
}
main()
{PERSON std[]={5, "Zhanghu", 2, "WangLi", 6, "LinMin");
int i;
/**********found**********/
fun(______);
printf("/nThe result is:/n");
for(i=0; i<3; i++)
printf("%d, %s/n", std[i].num, std[i].name);
}
【正确答案】
【答案解析】(1)*std (2)PERSON (3)std
答案考生文件夹 [解析] fun函数的功能是将形参指针所指结构体数组中的三个元素进行排序。
第一空:fun函数的参数是指针,由“if(std[0].num>std[1].num)”可知形参名为std,故第一空处应为“void fun(PERSON*std)”。
第二空:temp变量用来进行交换时临时保存变量值,由“temp=std[0]; ”可知temp是PERSON结构体变量,故第二空处的temp结构体定义应为“PERSON temp; ”。
第三空:这里是调用fun函数,fun函数的参数是结构体变量指针,std是结构体数组,数组名相当于数组的首地址,故第三空处应为“fun(std); ”。
[考点] 结构体数组、循环的嵌套。