第36套
填空题
Str是全部由数字和字母字符组成的字符串,由num传入字符串的长度,请补充fun函数,该函数的功能是把字符串str中的数字字符转换成数字并存放到整型数组bb中,函数返回数组bb的长度。
例如:str=“Abc123e456hui7890”,结果为:1234567890
注意:部分源程序给出如下
请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
试题程序:#include
#define N 80
int bb[N];
int fun(char s[], int bb[], int num)
{
int i, n = 0;
for (i=0; i if (s[i]>='0' ___1___ s[i]<='9')
{
bb[n] = ___2___;
n++;
}
return ___3___;
}
main()
{
char str[N];
int num = 0, n, i;
printf("Enter a string :\n");
gets(str);
while (str[num])
num++;
n = fun(str, bb, num);
printf("\nbb= ");
for (i=0; i printf("%d", bb[i]);
}
第1处填空:&&
第2处填空:s[i]-‘0’
第3处填空:n [NextPage] 改错题
下列给定程序中,函数fun的功能是:求三个数的最小公倍数。例如,给变量x1,x2,x3分别输入15112,则输出结果应当是330。
请改正程序中的错误,使其能得出正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题 程序:
#include
int fun(int x, int y, int z)
{
int j, t, n, m;
/********found********/
j = 1;
t = m = n = 1;
/********found********/
while (t!=0 && m!=0 && n!=0)
{
j = j+1;
t = j%x;
m = j%y;
n = j%z;
}
return j;
}
main()
{
int x1, x2, x3, j;
printf("Input x1 x2 x3: ");
scanf("%d%d%d", &x1, &x2, &x3);
printf("x1=%d, x2=%d, x3=%d \n", x1, x2, x3);
j = fun(x1, x2, x3);
printf("The minimal common multiple is : %d\n", j);
}
第1处:j=1;应改为j=0;
第2处:while(t!=0&&m!=0&&n!=0)应改为while(t!=0||n!=0)[NextPage] 编程题
假定输入的字符串中只包含字母和*号。请编写函数FUN,它的功能是:使字符串中前导*号全部移到字符串的尾部。
例如,若字符串中的内容为*******A*BC*DEF*G****,移动后,字符串中的内容应当是A*BC*DEF*G***********。在编写函数时,不得使用C语言提供的字符串函数。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include
#include
void fun( char *a)
{
}
main()
{
char s[81],*p;
FILE *out;
char test[4][80] = {"*******A*BC*DEF*G****", "A******B*CD**EF*G*",
"****A****G*", "*d**b**a**e*"};
int i;
printf("Enter a string:\n");
gets(s);
fun( s );
printf("The string after moveing:\n");
puts(s);
out=fopen("out.dat", "w");
for(i=0;i<4;i++)
{
fun(test[i]);
fprintf(out, "%s\n", test[i]);
}
fclose(out);
}
答案是:
void fun( char *a)
{
int i=0,n=0;
char *p;
p=a;
while(*p==’*’)
{
n++;
p++;
}
while(*p)
{
a[i]=*p;
i++;
p++;
}
while(n!=0)
{
a[i]=’*’;
i++;
n--;
}
a[i]=’\0’;
}
| 广告合作:400-664-0084 全国热线:400-664-0084 Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号 珠峰网 版权所有 All Rights Reserved
|