2015年计算机二级C语言上机操作题及答案(71)
发布时间:2011/7/20 13:18:27 来源:城市学习网 编辑:ziteng
(填空题)给定程序中,函数fun的功能是:将行参指针所指结构体数组中的三个元素按num成员进行升序排列。请勿改动主函数main和其他函数中的任何内容,仅在横线上填入所编写的若干表达式或语句。
#include
typedef struct
{
int num;
char name[10];
} PERSON;
void fun(PERSON ___1___)
{
___2___ 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;
fun(___3___);
printf("\nThe result is :\n");
for (i=0; i<3; i++)
printf("%d,%s\n", std[i].num, std[i].name);
}
填空题答案:
第一处:std[3] 或 std[] 或 *std
第二处:PERSON
第三处:std [NextPage] (改错题)下列给定程序中,函数fun的功能是:求k!(k〈13 〉,所求阶乘的值作为函数值返回。例如:若k=10,则应输出3628800。请改正程序中的错误,使它能得出正确的结果。注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
long fun(int k)
{
/********found********/
if k > 1
return (k*fun(k-1));
return 1;
}
main()
{
int k = 10;
printf("%d!=%ld\n", k, fun(k));
}
改错题答案:
第一处:if k>1 改为:if(k>1) [NextPage] (编程题)请编写函数fun,其功能是:将两个两位正整数a、b合并形成一个整数放在c中。合并的方式是:将a数的十位和各位树依次放在c树的十位和千位上,b树十位和各位树依次放在c树的白位和各位上。
例如:当a=45,b=12,调用该函数后,c=5142。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#include
void fun(int a, int b, long *c)
{
}
main()
{
int a,b;
long c;
FILE *out;
printf(" Input a, b: ");
scanf("%d%d", &a,&b);
fun(a,b,&c);
printf(" The result is :%ld\n", c);
out=fopen ("out.dat", "w");
for (a = 21; a < 51; a+=3)
{
fun(a, 109-a, &c);
fprintf(out, "%ld\n", c);
}
fclose (out );
}
编程题答案:
c=(a)1000+(b/10)*100+(a/10)*10+b;