就业数据资源平台
当前位置:首页 > C语言程序设计
2013计算机等级考试二级C语言试题及答案14

  填空题
  请补充main函数,该函数的功能是:从键盘输入一组字符串,以’*’结束输入,并显示出这个字符串。
  例如,输入abcdef *, 结果显示abcdef。
  仅在横线上添入所编写的若干表达式或语句,勿改动函数中的其他任何内容。
  #include
  #define N 80
  main()
  { 考试大(www.Examda。com)
  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]


  改错题:
  下列给定程序中,函数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;
  程序设计:
  请编写函数 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);
  }
就业数据资源平台