当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机题库及答案(94)
发布时间:2011/10/1 9:52:43 来源:城市学习网 编辑:ziteng
  一、填空题:给定程序中,函数fun的功能是将带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为:2、4、6、8、10,逆置后,从头至尾结点数据域依次为: 10、8、6、4、2。

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

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

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

  给定源程序:

  #include

  #include

  #define N 5

  typedef struct node {

  int data;

  struct node *next;

  } NODE;

  void fun(NODE *h)

  {NODE *p, *q, *r;

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

  p = h->__1__;

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

  if (p==__2__) return;

  q = p->next;

  p->next = NULL;

  while (q)

  {r = q->next; q->next = p;

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

  p = q; q = __3__;

  }

  h->next = p;

  }

  NODE *creatlist(int a[])

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

  h = (NODE *)malloc(sizeof(NODE));

  h->next = NULL;

  for(i=0; i

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

  q->data=a[i];

  q->next = NULL;

  if (h->next == NULL) h->next = p = q;

  else {p->next = q; p = q;}

  }

  return h;

  }

  void outlist(NODE *h)

  {NODE *p;

  p = h->next;

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

  else

  {printf("\nHead ");

  do

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

  while(p!=NULL);

  printf("->End\n");

  }

  }

  main()

  {NODE *head;

  int a[N]={2,4,6,8,10};

  head=creatlist(a);

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

  outlist(head);

  fun(head);

  printf("\nThe list after inverting :\n");

  outlist(head);

  }

  解题答案:

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

  p = h->next;

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

  if (p==0) return;

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

  p = q; q = r;

  ****************************************** [NextPage]   二、改错题:给定程序MODI1.C中函数fun的功能是: 计算s所指字符串中含有t所指字符串的数目, 并作为函数值返回。

  请改正函数fun中指定部位的错误, 使它能得出正确的结果。

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

  给定源程序:

  #include

  #include

  #define N 80

  int fun(char *s, char *t)

  {int n;

  char *p , *r;

  n=0;

  while (*s)

  {p=s;

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

  r=p;

  while(*r)

  if(*r==*p) {r++; p++;}

  else break;

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

  if(*r= 0)

  n++;

  s++;

  }

  return n;

  }

  main()

  {char a[N],b[N]; int m;

  printf("\nPlease enter string a : "); gets(a);

  printf("\nPlease enter substring b : "); gets(b);

  m=fun(a, b);

  printf("\nThe result is : m = %d\n",m);

  }

  解题答案:

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

  r=t;

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

  if(*r==0)

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

 [NextPage]   三、程序题:请编写函数fun, 函数的功能是: 将放在字符串数组中的M个字符串(每串的长度不超过N), 按顺序合并组成一个新的字符串。函数fun中给出的语句仅供参考。

  例如, 字符串数组中的M个字符串为

  AAAA

  BBBBBBB

  CC

  则合并后的字符串的内容应是: AAAABBBBBBBCC。

  提示:strcat(a,b)的功能是将字符串b复制到字符串a的串尾上,成为一个新串。

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

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

  给定源程序:

  #include

  #include

  #define M 3

  #define N 20

  void fun(char a[M][N], char *b)

  {

  }

  main()

  {char w[M][N]={"AAAA","BBBBBBB","CC"}, a[100];

  int i ;void NONO ();

  printf("The string:\n");

  for(i=0; i

  printf("\n");

  fun(w,a);

  printf("The A string:\n");

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

  NONO();

  }

  void NONO ()

  {/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */

  FILE *rf, *wf ;

  char w[M][N], a[100] ; int i ;

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

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

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

  fscanf(rf, "%s %s %s", w[0], w[1], w[2]);

  fun(w, a);

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

  }

  fclose(rf); fclose(wf);

  }

  参考答案:

  #include

  #define M 3

  #define N 20

  void fun(char a[M][N], char *b)

  {

  /* 以下代码仅供参考 */

  int i; *b=0;

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

  strcat(b, a[i]) ;

  }

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