当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机题库及答案(31)
发布时间:2011/8/29 11:22:44 来源:城市学习网 编辑:ziteng
  一、填空题:给定程序中,函数fun的功能是:有N×N矩阵,以主对角线为对称线,对称元素相加并将结果存放在左下三角元素中,右上三角元素置为0。例如,若N=3,有下列矩阵:

  1 2 3

  4 5 6

  7 8 9

  计算结果为

  1 0 0

  6 5 0

  10 14 9

  请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结 果。

  注意:源程序存放在考生文件夹的BLANK1.C中。

  不得增行或删行,也不得更改程序的结构!

  给定源程序:

  #include

  #define N 4

  /**********found**********/

  void fun(int (*t)___1___)

  {int i, j;

  for(i=1; i

  {for(j=0; j

  {

  /**********found**********/

  ___2___ =t[i][j]+t[j][i];

  /**********found**********/

  ___3___ =0;

  }

  }

  }

  main()

  {int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42, 21,33,10}, i, j;

  printf("\nThe original array:\n");

  for(i=0; i

  {for(j=0; j

  }

  fun(t);

  printf("\nThe result is:\n");

  for(i=0; i

  {for(j=0; j

  }

  }

  解题答案:

  /**********第一空**********/

  void fun(int (*t)[N])

  /**********第二空**********/

  t[i][j] =t[i][j]+t[j][i];

  /**********第三空**********/

  t[j][i] =0;

  ****************************************** [NextPage]  二、改错题:给定程序MODI1.C中函数fun的功能是:计算函数 F(x,y,z)=(x+y)/(x-y)+(z+y)/(z-y)的值。其中x和y的值不等,z和y的值不等。

  例如,当x的值为9、y的值为11、z的值为15时,函数值为 -3.50。

  请改正程序中的错误,使它能得出正确结果。

  注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。

  给定源程序:

  #include

  #include

  #include

  /************found************/

  #define FU(m,n) (m/n)

  float fun(float a,float b,float c)

  {float value;

  value=FU(a+b,a-b)+FU(c+b,c-b);

  /************found************/

  Return(Value);

  }

  main()

  {float x,y,z,sum;

  printf("Input x y z: ");

  scanf("%f%f%f",&x,&y,&z);

  printf("x=%f,y=%f,z=%f\n",x,y,z);

  if (x==y||y==z){printf("Data error!\n");exit(0);}

  sum=fun(x,y,z);

  printf("The result is : %5.2f\n",sum);

  }

  解题答案:

  /************found************/

  #define FU(m,n) ((m)/(n))

  /************found************/

  return (value);

  ******************************************

 [NextPage]   三、程序题:规定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:将字符串中的前导*号全部删除,中间和尾部的*号不删除。 例如,字符串中的内容为:*******A*BC*DEF*G****,删除后,字符串中的内 容应当是:A*BC*DEF*G****。在编写函数时,不得使用C语言提供的字符串函数。

  注意: 部分源程序在文件PROG1.C中。

  请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入

  你编写的若干语句。

  给定源程序:

  #include

  void fun(char *a)

  {

  }

  main()

  {char s[81];

  void NONO ();

  printf("Enter a string:\n");gets(s);

  fun(s);

  printf("The string after deleted:\n");puts(s);

  NONO();

  }

  void NONO()

  {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */

  FILE *in, *out ;

  int i ; char s[81] ;

  in = fopen("in.dat","r");

  out = fopen("out.dat","w");

  for(i = 0 ; i < 10 ; i++) {

  fscanf(in, "%s", s);

  fun(s);

  fprintf(out, "%s\n", s) ;

  }

  fclose(in);

  fclose(out);

  }

  参考答案:

  void fun( char *a )

  {

  int j=0;

  char *p = a ;

  while(*p == '*') p++ ;

  while(*p) {

  a[j++] = *p ;

  p++;

  }

  a[j]=0 ;

  }

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