2015年计算机二级C语言上机操作题及答案(15)
发布时间:2011/7/4 10:58:26 来源:城市学习网 编辑:ziteng
第15套
填空题
给定程序的功能是判断字符ch是否与串str中的某个字符相同;若相同,什么也不做,若不同,则插在串的最后。
注意:部分源程序给出如下
请勿改动主函数main和其他函数中的任何内容,仅在横线上填如所编写的若干表达式或语句。
试题程序:
#include
#include
Void fun(char*str,char ch)
{ while( *str&&*str!=ch) str++;
If(*ste_1_ch)
{ str[0]=ch;
_2_=0;
}
}
Main()
{ char s[81], c;
Printf(“\nplease enter a string:\n”);gets(s);
Printf(“\nplease enter the character to search :”);
C=getchar();
Fun(_3_);
Printf(“\nThe result is%s\n”, s);
}
第1处填空:!=
第2处填空:str[1]或*(str+1)
第3处填空:s,c [NextPage] 改错题
下列给定程序中,函数FUN的功能是:判断字符CH是否与STR所指串中的某个字符相同:若相同,则什么也不做,若不同,则将其插在串的最后。
请改正程序中的错误,使它能得出正确结果。
注意:不要改动MAIN函数,不得增行或删行,也不要更改程序的结构!
试题程序:#include
#include
#include
/********found********/
void fun(char str, char ch)
{
while (*str && *str!=ch)
str++;
/********found********/
if (*str == ch)
{
str[0] = ch;
/********found********/
str[1] = ’0’;
}
}
main()
{
char s[81], c;
printf("\n Please enter a string:\n");
gets(s);
printf("\n Please enter the character to search:");
c = getchar();
fun(s, c);
printf("\nThe result is %s\n", s);
}
第1处:void fun(char str,char ch)应改为void fun(char *str,char ch)
第2处:if(*str==ch)应改为if(*str==’\0’)
第3处:str[1]=’0’;应改为str[1]=’\0’;或str[1]=0[NextPage] 编程题
请编写个函数unsigned fun(unsigned w),w 是一个大于10的无符号整数,若w是n(n>=2)位的整数,则函数求出w的后n-1位作为函数值返回。
例如:w值为 5923,则函数返回923,若w值为923,则函数返回23
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include
#include
unsigned fun ( unsigned w )
{
}
main( )
{
unsigned x;
FILE *out;
printf ( "Enter a unsigned integer number : " );
scanf ( "%u", &x );
printf ( "The original data is : %u\n", x );
if ( x<10 )
printf ("Data error !");
else
printf ( "The result : %u\n", fun ( x ) );
out = fopen("out.dat", "w");
fprintf(out, "%u" , fun(28));
fclose(out);
}
答案是:
unsigned fun(unsigned w)
{
unsigned t,s=0,s1=1,p=0;
t=w;
while(t>10)
{
if(t/10)
p=t;
s=s+p*s1;
s1=s1*10;
t=t/10;
}
return s;
}