2015年计算机二级C语言上机操作题及答案(33)
发布时间:2011/7/10 10:24:41 来源:城市学习网 编辑:ziteng
第33套
填空题
函数fun的功能是:将形参a所指数组中的前半部分元素中的值和后半部分元素中的值兑换。形参n中存在数组的个数,若n为奇数,则中间的元素不动。
例如;若a所指数组中的数据的依次为:1、2、3、4、5、6、7、8、9,则调换后为6、7、8、9、5、1、2、3、4。
注意:部分源程序给出如下
请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
试题程序:#include <stdio.h>
#define N 9
void fun(int a[], int n)
{
int i, t, p;
p = (n%2 == 0) ? n/2 : n/2 + ___1___;
for (i=0; i<n/2; i++)
{
t = a[i];
a[i] = a[p+___2___];
___3___ = t;
}
}
main()
{
int b[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, i;
printf("\nThe original data :\n");
for (i=0; i<N; i++)
printf("%4d ", b[i]);
printf("\n");
fun(b, N);
printf("\nThe data after moving :\n");
for (i=0; i<N; i++)
printf("%4d ", b[i]);
printf("\n");
}
第1处填空:1
第2处填空:i
第3处填空:a[p+i]或*(a+p+i) [NextPage] 改错题
下列给定程序中,函数fun的功能是:将s所指字符串中的字母转换为按字母序列的后续字(但Z转换为A,z转换为a),其他字符不变。
请改正程序中的错误,使其能得出正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题 程序:
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
void fun(char *s)
{
/********found********/
while (*s != '@')
{
if (*s>='A'&&*s<='Z' || *s>='a'&&*s<='z')
{
if (*s == 'Z')
*s = 'A';
else if(*s == 'z')
*s = 'a';
else
*s += 1;
}
/********found********/
(*s)++;
}
}
main()
{
char s[80];
printf("\n Enter a string with length<80. :\n\n ");
gets(s);
printf("\n The string: \n\n ");
puts(s);
fun(s);
printf("\n\n The Cords:\n\n ");
puts(s);
}
第1处:while(*s !=’@’)应改为while(*s)或while(*s!=’\0’)或while(&s!=0)
第2处:(*s)++;应改为s++; [NextPage] 编程题
假定输入的字符串中只包含字母和*号。请编写函数FUN,它的功能是:使字符串中尾部的*号不得多于N个;若多于N个,则删除多余的*号;若少于或等于N个,则什么也不做,字符串中间和前面的*号不删除。
例如,字符串中的内容为****A*BC*DEF*G*******,若N的值为4,删除后,字符串中的内容则应当是****A*BC*DEF*G****;若N的值为7,则字符串中的内容仍为****A*BC*DEF*G*******。N的值在主函数中输入。在编写函数时,不得使用C语言提供的字符串函数。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void fun(char *a , int n)
{
}
main()
{
char s[81];
int n;
FILE *out;
printf("Enter a string :\n");
gets(s);
printf("Enter n: ");
scanf("%d",&n);
fun( s,n );
printf("The string after deleted :\n");
puts(s);
out=fopen("out.dat", "w");
strcpy(s, "****A*BC*D**EF*G*********");
fun(s, 5);
fprintf(out, "%s", s);
fclose(out);
}
答案是:
void fun( char *a,int n)
{
int i=0,k=0;
char *p,*t;
p=t=a;
while(*t)
t++;
t--;
while(*t==’*’)
{
k++;
t--;
}
t++;
if(k>n)
{
while(*p&&p<t+n)
{
a[i]=*p;
i++;
p++;
}
a[i]=’\0’;
}
}