c语言的上机测试题,有助于测试与提高。
请补充函数fun,该函数的功能是:把ASCII码为偶数的字符从字符串str中删除,结果仍然保存在字符串str中,字符串str从键盘输入,其长度作为参数传入函数fun。
例如,输入"abcdef",输出"ace"。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。
试题程序:
#include
#include
#define N 80
【1】
{
int i, j;
【2】;
for (i=0;i
{
if(s[i]%2!=0)
s[j++]=s[i];
}
【3】;
}
void main()
{
int i=0, strlen=0;
char str[N];
system("CLS");
printf("\nInput a string:\n");
gets(str);
while(str[i]!='\0')
{
strlen++;
i++;
}
fun(str, strlen);
printf("\n*** display string ***\n");
puts (str);
}
下列给定程序中函数fun的功能是:计算正整数num各位上的数字之积。例如,若输入252,则输出应该是20。若输入202,则输出应该是0。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
long fun(long num)
{
/*************found**************/
long k;
do
{
k *=num%10;
/*************found**************/
num \=10;
}while(num);
return(k);
}
main()
{
int n;
printf("\Please enter a number: ");
scanf("%ld",&n);
printf("\n%ld\n",fun(n));
}
请编写一个函数fun,它的功能是:求出1到m(含m)之内能被7或11整除的所有整数放在数组a中,通过n返回这些数的个数。
例如,若传给m的值为50,则程序输出:
7 11 14 21 22 28 33 35 42 44 49
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include
#include
#include
#define M 100
void fun(int m, int *a, int *n)
{
}
void main()
{
FILE *wf;
int aa[M],n,k;
system("CLS");
fun(50,aa,&n);
for(k=0;k
if((k+1)%20==0) /*每行输出20个数*/
{printf("%4d",aa[k]);
printf("\n");
}
else
printf("%4d",aa[k]);
printf("\n");
/******************************/
wf=fopen("out.dat","w");
for(k=0;k
fprintf(wf,"%4d",aa[k]);
fclose(wf);
/*****************************/
}
【参考答案】
(1) void fun(char s[],int n)
(2) j=0
(3) s[j]='\0'
【考点分析】
本题考查:函数定义,
需要确定类型标识符、函数名和形式参数。变量初始化,变量声明后,要进行初始化赋值,我们对变量进行初始化时,必须确定好变量的作用。字符串结束标识'\0',用来结束字符串,是经常考查的知识点。
【解题思路】
填空1:由main函数调用函数fun的格式可以知道,函数fun