当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机操作题及答案(94)
发布时间:2011/7/28 15:43:55 来源:城市学习网 编辑:ziteng
  一、 填空题
  请补充fun函数,该函数的功能是:删除字符数组中比指定字符小的字符,指定字符从键盘输入,结果仍保存在原数组中。
  例如,输入“abcdefghij”,指定字符为’d’则结果输出“defghij”。
  请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
  #include
  #define  N 80
  void fun(char  s[], char  ch)
  {
  int  i = 0, j = 0;
  while (s[i])
  {
  if (s[i] < ch)
  {
  ___1___;
  }
  else
  {
  ___2___;
  i++;
  }
  }
  ___3___;
  }
  main()
  {
  char  str[N], ch;
  printf("\n Input a string:\n");
  gets(str);
  printf("\n******** original string ********\n");
  puts(str);
  printf("\n Input a character :\n");
  scanf("%c", &ch);
  fun(str, ch);
  printf("\n******** new string ********\n");
  puts(str);
  }
  答案:
  1、i++ 或 ++i 或 i+=1 或 i=i+1
  2、s[j++]=s[i]
  3、s[j]=’\0’ 或s[j]=0 [NextPage]  二、 改错题
  在主函数中从键盘输入若干个数放数组中,用0结束输入并放在最后一个元素中。下列给定程序中,函数fun的功能是:计算数组元素中值为正数的平均值(不包括0)。例如:数组中元素的值依次为39,-47,21,2,-8,15,0,则程序的运行结果为19.250000。
  请改正程序中的错误,使它能得出正确的结果。
  注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
  #include
  #include
  double fun(int  x[])
  {
  /********found********/
  int  sum = 0.0;
  int  c = 0, i = 0;
  while (x[i] != 0)
  {
  if (x[i] > 0)
  {
  sum += x[i];
  c++;
  }
  i++;
  }
  /********found********/
  sum \= c;
  return sum;
  }
  main()
  {
  int  x[1000];
  int  i = 0;
  printf("\nPlease enter some data(end with 0):");
  do
  {
  scanf("%d", &x[i]);
  } while (x[i++] != 0);
  printf("%lf\n", fun(x));
  }
  答案:
  1、int  sum=0.0; 应改为  double  sum=0.0;
  2、return  s ; 应改为  return  t ;
  [NextPage]   三、 编程题
  规定输入的字符串中只含字母和*号。请编写函数fun,它的功能是:将字符串中的前导*号全部删除,中间和结尾的*号不删除。
  例如,若字符串中的内容为*******A*BC*DEF*G****,删除后,字符串中的内容则应当是A*BC*DEF*G****。在编写函数时,不得使用C语言提供的字符串函数。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  #include
  #include
  #include
  void fun(char *a)
  {
  }
  main()
  {
  char s[81];
  FILE *out;
  printf("Enter a string :\n");
  gets(s);
  fun(s);
  printf("The string after deleted :\n");
  puts(s);
  out=fopen("out.dat","w");
  strcpy(s, "*******A*BC*DEF*G****");
  fun(s);
  fprintf(out, "%s", s);
  fclose(out);
  }
  答案:
  void  fun ( char  *a)
  {
  int  i=0;
  char  *p=a;
  while (*p&&*p==’*’)
  p++;
  while (*p)
  {
  a[i]=*p;
  i++;
  p++;
  }
  a[i]=’\0’;
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved