当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机操作题及答案(62)
发布时间:2011/7/20 13:10:29 来源:城市学习网 编辑:ziteng

  第62套题:填空题
  请补充main函数,该函数的功能是:从键盘输入一组字符串,以’*’结束输入,并显示出这个字符串。
  例如,输入abcdef *, 结果显示abcdef。
  仅在横线上添入所编写的若干表达式或语句,勿改动函数中的其他任何内容。
  #include
  #define  N 80
  main()
  {
  int  i = -1, j = 0;
  char  str[N];
  printf("\n Input a string \n");
  do
  {
  i++;
  scanf(_1_);
  } while (_2_);
  printf("\n ******* display the string ******* \n");
  while (j < i)
  {
  printf(_3_);
  j++;
  }
  }
  答案: (1)“%c”,&str[i]
  (2)str[i]!=’*’
  (3)”%c”,str[j] [NextPage]   改错题:
  下列给定程序中,函数fun的功能是:计算并输出high 以内最大的10个素数之和。 High由主函数传给fun 函数。若high的值为100,则函数的值为732。
  #include
  #include
  #include
  int fun(int  high)
  {
  int  sum = 0, n = 0, j, yes;
  while ((high >= 2) && (n < 10))
  {
  yes = 1;
  for (j=2; j<=high/2; j++)
  /********found********/
  if (high%j == 0)
  {
  yes = 0;
  break
  }
  if (yes)
  {
  sum += high;
  n++;
  }
  high--;
  }
  return sum;
  }
  main()
  {
  printf("%d\n", fun(100));
  }
  答案:break改为break;
 [NextPage]   程序设计:
  请编写函数 fun,该 函数的功能是:统计一行字符串中单词的个数,作为函数值返回。一行字符串在主函数中输入,规定所有单词由小写字母组成,单词之间由若干个空格隔开,一行的开始和结束都没有空格。
  #include
  #include
  #define  N  80
  int  fun(char *s)
  {
  }
  main()
  {
  char  line[N];
  int  num=0;
  FILE *out;
  char *test[] = {"Hello  World!", "This is  a   test    string.", "a    b", "cde f g,sf  l"};
  printf("Enter a string :\n");
  gets(line);
  num=fun( line );
  printf("The number of word is : %d\n\n",num);
  out=fopen("out.dat", "w");
  for(num=0;num<4;num++)
  printf(out, "%d\n", fun(test[num]));
  fclose(out);
  }

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