2013计算机等级考试二级C语言试题及答案16
第一题:请补充函数fun,该函数的功能是比较字符串str1和str2的大小,并返回比较的结果。
例如:当str1=“abcd”,str2=”abc”时,fun函数返回”>”。
请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式和语句。#include
#include
#define N 80
char *fun(char *str1, char *str2)
{
char *p1 = str1, *p2 = str2;
while (*p1 && *p2)
{
if (___1___)
return "<";
if (___2___)
return ">";
p1++;
p2++;
}
if (*p1 == *p2)
return "==";
if (*p1 == ___3___)
return "<";
else
return ">";
}
main()
{
char str1[N], str2[N];
printf("Input str1:\n");
gets(str1);
printf("Input str2:\n");
gets(str2);
printf("\n*******the result********\n");
printf("\nstr1 %s str2", fun(str1, str2));
}
填空题参考答案:
第1处填空:*p1<*p2或*p2>*p1
第2处填空:*p1>*p2或*p2<*p1
第3处填空:’\0’或0
第二题:下列给定程序中,函数fun的功能是:比较两个字符串,将长的那个字符串的首地址作为函数值返回。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
/********found********/
double fun(char *s, char *t)
{
int s1 = 0, t1 = 0;
char *ss, *tt;
ss = s;
tt = t;
/********found********/
while (*ss)
{
s1++;
(*ss)++;
}
/********found********/
while (*tt)
{
t1++;
(*tt)++;
}
if (t1 > s1)
return t;
else
return s;
}
main()
{
char a[80], b[80];
printf("\nEnter a string : ");
gets(a);
printf("\nEnter a string again : ");
gets(b);
printf("\nThe longer is :\n\n%s\n", fun(a, b));
}
改错题参考答案:
第一处: double fun(char *s, char *t) 应改为 char *fun(char *s, char *t)
第二处: (*ss)++; 应改为 ss++;
第三处: (*tt)++; 应改为 tt++;
第三题:学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:把低于平均分的学生数据放在b 所指的数组中,低于平均分的学生人数通过形参n传回,平均分通过函数值返回。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写在若干语句。
#include
#define N 8
typedef struct
{
char num[10];
double s;
} STREC;
double fun ( STREC *a, STREC *b, int *n )
{
} www.Examda.CoM
main()
{
STREC s[N]={{"GA05",85}, {"GA03",76}, {"GA02",69}, {"GA04",85},
{"GA01",91}, {"GA07",72}, {"GA08",64}, {"GA06", 87}};
STREC h[N], t;FILE *out ;
int i, j, n;
double ave;
ave=fun ( s, h, &n );
printf ("The %d student data which is lower than %7.3f:\n", n, ave );
for (i=0; i
printf ("\n");
out=fopen ("out.dat","w");
fprintf (out, "%d\n%7.3f\n", n, ave);
for (i=0; i
{
t=h[i] ;
h[i]=h[j];
h[j]=t;
}
for(i=0;i
fclose (out );
}
编程题参考答案:
double fun (STREC *a, STREC *b, int *n)
{
double aver=0.0;
int i, j=0;
*n=0;
for (i=0; i
aver/=N;
for(i=0;i
b[j]=a[i];
(*n)++;
j++;
}
return aver;
}