当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机操作题及答案(8)
发布时间:2011/6/30 10:59:46 来源:城市学习网 编辑:ziteng
  第8套
  填空题
  请补充main函数,该函数的功能是:从键盘输入一组整数,使用条件表达式找出最大的整数。当输入的整数为0时结束。
  例如,输入1 2 3 5 4 0时,最大的数为5。
  请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
  试题程序:  #include <stdio.h>
  #include <conio.h>
  #define  N 100
  main()
  {
  int  num[N];
  int  i = -1;
  int  max = 0;
  printf("\nInput  integer number: \n");
  do
  {
  i++;
  printf("num[%d]=", i);
  scanf("%d", ___1___);
  max = ___2___ num[i] : max;
  } while(___3___);
  printf("max=%d\n", max);
  }
  第1处填空:&num[i]或num+i
  第2处填空:max<num[i]?或num[i]>max?
  第3处填空:num[i]!=0或0!=num[i] [NextPage]  改错题
  下列给定程序中,函数fun的功能是:在字符串str中找出ASCII码值最大的字符,将其放在第一个位置上;并将该字符前的原字符向后顺序移动。例如,调用fun函数之前给字符串输入:ABCDeFGH,调用后字符串中的内容为eABCDFGH
  请改正程序中的错误,使程序能得出正确的结果。
  注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
  试题程序:#include <stdio.h>
  /********found********/
  void fun(char  *p);
  {
  char  max, *q;
  int  i = 0;
  max = p[i];
  while (p[i] != 0)
  {
  if (max < p[i])
  {
  /********found********/
  max = p[i];
  p = q+i;
  }
  i++;
  }
  while (q > p)
  {
  *q = *(q-1);
  q--;
  }
  p[0] = max;
  }
  main()
  {
  char  str[80];
  printf("Enter a string: ");
  gets(str);
  printf("\nThe original string:   ");
  puts(str);
  fun(str);
  printf("\nThe string after moving:  ");
  puts(str);
  printf("\n\n");
  }
  第1处:void fun(char *p);应改为void fun(char *p)
  第2处:p=q+I;应改为q=p+i;
  [NextPage]   编程题
  编写函数fun,函数功能是:根据以下公式计算s,计算结果作为函数值返回;n通过型参传入。
  S=1+ 例如:若n的值为11时,函数的值为1.833333。
  注意:部分源程序给出如下。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  试题程序:#include<conio.h>
  #include<stdio.h>
  #include<string.h>
  float fun (int n)
  {
  }
  main()
  {
  int n;
  float s;
  FILE *out;
  printf("\nPlease enter N:");
  scanf("%d",&n);
  s=fun(n);
  printf("The result is: %f\n",s);
  s = fun(28);
  out = fopen("out.dat", "w");
  fprintf(out, "%f", s);
  fclose(out);
  }
  答案是:
  float fun(int n)
  {
  int i;
  float s=1.0,t=1.0;
  for(i=2;i<=n;i++)
  {
  t=t+i;
  s=s+1/t;
  }
  return s;
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved