2015年计算机二级C语言上机操作题及答案(67)
发布时间:2011/7/20 13:14:43 来源:城市学习网 编辑:ziteng
1.填空题
请补充main函数,该函数的功能是:把一个二维字符数组每行字符川最大的字符拷贝到字符数组S中。
例如,如果str[3]={"adefj","ehfkn","opwxres"},则s="jnx"。
仅在横线上填入所编写的若干表达式或语句,勿改动函数中的其他任何内容。
#include
main()
{
int i = 0;
char *str[3] = {"adefj", "ehfkn", "opwxres"};
char **p;
char s[3];
___1___;
for (i=0; i<3; i++)
{
s[i] = *p[i];
while (*p[i])
{
if (s[i] < *p[i])
s[i] = *p[i];
___2___;
}
}
___3___;
printf(" new string \n");
puts(s);
}
答案:
第1处:p=str
第2处:p[i]++或++p[i]或p[i]+=1或p[i]=p[i]=p[i]+1
第3处:s[i]=’\0’或s[i]=0 [NextPage] 2.改错题
下列给定程序中,函数fun的功能是:应用递归算法求某数a的平方根。求平方根的迭代公式如下:
例如,2的平方根值为1.414214
请改正程序中的错误,使它能的出正确的结果.
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构.
#include
#include
/********found********/
fun(double a, double x0)
{
double x1, y;
x1 = (x0 + a/x0)/2.0;
/********found********/
if (abs(x1-x0) >= 1e-6)
y = fun(a, x1);
else
y = x1;
return y;
}
main()
{
double x;
printf("Enter x: ");
scanf("%lf", &x);
printf("The square root of %lf is %lf\n", x, fun(x, 1.0));
}
答案:
第1处:fun(double a, double x0)应改为 double fun (double a, double x0)
第2处:if (abs(x1-x0)>=1e-6)应改为 if(fabs(x1-x0)>=1e-6) [NextPage] 3.编程题
学生的记录有学号和成绩组成,N名学生的数据已在主函数中放入结构提函数组s中,请编写函数fun,它的功能是:函数返回指定学号的学生数据,指定的学号在主函数中输出.若没找到指定学号,在结构提变量中给学号置空串,给成绩置-1,作为函数值返回(用于字符串比较的函数是strcmp)。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#include
#define N 16
typedef struct
{
char num[10];
int s;
} STREC;
STREC fun ( STREC *a, char *b )
{
}
main ()
{
STREC s[N]= {{"GA005",85}, {"GA003",76}, {"GA002",69}, {"GA004",85},
{"GA001",91}, {"GA007",72}, {"GA008",64}, {"GA006",87},
{"GA015",85}, {"GA013",91}, {"GA012",64}, {"GA014",91},
{"GA011",77}, {"GA017",64}, {"GA018",64}, {"GA016",72}};
STREC h;
char m[10];
int i; FILE *out;
printf ( "The original data :\n" );
for (i=0; i {
if ( i%4==0 ) printf ("\n");
printf ("%s =", s[i]. num, s[i]. s);
}
printf ("\n\nEnter the number : ");
gets ( m );
h=fun ( s, m );
printf ( " The data : " );
printf ( "\n%s M\n", h . num, h . s );
printf ( "\n" );
out=fopen ("out.dat", "w");
h=fun ( s, "GA013" );
fprintf (out, "%s M\n", h . num, h . s);
fclose (out );
}
答案:
STREC fun (STREC *a, char *b)
{
int i;
STREC h;
For(i=0;iIf(strcmp(a[i].num, b)==0)
{
h=a[i];
break;
}
else
{
strcpy(h .num, “”);
h.s=-1;
}
return h;
}