当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机题库及答案(89)
发布时间:2011/10/1 9:48:05 来源:城市学习网 编辑:ziteng
  一、填空题:程序通过定义学生结构体数组,存储了若干名学生的学号、姓名和3门课的成绩。函数fun的功能是将存放学生数据的结构体数组,按照姓名的字典序(从小到大)排序。

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

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

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

  给定源程序:

  #include

  #include

  struct student {

  long sno;

  char name[10];

  float score[3];

  };

  void fun(struct student a[], int n)

  {

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

  __1__ t;

  int i, j;

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

  for (i=0; i<__2__; i++)

  for (j=i+1; j

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

  if (strcmp(__3__) > 0)

  {t = a[i]; a[i] = a[j]; a[j] = t;}

  }

  main()

  {struct student s[4]={{10001,"ZhangSan", 95, 80, 88},{10002,"LiSi", 85, 70, 78},{10003,"CaoKai", 75, 60, 88}, {10004,"FangFang", 90, 82, 87}};

  int i, j;

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

  for (j=0; j<4; j++)

  {printf("\nNo: %ld Name: %-8s Scores: ",s[j].sno, s[j].name);

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

  printf("\n");

  }

  fun(s, 4);

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

  for (j=0; j<4; j++)

  {printf("\nNo: %ld Name: %-8s Scores: ",s[j].sno, s[j].name);

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

  printf("\n");

  }

  }

  解题答案:

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

  struct student t;

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

  for (i=0; i

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

  if (strcmp(a[i].name,a[j].name) > 0)

  ****************************************** [NextPage]   二、改错题:给定程序MODI1.C中函数fun的功能是:在p所指字符串中找出ASCII码值最大的字符,将其放在第一个位置上;并将该字符前的原字符向后顺序移动。

  例如,调用fun函数之前给字符串输入:ABCDeFGH,调用后字符串中的内容为:eABCDFGH。 请改正程序中的错误,使它能得出正确结果。

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

  给定源程序:

  #include

  void fun(char *p)

  {char max,*q; int i=0;

  max=p[i];

  while(p[i]!=0)

  {if(max

  {max=p[i];

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

  q=p+i

  }

  i++;

  }

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

  wihle(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");

  }

  解题答案:

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

  q=p+i;

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

  while(q>p)

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

 [NextPage]   三、程序题:学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组 s中, 请编写函数fun,它的功能是:把指定分数范围内的学生数据放在b所指的数组中,分数范围内的学生人数由函数值返回。

  例如,输入的分数是60 69, 则应当把分数在60到69的学生数据进行输出,

  包含60分和69分的学生数据。主函数中将把60放在low中,把69放在heigh中。

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

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

  给定源程序:

  #include

  #define N 16

  typedef struct

  {char num[10];

  int s;

  } STREC;

  int fun(STREC *a,STREC *b,int l, int h)

  {

  }

  main()

  {STREC s[N]={{"GA005",85},{"GA003",76}, {"GA002",69},{"GA004",85},{"GA001",96},{"GA007",72},{"GA008",64},{"GA006",87},{"GA015",85},{"GA013",94},{"GA012",64},{"GA014",91},{"GA011",90},{"GA017",64},{"GA018",64},{"GA016",72}};

  STREC h[N],tt;FILE *out ;

  int i,j,n,low,heigh,t;

  printf("Enter 2 integer number low & heigh : ");

  scanf("%d%d", &low,&heigh);

  if (heigh< low){t=heigh;heigh=low;low=t;}

  n=fun(s,h,low,heigh);

  printf("The student's data between %d--%d :\n",low,heigh);

  for(i=0;i

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

  printf("\n");

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

  n=fun(s,h,80,98);

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

  for(i=0;i

  for(j=i+1;j

  if(h[i].s>h[j].s) {tt=h[i] ;h[i]=h[j]; h[j]=tt;}

  for(i=0;i

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

  fprintf(out,"\n");

  fclose(out);

  }

  参考答案:

  int fun( STREC *a,STREC *b,int l, int h )

  {

  int i,j = 0 ;

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

  if(a[i].s >= l && a[i].s <= h) b[j++] = a[i] ;

  return j ;

  }

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