当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机操作题及答案(92)
发布时间:2011/7/28 15:42:23 来源:城市学习网 编辑:ziteng
  一、填空题
  请补充main函数,该函数的功能是:把字符串str中的字符向前移动一位,原来的第一个字符移动到字符串尾,结果仍然保存在原字符串中。
  例如,输入“how  are  you ?” ,则结果输出“ ow  are  you ? h”。
  仅在横线上填入所编写的若干表达式或语句,勿改动函数中的其他任何内容。
  #include
  #define  N 80
  main()
  {
  char  str[N], ch;
  int  i;
  printf("\n Input a string: \n");
  gets(str);
  printf("\n******** original string ********\n");
  puts(str);
  ch = str[0];
  for (i=0; ___1___; i++)
  str[i] = str[i+1];
  ___2___;
  printf("\n ******** new string ******** \n");
  puts(str);
  }
  答案:
  1、str[i+1]或str[i+1]!=’\0’或 str[i+1]!=0
  2、stri[i]=ch [NextPage]  二、改错题
  下列给定程序中,fun函数的功能是:求s=aa…aa-…-aa-aa-a(此处aa…aa表示n个a,a和n的值在1至9之间)。例如a=3,n=6,则以上表达式为:
  s=333333-33333-3333-333-33-3
  其值是296298。a 和 n 是fun函数的形参,表达式的值作为函数值传回main函数。
  请改正程序中的错误,使它能计算出正确的结果。
  注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
  #include
  #include
  long fun(int  a, int  n)
  {
  int  j;
  /********found********/
  long  s = 0, t = 1;
  /********found********/
  for (j=0; j<=n; j++)
  t = t*10 + a;
  s = t;
  for (j=1; j       {
  /********found********/
  t = t;
  s = s-t;
  }
  return(s);
  }
  main()
  {
  int  a, n;
  printf("\nPlease enter a and n:");
  scanf("%d%d", &a, &n);
  printf("The value of function is %ld\n", fun(a, n));
  }
  答案:
  1、long  s=0, t=1; 应改为 long  s=0, t=0;
  2、for(j=0;j<=n;j++) 应改为 for(j=0;j3、t=t; 应改为 t=s/n;
  [NextPage]   三、编程题
  请编写函数fun,它的功能是计算:
  s=       s作为函数值返回。
  在C语言中可调用log(n)函数求ln(n)。log函数的引用说明是:double log (double x)。
  例如,若m的值为20,则fun函数值为6.506583。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  #include
  #include
  #include
  double  fun( int  m )
  {
  }
  main()
  {
  int i;
  FILE *out;
  printf("%f\n",fun(20));
  out=fopen("out.dat","w");
  for (i = 0; i < 10; i++)
  fprintf(out, "%f\n", fun(i+15));
  fclose(out);
  }
  答案:
  double  fun ( int  m)
  {
  int  i ;
  double  s=0.0 , log(double  x);
  for (i=1; i<=m ;i++)
  s=s+log(i);
  s=sqrt(s);
  return  s;
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved