2016年计算机软件水平考试程序员精选模拟试题

时间:2016-08-08 17:25:00   来源:开云网页版     [字体: ]

  试题一 阅读下列说明、流程图和算法,将应填入 n 处的字句写在答题纸的对应栏内。

  [流程图说明]

  下面的流程图用N-S盒图形式描述了在一棵排序二叉树中查找元素的过程,节点有三个成员:data,left,right。其查找的方法是:首先与树的根节点的元素值进行比较:若相等则找到,返回此结点的地址;若要查找的元素小于根节点的元素值,则指针指向此结点的左子树,继续查找;若要查找的元素大于根节点的元素值,则指针指向此结点的游子树,继续查找。直到指针为空,表示此树中不存在所要查找的元素。

  [算法说明]

  将上题的排序二叉树中查找元素的过程用递归的方法实现。其中NODE是自定义类型:

  typedef struct node {

  int data;

  struct node *left;

  struct node *right;

  } NODE;

  [算法]

  NODE *SearchSortTree(NODE *tree,int e) {

  if(tree != NULL)

  { if (tree->data < e)

  (4) ; //小于查找左子树

  else if (tree->data < e)

  (5) ;//大于查找左子树

  else return tree;

  }

  return tree;

  }

  试题二

  阅读下列函数说明和C函数,将应填入 n 处的字句写在答题纸的对应栏内。

  [函数2.1说明]

  函数strcat(char s[], char t[])的功能是:将字符串t复制连接字符串s的尾部,并返回新字符串的首地址作为函数值。例如:若s=“abcd”,t=“efg”,则新字符串应该是“abcdefg”。

  [函数2.1]

  char *strcat(char s[], char t[])

  { char *p;

  p = s + strlen(s)-1

  while( (1) ) {

  (2) ;

  }

  *p = ‘\0’;

  return s;

  }

  [函数2.2说明]

  函数f(char *str, char del)的功能是:将非空字符串str中的指定字符del删除,形成一个新字符串仍存放在str所指内存单元中。

  例如若str的值为“33123333435”,del的值为‘3’,调用此函数后,新字符串为:“1245”。

  [函数2.2]

  void f(char *str, char del)

  {

  int i, j, len;

  len=strlen(str);

  i=j=0;

  while(i

  if ( (3) )

  (4) = str[i];

  i++;

  }

  (5) ;

  }

  试题三

  阅读以下说明和C代码,将应填入 n 处的字句写在答题纸的对应栏内。

  [说明]

  下面程序中函数fun的功能是:在含有10 个元素的s数组中查找数,及数所在位置(即,下标值),数可能不止一个。数作为函数值返回,数的个数通过指针变量n传回,所在位置由数组pos传回。

  例如:

  若输入 2 8 5 7 8 4 5 3 2 8

  则应输出:

  The max: 8

  Total: 3 //数出现次数

  The positions: 1 4 9

  #include

  #define M 10

  int fun(int *a, int *n, int pos[])

  { int i, k, max=-32767;

  (1)

  for(i=0; i

  if( (2) ) max=a[i];

  for(i=0; i

  if( (3) ) pos[k++]=i;

  *n=k;

  return max;

  }

  main()

  { int a[M], pos[M], i=0, j, n;

  printf("Enter 10 number :");

  for(i=0; i

  j=fun( (5) );

  printf("The max: %d\n", j);

  printf("Total: %d",n);

  printf("The position:");

  for(i=0; i

  printf("\n");

  }

  试题四 (试题四和试题五选做一题)

  阅读下列函数说明和C函数,将应填入 n 处的字句写在答题纸的对应栏内。

  [函数说明]

  函数ReadDat()实现从文件in.dat中读取20行数据存放到字符串数组dat中(第行字符串长度均小于80)。请编制函数jsSort(),其函数的功能是:以行为单位对字符串按给定的条件进行排序,排序后的结果仍按行重新存入字符串数组dat中,最后调用函数WriteDat()把结果dat输出到文件out.dat中。

  条件:从字符串中间一分为二,右边部分按字符的ASCII值降序排序,排序后左边部分与右边部分进行交换。如果原字符串长度为奇数,则最中间的字符不参加排序,字符仍放在原位置上。

  例如:位置    0 1 2 3 4 5 6 7

  源字符串  h g f e a b c d

  则处理后字符串 d c b a h g f e

  部分源程序已给出。

  请勿改动主函数main()、读数据函数ReadDat()和输出数据函数WriteDat()的内容。

  #include

  #include

  char dat[20][80];

  void jsSort()

  { int i,j,k,strl;

  char ch;

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

  { strl=strlen(dat[i]);

  for(j= (1) ;j

  for(k=j+1;k

  if( (2) )

  {

  ch=dat[i][j];

  dat[i][j]=dat[i][k];

  dat[i][k]=ch;

  }

  for(j=0; (3) ;j++)

  {

  ch=dat[i][j];

  dat[i][j]=dat[i][(strl+1)/2+j];

  dat[i][(strl+1)/2+j]=ch;

  }

  }

  }

  readDat()

  { FILE *in;

  int i=0;

  char *p;

  (4) ;

  while(i<20&&fgets(dat[i],80,in)!=NULL)

  { p=strchr(dat[i],’\n’);

  if(p)*p=0;

  i++;

  }

  fclose(in);

  }

  writeDat()

  { FILE *out;

  int i;

  out=fopen(“out.dat”,”w”);

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

  { printf(“%s\n” ,dat[i]);

  fprintf( (5) );

  }

  fclose(out);

  }

  void main()

  { readDat();

  jsSort();

  writeDat();

  }