2015年计算机二级C语言上机操作题及答案(12)
发布时间:2011/7/4 10:53:43 来源:城市学习网 编辑:ziteng
第12套
填空题
请补充fun函数,该函数的功能是字符串tt中的大写字母都改为对应的小写字母,其它字符不变.
例如,若输入"Are you come from Sichuan?则输出","are you come from sichuan?"
注意:部分源程序给出如下。
仅在横线上填入所编写的若干表达式或语句,勿改动函数中的其他任何内容。
试题程序:#include <stdio.h>
#include <string.h>
#include <conio.h>
char *fun(char tt[])
{
int i;
for(i=0;tt[i];i++)
{
if((tt[i]>=’A’)&&(___1___))
tt[i] += ___2___;
}
return (___3___);
}
main()
{
char tt[81];
printf("\nPlease enter a string: ");
gets(tt);
printf("\nThe result string is: \n%s", fun(tt));
}
第1处填空:tt[i]<=’z’或’z’>=tt[i]
第2处填空:32或’a’-‘A’
第3处填空:tt [NextPage] 改错题
下列给定程序中函数FUN的功能是:求出在字符串中最后一次出现的子符串的地址,通过函数值返回,在主函数中输出从此开始的字符串;若未找到,则函数值为NULL。例如,当字符串中的内容为abcdabfabcdx,t中的内容为ab时,输出结果应是:abcdx。当字符串中的内容为abcdabfabcdx,t中的内容为abd时,则程序输出未找到信息:not be found!。
请改正程序中的错误,使它能得出正确结果。
注意:不要改动MAIN函数,不得增行或删行,也不要更改程序的结构!
试题程序:#include <conio.h>
#include <stdio.h>
#include <string.h>
char *fun(char *s, char *t)
{
char *p, *r, *a;
/********found********/
a = NuLL;
while (*s)
{
p = s;
r = t;
while (*r)
/********found********/
if (r == p)
{
r++;
p++;
}
else
{
break;
}
if (*r == ’\0’)
a = s;
s++;
}
return a;
}
main()
{
char s[100], t[100], *p;
printf("\nplease enter string s:");
scanf("%s", s);
printf("\nplease enter substring t:");
scanf("%s", t);
p = fun(s, t);
if (p)
printf("\nthe result is:%s\n", p);
else
printf("\nnot found!\n");
}
第1处:a = NULL;应改为a=NULL;
第2处:if(r==p)应改为if(*r==*p) [NextPage] 编程题
下列程序定义了N×N的而维数组,并在主函数中自动赋值。请编写函数fun,函数的功能是:求出数组周边元素的平均值 并作为函数值返回给主函数中的s
则返回主程序后s 的值应为3.375.
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define N 5
double fun (int w[][N])
{
}
main()
{
int a[N][N]={0,1,2,7,9,1,9,7,4,5,2,3,8,3,1,4,5,6,8,2,5,9,1,4,1};
int i,j;
FILE *out;
double s;
printf("***** The array *****\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}
s=fun(a);
printf("***** THE RESULT *****\n");
printf("The sum is %lf\n",s);
out = fopen("out.dat", "w");
fprintf(out, "%lf", s);
fclose(out);
}
答案是:
double fun(int w[][N])
{
int i,j,k=0;
double s=0.0;
for(j=0;j<N;j++)
{
s+=w[0][j];
k++;
}
for(j=0;j<N;j++)
{
s+=w[N-1][j];
k++;
}
for(i=1;i<=N-2;i++)
{
s+=w[i][0];
k++;
}
for(i=1;i<=N-2;i++)
{
s+=w[i][N-1];
k+;
}
rturn s/=k;
}