2015年计算机二级C语言上机操作题及答案(10)
发布时间:2011/7/4 10:52:03 来源:城市学习网 编辑:ziteng
第10套
填空题
请补充main函数,该函数的功能是:从键盘键盘输入3个整数然后找出最大的数并输出。
例如,输入:12 45 43,输出为45
注意:部分源程序给出如下。
仅在横线上填入所编写的若干表达式或语句,勿改动函数中的其他任何内容。
试题程序:#include
#include
main()
{
int a, b, c, max;
printf("\nInput three numbers:\n");
scanf("%d,%d,%d", &a, &b, &c);
printf("The three numbers are:%d,%d,%d\n", a, b, c);
if (a > b)
___1___;
else
___2___;
if (max < c)
___3___;
printf("max=%d\n", max);
}
第1处填空:max=a
第2处填空:max=b
第3处填空:max=c [NextPage] 改错题
下列给定程序中,函数fun的功能是:将s所指字符串出现的t1所指子串全部替换成t2所指子字符串,错形成的新串放在w所指的数组中。在此处,要求t1和t2所指字符串的长度相同。例如,当s所指字符串的内容为abcdabfab,t1所指子串中的内容为ab,t2所指子串中的内容为99时,结果,在w所指的数组中的内容为99cd99f99。
请改正程序中的错误,使程序能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:#include
#include
#include
/********found********/
void fun(char *s, *t1, *t2, *w)
{
int i;
char *p, *r, *a;
strcpy(w, s);
while (*w)
{
p = w;
r = t1;
/********found********/
while (r)
if (*r == *p)
{
r++;
p++;
}
else
{
break;
}
if (*r == ’\0’)
{
a = w;
r = t2;
while (*r)
{
*a = *r;
a++;
r++;
}
w += strlen(t2);
}
else
{
w++;
}
}
}
main()
{
char s[100], t1[100], t2[100], w[100];
printf("\nPlease enter string s:");
scanf("%s", s);
printf("\nPlease enter substring t1:");
scanf("%s", t1);
printf("\nPlease enter substring t2:");
scanf("%s", t2);
if (strlen(t1) == strlen(t2))
{
fun(s, t1, t2, w);
printf("\nThe result is :%s\n", w);
}
else
{
printf("Error :strlen(t1)!=strlen(t2)\n");
}
}
第1处:void fun(char *s,*t1,*t2,*w)应改为void fun(char *s,char *t1,char *t2,char *w)
第2处:while(r)应改为while(*r) [NextPage] 编程题
编写函数fun,它的功能是:利用以下所示的简单迭代方法求方程式cos(x)-x=0的一个实根。
迭代步骤如下:
(1)取x1初值为0.0;
(2)x0=x1,把x1的值赋给x0;
(3)x1=cos(x0),求出一个新的x1;
(4)若x0-x1,的绝对值小于0.000001,则执行步骤(5),否则执行步骤(2);
(5)所求x1就是方程cos(x)-x=0的一个实根,作为函数值返回。
程序将输出结果Root=0.739085。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:#include
#include
#include
float fun()
{
}
main()
{
FILE *out;
float f = fun();
printf("Root=%f\n", f);
out = fopen("out.dat", "w");
fprintf(out, "%f", f);
fclose(out);
}
答案是:
float fun()
{
float x1=0.0,x0;
do
{
x0=x1;
x1=cos(x0);
}
while(fabs(x0-x1)>=le-6);
return x1;
}