2015年计算机二级C语言上机操作题及答案(3)
发布时间:2011/6/30 10:53:13 来源:城市学习网 编辑:ziteng
第3套
填空题
请补充函数FUN(CHAR *S),该函数的功能是把字符串中的内容逆置.
例如:字符串中原有的字符串为abcde,则调用该函数后,串中的内容为edcba.
请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
试题程序:
#include <string.h>
#include <conio.h>
#include <stdio.h>
#define N 81
void fun(char *s)
{
int i, n = strlen(s)-1;
char t;
for (i=0; i<n; i++, ___1___)
{
t = s[i];
___2___;
___3___;
}
}
main()
{
char a[N];
printf("Enter a string:");
gets(a);
printf("The original string is:");
puts(a);
fun(a);
printf("\n");
printf("The string after modified:");
puts(a);
}
第1处填空:n--或--n或n-=1或n=n-1
第2处填空:s[i]=s[n]或*(s+i)=*(s+n)
第3处填空:s[n]=t或*(s+n)=t [NextPage] 改错题
下列给定的程序中,函数fun的功能是:依次取出字符串中所以数字字符,形成新的字符串,并取代原字符串。
请改正函数fun中的错误,使程序能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:#include <stdio.h>
#include <conio.h>
void fun(char *s)
{
int i, j;
/********found********/
for (i=0, j=0; s[i]!=’\0’; i++)
if (s[j]>=’0’ && s[i]<=’9’)
s[j] = s[i];
/********found********/
s[j] = "\0";
}
main()
{
char item[80];
printf("\nEnter a string :");
gets(item);
printf("\n\nThe string is : %s\n", item);
fun(item);
printf("\n\nThe string of changing is : %s\n", item);
}
第1处:s[j]=s[i];应改为s[j++]=s[i]
第2处:s[j]=”/0”;应改为s[j]=’\0’; [NextPage] 编程题
请编写函数void fun(int x,int pp[],int *n),它的功能是:求出能整除x且不是偶数的各整数,并按从小到大的顺序放在 pp所指的数组中,这些除数的个数通过形参 n返回.
例如,若x中的值为 30,则有 4个数符合要求,它们是 1,3,5,15.
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:#include <conio.h>
#include <stdio.h>
void fun(int x, int pp[], int *n)
{
}
main()
{
int x, aa[1000], n, i;
FILE *out;
printf("\nPlease enter an integer number:\n");
scanf("%d",&x);
fun(x,aa,&n);
for(i=0;i<n;i++)
printf("%d ", aa[i]);
printf("\n");
fun(730, aa, &n);
out = fopen("out.dat", "w");
for (i = 0; i < n; i++)
fprintf(out, "%d\n", aa[i]);
fclose(out);
}
答案是:
void fun(int x,int pp[],int *n)
{
int i=1,j=0,k=0,*t=pp;
for(i=0;i<=x;i++)
if(i%2!=0)
{
t[j]=i;
j++;
}
for(i=0;i<j;i++)
if(x%t[i]==0)
{
pp[k]=t[i];
k++;
}
*n=k;
}