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

  一、填空题:给定程序中,函数fun的功能是:找出形参s所指字符串中出现频率最高的字母(不区分大小写),并统计出其出现的次数。

  例如,形参s所指的字符串为:abcAbsmaxless,程序执行后的输出结果为:

  letter 'a' : 3 times

  letter 's' : 3 times

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

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

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

  给定源程序:

  #include

  #include

  #include

  void fun(char *s)

  {int k[26]={0},n,i,max=0; char ch;

  while(*s)

  {if(isalpha(*s)) {

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

  ch=tolower(__1__);

  n=ch-'a';

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

  k[n]+= __2__ ;

  }

  s++;

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

  if(max

  }

  printf("\nAfter count :\n");

  for(i=0; i<26;i++)

  if (k[i]==max) printf("\nletter \'%c\' : %d times\n",i+'a',k[i]);

  }

  main()

  {char s[81];

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

  fun(s);

  }

  解题答案:

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

  ch=tolower(*s);

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

  k[n]+= 1 ;

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

  if(max

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

 [NextPage] 二、改错题:给定程序MODI1.C中函数 fun 的功能是:求S的值。

  例如,当k为10时,函数值应为:1.533852。

  请改正程序中的错误,使程序能输出正确的结果。

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

  给定源程序:

  #include

  #include

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

  void fun(int k)

  {int n; double s, w, p, q;

  n = 1;

  s = 1.0;

  while (n <= k)

  {w = 2.0 * n;

  p = w - 1.0;

  q = w + 1.0;

  s = s * w *w/p/q;

  n++;

  }

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

  return s

  }

  main ()

  {

  printf("%f\n", fun (10));

  }

  解题答案:

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

  double fun(int k)

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

  return s;

  ****************************************** [NextPage]   三、程序题:编写函数fun,它的功能是:计算并输出下列级数和:

  1 1 1

  S = ── + ── + … + ───

  1×2 2×3 n(n+1)

  例如,当n = 10时,函数值为:0.909091。

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

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

  给定源程序:

  #include

  double fun(int n)

  {

  }

  main() /* 主函数 */

  {void NONO ();

  printf("%f\n", fun(10));

  NONO();

  }

  void NONO ()

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

  FILE *fp, *wf ;

  int i, n ;

  double s ;

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

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

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

  fscanf(fp, "%d", &n);

  s = fun(n);

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

  }

  fclose(fp);

  fclose(wf);

  }

  参考答案:

  double fun( int n )

  {

  double s = 0.0 ;

  int i ;

  for(i = 1 ; i <= n ; i++) s = s + 1.0 / (i * (i + 1)) ;

  return s ;

  }

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