当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机题库及答案(17)
发布时间:2011/8/19 15:15:56 来源:城市学习网 编辑:ziteng
  一、填空题:给定程序中,函数fun的功能是:对形参ss所指字符串数组中的M个字符串按长度由短到长进行排序。ss所指字符串数组中共有M个字符串,且串长

  注意:源程序存放在考生文件夹的BLANK1.C中。

  不得增行或删行,也不得更改程序的结构!

  给定源程序:

  #include

  #include

  #define M 5

  #define N 20

  void fun(char (*ss)[N])

  {int i, j, k, n[M]; char t[N];

  for(i=0; i

  for(i=0; i

  {k=i;

  /**********found**********/

  for(j=___1___; j

  /**********found**********/

  if(n[k]>n[j]) ___2___;

  if(k!=i)

  {strcpy(t,ss[i]);

  strcpy(ss[i],ss[k]);

  /**********found**********/

  strcpy(ss[k],___3___);

  n[k]=n[i];

  }

  }

  }

  main()

  {char ss[M][N]={"shanghai","guangzhou","beijing", "tianjing","cchongqing"};

  int i;

  printf("\nThe original strings are :\n");

  for(i=0; i

  printf("\n");

  fun(ss);

  printf("\nThe result :\n");

  for(i=0; i

  }解题答案:

  /**********第一空**********/

  for(j=i+1; j

  /**********第二空**********/

  if(n[k]>n[j]) k=j;

  /**********第三空**********/

  strcpy(ss[k],t);

  ****************************************** [NextPage]   二、改错题:给定程序MODI1.C中函数 fun 的功能是:判断ch中的字符是否与str所指串中的某个字符相同; 若相同,什么也不做,若不同,则将其插在串的最后。请改正程序中的错误,使它能进行正确操作。

  注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构!

  给定源程序:

  #include

  #include

  /**********found**********/

  void fun(char str, char ch)

  {while (*str && *str != ch) str++;

  /**********found**********/

  if (*str == ch)

  {str [0] = ch;

  /**********found**********/

  str[1] = '0';

  }

  }

  main()

  {char s[81], c ;

  printf("\nPlease enter a string:\n"); gets (s);

  printf ("\n Please enter the character to search : ");

  c = getchar();

  fun(s, c);

  printf("\nThe result is %s\n", s);

  }

  解题答案:

  /**********found*********void fun(char *str, char ch)*/

  /**********found*********if(*str !=ch)*/

  if(*str !=ch)

  /**********found*********str[1]='\0';*/

  str[1]='\0';

  ******************************************

 [NextPage]   三、程序题:请编一个函数fun(char *s),函数的功能是把s所指字符串中的内容逆置。例如:字符串中原有的字符串为:abcdefg,则调用该函数后, 串中的内容为:gfedcba。注意: 部分源程序存在文件PROG1.C中。

  请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。

  给定源程序:

  #include

  #include

  #define N 81

  fun (char *s)

  {

  }

  NONO()

  {/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */

  int i ;

  char a[N] ;

  FILE *rf, *wf ;

  rf = fopen("in.dat","r");

  wf = fopen("out.dat","w");

  for(i = 0 ; i < 10 ; i++) {

  fscanf(rf, "%s", a);

  fun(a);

  fprintf(wf, "%s\n", a);

  }

  fclose(rf);

  fclose(wf);

  }

  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);

  NONO();

  }

  参考答案:

  fun ( char *s )

  {

  char b[N] ;

  int i = 0, j ;

  memset(b, 0, N) ;

  for(j = strlen(s) - 1 ; j >= 0 ; j--) b[i++] = s[j] ;

  strcpy(s, b) ;

  }

广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved