当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机题库及答案(11)
发布时间:2011/8/16 8:55:58 来源:城市学习网 编辑:ziteng
  一、填空题

  人员的记录由编号和出生年、月、日组成,N名人员的数据已在主函数中存入结构体数组std中,且编号唯一。函数fun的功能是:找出指定编号人员的数据,作为函数值返回,由主函数输出,若指定编号不存在,返回数据中的编号为空串。

  请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。

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

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

  给定源程序:

  #include

  #include

  #define N 8

  typedef struct

  {char num[10];

  int year,month,day ;

  }STU;

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

  ___1___ fun(STU *std, char *num)

  {int i; STU a={"",9999,99,99};

  for (i=0; i

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

  if(strcmp(___2___,num)==0)

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

  return (___3___);

  return a;

  }

  main()

  {STU std[N]={ {"111111",1984,2,15},{"222222", 1983,9,21},{"333333",1984,9,1},{"444444",1983,7,15},{"555555",1984,9,28},{"666666",1983,11,15},{"777777",1983,6,22},{"888888",1984,8,19}};

  STU p; char n[10]="666666";

  p=fun(std,n);

  if(p.num[0]==0)

  printf("\nNot found !\n");

  else

  {printf("\nSucceed !\n ");

  printf("%s %d-%d-%d\n",p.num,p.year, p.month,p.day);

  }

  }

  解题答案:

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

  STU fun(STU *std, char *num)

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

  if(strcmp(std[i].num,num)==0)

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

  return (std[i]);

  ****************************************** [NextPage]   二、改错题:给定程序MODI1.C中函数fun的功能是:从s所指字符串中,找出与t所指字符串相同的子串的个数作为函数值返回。例如,当s所指字符串中的内容为:"abcdabfab",t所指字符串的内容为:

  "ab",则函数返回整数3。请改正程序中的错误,使它能得出正确的结果。

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

  给定源程序:

  #include

  #include

  int fun (char *s, char *t)

  {

  int n; char *p , *r;

  n = 0;

  while (*s)

  {p = s; r = t;

  while (*r)

  if (*r == *p) {

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

  r++; p++

  }

  else break;

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

  if (r == '\0')

  n++;

  s++;

  }

  return n;

  }

  main()

  {

  char s[100], t[100]; int m;

  printf("\nPlease enter string S:"); scanf("%s", s);

  printf("\nPlease enter substring t:"); scanf("%s", t);

  m = fun(s, t);

  printf("\nThe result is: m = %d\n", m);

  }

  解题答案:

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

  r++; p++;

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

  if(*r=='\0')

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

 [NextPage]   三、程序题:函数fun的功能是:将s所指字符串中ASCII值为偶数的字符删除,串中剩余字 符形成一个新串放在t所指的数组中。例如,若s所指字符串中的内容为:"ABCDEFG12345",其中字符B的ASCII码值为偶数、…、字符2的ASCII码值为偶数、… 都应当删除,其它依此类推。最后t所指的数组中的内容应是:"ACEG135"。

  注意: 部分源程序存在文件PROG1.C中。

  请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入

  你编写的若干语句。

  给定源程序:

  #include

  #include

  void fun(char *s, char t[])

  {

  }

  main()

  {

  char s[100], t[100];

  printf("\nPlease enter string S:"); scanf("%s", s);

  fun(s, t);

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

  NONO();

  }

  NONO ()

  {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */

  char s[100], t[100] ;

  FILE *rf, *wf ;

  int i ;

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

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

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

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

  fun(s, t);

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

  }

  fclose(rf);

  fclose(wf);

  参考答案:

  void fun(char *s, char t[])

  {

  int i, j = 0 ;

  for(i = 0 ; i < strlen(s); i++) if(s[i] % 2) t[j++] = s[i] ;

  t[j] = 0 ;

  }

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