当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机题库及答案(87)
发布时间:2011/10/1 9:46:13 来源:城市学习网 编辑:ziteng
  一、填空题:程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是将形参a中的数据进行修改,把修改后的数据作为函数值返回主函数进行输出。

  例如:传给形参a的数据中,学号、姓名、和三门课的成绩依次是:10001、

  "ZhangSan"、95、80、88,修改后的数据应为:10002、"LiSi"、96、81、89。

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

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

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

  给定源程序:

  #include

  #include

  struct student {

  long sno;

  char name[10];

  float score[3];

  };

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

  __1__ fun(struct student a)

  {int i;

  a.sno = 10002;

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

  strcpy(__2__, "LiSi");

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

  for (i=0; i<3; i++) __3__+= 1;

  return a;

  }

  main()

  {struct student s={10001,"ZhangSan", 95, 80, 88}, t;

  int i;

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

  printf("\nNo: %ld Name: %s\nScores: ",s.sno, s.name);

  for (i=0; i<3; i++) printf("%6.2f ", s.score[i]);

  printf("\n");

  t = fun(s);

  printf("\nThe data after modified :\n");

  printf("\nNo: %ld Name: %s\nScores: ",t.sno, t.name);

  for (i=0; i<3; i++) printf("%6.2f ", t.score[i]);

  printf("\n");

  }

  解题答案:

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

  struct student fun(struct student a)

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

  strcpy(a.name, "LiSi");

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

  for (i=0; i<3; i++) a.score[i]+= 1;

  ****************************************** [NextPage]   二、改错题:假定整数数列中的数不重复,并存放在数组中。给定程序MODI1.C中函数fun 的功能是:删除数列中值为x的元素。n中存放的是数列中元素的个数。

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

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

  给定源程序:

  #include

  #define N 20

  int fun(int *a,int n,int x)

  {int p=0,i;

  a[n]=x;

  while(x!=a[p])

  p=p+1;

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

  if(P==n) return -1;

  else

  {for(i=p;i

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

  a[i+1]=a[i];

  return n-1;

  }

  }

  main()

  {int w[N]={-3,0,1,5,7,99,10,15,30,90},x,n,i;

  n=10;

  printf("The original data :\n");

  for(i=0;i

  printf("\nInput x (to delete): "); scanf("%d",&x);

  printf("Delete : %d\n",x);

  n=fun(w,n,x);

  if (n==-1) printf("***Not be found!***\n\n");

  else

  {printf("The data after deleted:\n");

  for(i=0;i

  }

  }

  解题答案:

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

  if(p==n)return -1;

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

  a[i]=a[i+1];

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

 [NextPage]   三、改错题:学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中, 请编写函数fun,它的功能是:把分数最低的学生数据放在b所指的数组中,

  注意:分数最低的学生可能不止一个,函数返回分数最低的学生的人数。

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

  请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。

  给定源程序:

  #include

  #define N 16

  typedef struct

  {char num[10];

  int s;

  } STREC;

  int fun(STREC *a, STREC *b)

  {

  }

  main()

  {STREC s[N]={{"GA05",85},{"GA03",76}, {"GA02",69},{"GA04",85},{"GA01",91},{"GA07",72},{"GA08",64},{"GA06",87},{"GA015",85},{"GA013",91},{"GA012",64},{"GA014",91},{"GA011",91},{"GA017",64},{"GA018",64},{"GA016",72}};

  STREC h[N];

  int i,n;FILE *out ;

  n=fun(s,h);

  printf("The %d lowest score :\n",n);

  for(i=0;i

  printf("%s M\n",h[i].num,h[i].s);

  printf("\n");

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

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

  for(i=0;i

  fprintf(out, "M\n",h[i].s);

  fclose(out);

  }

  参考答案:

  int fun( STREC *a, STREC *b )

  {

  int i, j = 0, min=a[0].s ;

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

  if(min > a[i].s) {

  j = 0 ; b[j++] = a[i] ; min = a[i].s ;

  }

  else if(min == a[i].s)

  b[j++] = a[i] ;

  }

  return j ;

  }

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