2015年计算机二级C语言上机操作题及答案(83)
发布时间:2011/7/22 10:18:59 来源:城市学习网 编辑:ziteng
第一题:请补充fun函数,函数的功能是:从键盘输入一个字符串及一个指定字符,然后把这个字符及后面的所以字符全部删除。结果仍然保存在原串中。例如,输入”abcdef”,指定字符为’c’,则输出“ab“。
仅在横线上填入所编写的若干表达式或语句,勿改动函数中其他函数任何内容。
#include
#define N 80
main()
{
int i = 0;
char str[N];
char ch;
printf("\n Input a string:\n");
gets(str);
printf("\n Input a charater:\n");
scanf("%c", &ch);
while (str[i] != '\0')
{
if (str[i] == ch)
___1___
___2___;
}
str[i] = ___3___;
printf("\n********* display string *********\n");
puts(str);
}
答案:
第1处:break;
第2处 i++或++i 或i+=1或i=i+1
第3处 ‘\0’ 或0 [NextPage] 第二题:下列给定程序中,函数fun的功能是:交换主函数中两个变量的值。例如:若变量a中的值原为8,b中的值为3,则程序远行后a中的值为3,b中的值为8。
请改正程序中的错误,使它能得出正确的结果。
注意,不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
/********found********/
void fun(int x, int y)
{
int t;
/********found********/
t = x; x = y; y = t;
}
main()
{
int a, b;
a = 8;
b = 3;
fun(&a, &b);
printf("%d, %d\n", a, b);
}
答案:
第1处 void fun(int x,int y)应改为void fun(int*x,int*y)
第2处 t=x;x=y;y=t; 应改为t=*x;*x=*y;*y=t; [NextPage] 第三题:假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:将字符串尾部的*号全部删除,前面和中间的*号不删除。例如,若字符串中的内容为****A*BC*DEF*G*******,删除后,字符串中的内容则应当是****A*BC**DEF*G。在编写函数时,不得使用C语言提供的字符串函数。
请勿改动主函数main和其他函数中哦的任何内容,仅在函数fun的花括号填入所编写的若干语句。
#include
#include
#include
void fun( char *a)
{
}
main()
{
char s[81];
FILE *out;
printf("Enter a string:\n");
gets(s);
fun( s );
printf("The string after deleted:\n");
puts(s);
out=fopen ("out.dat", "w");
strcpy(s, "****A*BC*DE*F*G*********");
fun(s);
fprintf(out, "%s", s);
fclose (out );
}
答案
void fun( char *a)
{
int i=0;
char *p,*q;
p=q=a;
while(*p)
p++;
p--;
while(*p==’*’)
p--;
while(q<=p)
{
a[i]=*q;
i++;
q++;
}
a[i]=’\0’;
}