当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机题库及答案(66)
发布时间:2011/9/30 11:22:14 来源:城市学习网 编辑:ziteng
  一、填空题:给定程序中,函数fun的功能是将a和b所指的两个字符串转换成面值相同的整数,并进行相加作为函数值返回,规定字符串中只含9个以下数字字符。

  例如,主函数中输入字符串:32486和12345,在主函数中输出的函数值为:44831。

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

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

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

  给定源程序:

  #include

  #include

  #include

  #define N 9

  long ctod(char *s)

  {long d=0;

  while(*s)

  if(isdigit(*s)) {

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

  d=d*10+*s-__1__;

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

  __2__;}

  return d;

  }

  long fun(char *a, char *b)

  {

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

  return __3__;

  }

  main()

  {char s1[N],s2[N];

  do

  {printf("Input string s1 : "); gets(s1);}

  while(strlen(s1)>N);

  do

  {printf("Input string s2 : "); gets(s2);}

  while(strlen(s2)>N);

  printf("The result is: %ld\n", fun(s1,s2));

  }

  解题答案:

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

  d=d*10+*s-'0';

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

  s++;}

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

  return ctod(a)+ctod(b);

  ****************************************** [NextPage]   二、改错题:给定程序MODI1.C中 fun 函数的功能是:分别统计字符串中大写字母和小写字母的个数。

  例如, 给字符串 s 输入:AAaaBBb123CCccccd,则应输出结果:upper = 6, lower = 8。

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

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

  给定源程序:

  #include

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

  void fun (char *s, int a, int b)

  {

  while (*s)

  {if (*s >= 'A' && *s <= 'Z')

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

  *a=a+1 ;

  if (*s >= 'a' && *s <= 'z')

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

  *b=b+1;

  s++;

  }

  }

  main()

  {char s[100]; int upper = 0, lower = 0 ;

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

  fun (s, & upper, &lower);

  printf("\n upper = %d lower = %d\n", upper, lower);

  }

  解题答案:

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

  void fun(char *s, int *a, int *b)

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

  *a=*a+1;

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

  *b=*b+1;

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

 [NextPage]   三、程序题:请编一个函数fun,函数的功能是使实型数保留2位小数,并对第三位进行四舍五入 (规定实型数为正数)。

  例如:实型数为 1234.567, 则函数返回 1234.570000;

  实型数为 1234.564, 则函数返回 1234.560000。

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

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

  给定源程序:

  #include

  float fun (float h)

  {

  }

  main()

  {float a;void NONO ();

  printf ("Enter a: "); scanf ("%f", &a);

  printf ("The original data is : ");

  printf ("%f \n\n", a);

  printf ("The result : %f\n", fun (a));

  NONO();

  }

  void NONO ()

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

  int i ;

  float a ;

  FILE *rf, *wf ;

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

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

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

  fscanf(rf, "%f", &a);

  fprintf(wf, "%f\n", fun(a));

  }

  fclose(rf);

  fclose(wf);

  }

  参考答案:

  float fun ( float h )

  {

  long w ;

  w = h * 100 + 0.5 ;

  return (float) w / 100 ;

  }

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