当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机题库及答案(97)
发布时间:2011/10/1 9:55:45 来源:城市学习网 编辑:ziteng
  一、填空题:给定程序中,函数fun的功能是将形参给定的字符串、整数、浮点数写到文本 文件中,再用字符方式从此文本文件中逐个读入并显示在终端屏幕上。

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

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

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

  给定源程序:

  #include

  void fun(char *s, int a, double f)

  {

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

  __1__ fp;

  char ch;

  fp = fopen("file1.txt", "w");

  fprintf(fp, "%s %d %f\n", s, a, f);

  fclose(fp);

  fp = fopen("file1.txt", "r");

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

  ch = fgetc(fp);

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

  while (!feof(__2__)) {

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

  putchar(__3__); ch = fgetc(fp);}

  putchar('\n');

  fclose(fp);

  }

  main()

  {char a[10]="Hello!"; int b=12345;

  double c= 98.76;

  fun(a,b,c);

  }

  解题答案:

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

  FILE* fp;

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

  while (!feof(fp)) {

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

  putchar(ch); ch = fgetc(fp);}

  ****************************************** [NextPage]   二.改错题:给定程序MODI1.C中函数fun的功能是: 依次取出字符串中所有数字字符, 形成新的字符串, 并取代原字符串。

  请改正函数fun中指定部位的错误, 使它能得出正确的结果。

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

  给定源程序:

  #include

  void fun(char *s)

  {int i,j;

  for(i=0,j=0; s[i]!='\0'; i++)

  if(s[i]>='0' && s[i]<='9')

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

  s[j]=s[i];

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

  s[j]="\0";

  }

  main()

  {char item[80];

  printf("\nEnter a string : ");gets(item);

  printf("\n\nThe string is : \"%s\"\n",item);

  fun(item);

  printf("\n\nThe string of changing is : \"%s\"\n", item);

  }

  解题答案:

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

  s[j++]=s[i];

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

  s[j]='\0';

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

 [NextPage]   三、程序题:请编写函数fun, 函数的功能是: 将M行N列的二维数组中的字符数据, 按列的顺序依次放到一个字符串中。

  例如, 二维数组中的数据为:

  W W W W

  S S S S

  H H H H

  则字符串中的内容应是: WSHWSHWSH。

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

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

  你编写的若干语句。

  给定源程序:

  #include

  #define M 3

  #define N 4

  void fun(char s[][N], char *b)

  {

  int i,j,n=0;

  for(i=0; i < N;i++) /* 请填写相应语句完成其功能 */

  {

  }

  b[n]='\0';

  }

  main()

  {char a[100],w[M][N]={{'W','W','W','W'},{'S','S', 'S','S'},{'H','H','H','H'}};

  int i,j;void NONO ();

  printf("The matrix:\n");

  for(i=0; i

  {for(j=0;j

  printf("\n");

  }

  fun(w,a);

  printf("The A string:\n");puts(a);

  printf("\n\n");

  NONO();

  }

  void NONO ()

  {/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */

  FILE *rf, *wf ; int i,j,k ;

  char a[100],w[M][N], b ;

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

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

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

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

  for(j = 0 ; j < N ; j++) fscanf(rf, "%c", &w[i][j]);

  fscanf(rf, "%c", &b);

  }

  fun(w, a);

  fprintf(wf, "%s\n", a);

  }

  fclose(rf); fclose(wf);

  }#include

  #include

  void fun(char *s, int a, double f)

  {

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

  __1__ fp;

  char str[100], str1[100], str2[100];

  int a1; double f1;

  fp = fopen("file1.txt", "w");

  fprintf(fp, "%s %d %f\n", s, a, f);

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

  __2__ ;

  fp = fopen("file1.txt", "r");

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

  fscanf(__3__,"%s%s%s", str, str1, str2);

  fclose(fp);

  a1 = atoi(str1);

  f1 = atof(str2);

  printf("\nThe result :\n\n%s %d %f\n", str, a1, f1);

  }

  main()

  {char a[10]="Hello!"; int b=12345;

  double c= 98.76;

  fun(a,b,c);

  }

  参考答案:

  void fun(char s[][N], char *b)

  {

  int i,j,n=0;

  for(i=0; i < N;i++) /* 请填写相应语句完成其功能 */

  {

  for(j = 0 ; j < M ; j++) {

  b[n] = s[j][i] ;

  n = i * M + j + 1;

  }

  }

  b[n]='\0';

  }

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