当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机题库及答案(6)
发布时间:2011/8/16 8:52:46 来源:城市学习网 编辑:ziteng
  一、填空题

  给定程序中已建立一个带有头结点的单向链表,在main函数中将多次调用fun 函数,每调用一次fun函数,输出链表尾部结点中的数据,并释放该结点,使链表缩短。

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

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

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

  给定源程序:

  #include

  #include

  #define N 8

  typedef struct list

  {int data;

  struct list *next;

  } SLIST;

  void fun(SLIST *p)

  {SLIST *t, *s;

  t=p->next; s=p;

  while(t->next != NULL)

  {s=t;

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

  t=t->___1___;

  }

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

  printf(" %d ",___2___);

  s->next=NULL;

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

  free(___3___);

  }

  SLIST *creatlist(int *a)

  {SLIST *h,*p,*q; int i;

  h=p=(SLIST *)malloc(sizeof(SLIST));

  for(i=0; i

  {q=(SLIST *)malloc(sizeof(SLIST));

  q->data=a[i]; p->next=q; p=q;

  }

  p->next=0;

  return h;

  }

  void outlist(SLIST *h)

  {SLIST *p;

  p=h->next;

  if (p==NULL) printf("\nThe list is NULL!\n");

  else

  {printf("\nHead");

  do {printf("->%d",p->data); p=p->next;} while(p!=NULL);

  printf("->End\n");

  }

  }

  main()

  {SLIST *head;

  int a[N]={11,12,15,18,19,22,25,29};

  head=creatlist(a);

  printf("\nOutput from head:\n"); outlist(head);

  printf("\nOutput from tail: \n");

  while (head->next != NULL){

  fun(head);

  printf("\n\n");

  printf("\nOutput from head again :\n"); outlist(head);

  }

  }

  解题答案:

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

  t=t->next;

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

  printf(" %d ",t->data);

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

  free(t);

  ****************************************** [NextPage]  二、改错题:给定程序MODI1.C中函数fun的功能是:将字符串中的字符按逆序输出,但不改 变字符串中的内容。

  例如,若字符串为abcd,则应输出:dcba。

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

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

  给定源程序:

  #include

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

  fun (char a)

  {if (*a)

  {fun(a+1);

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

  printf("%c" *a);

  }

  }

  main()

  {char s[10]="abcd";

  printf("处理前字符串=%s\n处理后字符串=", s);

  fun(s); printf("\n");

  }

  解题答案:

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

  void fun(char *a)

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

  printf("%c", *a);

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

  [NextPage]   三、程序题:请编写一个函数fun,它的功能是:比较两个字符串的长度,(不得调用C语言 提供的求字符串长度的函数),函数返回较长的字符串。若两个字符串长度相同, 则返回第一个字符串。

  例如,输入beijing shanghai (为回车键), 函数将返回shanghai。

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

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

  给定源程序:

  #include

  char *fun (char *s, char *t)

  {

  }

  main()

  {char a[20],b[20];

  void NONO ();

  printf("Input 1th string:");

  gets(a);

  printf("Input 2th string:");

  gets(b);

  printf("%s\n",fun (a, b));

  NONO ();

  }

  void NONO ()

  {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */

  FILE *fp, *wf ;

  int i ;

  char a[20], b[20] ;

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

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

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

  fscanf(fp, "%s %s", a, b);

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

  }

  fclose(fp);

  fclose(wf);

  参考答案:

  {

  int i;

  char *p=s, *q=t;

  for(i=0;*p && *q; i++) {

  p++; q++;

  }

  if(*p == 0 && *q == 0) return s ;

  if(*p) return s ;

  else return t ;

  }

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