2015年计算机二级C语言上机操作题及答案(42)
发布时间:2011/7/14 11:33:04 来源:城市学习网 编辑:ziteng
第42套
填空题
请补充main函数,该函数的功能是:从字符串str中取出所有数字字符, 并分别计数,
并把结果保存在数组b中并输出,把其它字符保存在b[10]中.
例如:当str=”de123456789abc0908”时,结果为: 0: 2 1: 1 2: 1 3: 1 4: 1 5: 1 6: 1 7: 1 8:
2 9: 2 other character: 5.
仅在横线上填入所编写的若干表达式或语句,勿改动函数中的其它任何内容.
#include
#include
main()
{
int i, b[11];
char *str = "de123456789abc0908";
char *p = str;
printf("****** the origial data ********\n");
puts(str);
for (i=0; i<11; i++)
b[i] = 0;
while (*p)
{
switch (*p)
{
case '0':
b[0]++;
break;
case '1':
b[1]++;
break;
case '2':
b[2]++;
break;
case '3':
b[3]++;
break;
case '4':
b[4]++;
break;
case '5':
b[5]++;
break;
case '6':
b[6]++;
break;
case '7':
b[7]++;
break;
case '8':
b[8]++;
break;
case '9':
b[9]++;
break;
default: b[10]++;break;
}
p++;
}
printf("****** the result ********\n");
for (i=0; i<10; i++)
printf("\n%d:%d", i, b[i]);
printf("\nother character:%d", b[i]);
} [NextPage] 改错题
下列给定程序中,函数fun的功能是: 将字符串s中位于奇数位置的字符或ASCII码为偶数的字符依次放入字符串中. 例如,字符串的数据为AABBCCDDEEFF,则输出应当是ABBCDDEFF.
请改正函数fun中的错误,使它能得出正确的结果.
注意: 不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
#include
#define N 80
void fun(char *s, char t[])
{
int i, j = 0;
/********found********/
for (i=0; i<(int)strlen(s); i++)
if (i%2 || s[i]%2==0)
t[j++] = s[i];
/********found********/
t[j] = '\0';
}
main()
{
char s[N], t[N];
printf("\nPlease enther string s:");
gets(s);
fun(s, t);
printf("\nThe result is : %s\n", t);
} [NextPage] 程序题
下列程序定义了N*N的二维数组,并在主函数中自动赋值.请编写函数fun(int a[][N],
int n), 该函数的功能是:使数组右上半三角元素中的值乘以m.
例如:若m的值为2,a数组中的值为
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句. #include
#include
#include
#include
#define N 5
void fun ( int a[][N], int m )
{int i,j;
for(j=0;jfor(i=0;i<=j;i++)
a[i][j]=a[i][j]*m;
}
main ( )
{
int a[N][N], m, i, j;
FILE *out;
printf("**** The array *****\n");
for ( i=0; i {
for( j=0; j {
a[i][j] = rand() ;
printf("M", a[i][j] );
}
printf("\n");
}
m = rand()%4 ;
printf("m=M\n", m);
fun ( a ,m );
printf (" THE RESULT\n");
for ( i=0; i {
for ( j=0; j printf( "M", a[i][j] );
printf("\n");
}
out=fopen ("out.dat", "w");
for ( i=0; i for( j=0; j a[i][j] = i*j;
fun ( a ,8);
for ( i=0; i {
for ( j=0; j fprintf(out, "M", a[i][j] );
fprintf(out, "\n");
}
fclose (out );
}