当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机操作题及答案(34)
发布时间:2011/7/12 8:42:35 来源:城市学习网 编辑:ziteng
  第34套
  填空题
  请补充fun函数,该函数的功能是:分类统计一个字符串中元音字母和其它字符的个数(不区分大小写)。
  例如,输入aeiouAUpqr,记过为A:2     E:1    I:1    O:1    U:2     other:3
  注意:部分源程序给出如下
  请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
  试题程序:
  #include <stdio.h>
  #include <conio.h>
  #define  N 100
  void fun(char  *str, int  bb[])
  {
  char  *p = str;
  int  i = 0;
  for (i=0; i<6; i++)
  ___1___;
  while (*p)
  {
  switch (*p)
  {
  case 'A':
  case 'a':
  bb[0]++;
  break;
  case 'E':
  case 'e':
  bb[1]++;
  break;
  case 'I':
  case 'i':
  bb[2]++;
  break;
  case 'O':
  case 'o':
  bb[3]++;
  break;
  case 'U':
  case 'u':
  bb[4]++;
  break;
  default:
  ___2___;
  }
  ___3___
  }
  }
  main()
  {
  char  str[N], ss[5] = "AEIOU";
  int  i;
  int  bb[6];
  printf("Input a string: \n");
  gets(str);
  printf("the  string is: \n");
  puts(str);
  fun(str, bb);
  for (i=0; i<5; i++)
  printf("\n%c:%d", ss[i], bb[i]);
  printf("\nother:%d", bb[i]);
  }
  第1处填空:bb[i]=0或*(bb+i)=0
  第2处填空:bb[5]++或++bb[5]或bb[5]=bb[5]+1或bb[5]+=1
  第3处填空:p++;或++p;或p+=1;或p=p+1;[NextPage]  改错题
  下列给定程序中,函数fun的功能是:将长整型数中每一位上为奇数的数依次取出,构成一个新数放在t中,高位仍在高位,低位仍在低位,当s中的数为87653142时,t 中的数为7531。
  请改正程序中的错误,使其能得出正确结果。
  注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
  试题 程序:
  #include <conio.h>
  #include <stdio.h>
  void fun(long  s, long  *t)
  {
  int  d;
  long  s1 = 1;
  /********found********/
  t = 0;
  while (s > 0)
  {
  d = s%10;
  /********found********/
  if (d%2 == 0)
  {
  *t = d*s1 + *t;
  s1 *= 10;
  }
  s /= 10;
  }
  }
  main()
  {
  long  s, t;
  printf("\nPlease enter s: ");
  scanf("%ld", &s);
  fun(s, &t);
  printf("The result is: %ld\n", t);
  }
  第1处:t =0;应改为*t =0;
  第2处:if (d%2 ==0)应改为if(d%2!=0)
[NextPage]   编程题
  学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组S中,请编写函数FUN,它的功能是:把分数最高的学生数据放在H所指的数组中,注意:分数最高的学生可能不只一个,函数返回分数最高的学生的人数。
  注意:部分源程序给出如下。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  试题程序:
  #include <stdio.h>
  #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",77}, {"GA017",64}, {"GA018",64}, {"GA016",72}};
  STREC  h[N];
  int  i, n;
  FILE *out;
  n=fun ( s, h );
  printf ("The  %d  highest  score  :\n", n);
  for (i=0; i<n;  i++)
  printf ("%s  %4d\n", h[i]. num, h[i]. s);
  printf ("\n");
  out=fopen ("out.dat", "w");
  fprintf (out, "%d\n", n);
  for (i=0;  i<n;  i++)
  fprintf (out,  "%4d\n", h[i].s);
  fclose (out );
  }
  答案是:
  int fun(STREC *a,STREC *b)
  {
  int I,j=0,n=0,max;
  max=a[0].s;
  for(i=0;i<N;i++)
  if(a[i].s>max)
  max=a[i].s;
  for(i=0;i<N;i++)
  if(a[i].s==max)
  {
  *(b+j)=a[i];
  j++;
  n++;
  }
  return n;
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved