当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机题库及答案(93)
发布时间:2011/10/1 9:51:46 来源:城市学习网 编辑: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;

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

  __1__ fun(NODE *h)

  {NODE *p, *q, *r;

  p = h;

  if (p == NULL)

  return NULL;

  q = p->next;

  p->next = NULL;

  while (q)

  {

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

  r = q->__2__;

  q->next = p;

  p = q;

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

  q = __3__ ;

  }

  return p;

  }

  NODE *creatlist(int a[])

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

  h=NULL;

  for(i=0; i

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

  q->data=a[i];

  q->next = NULL;

  if (h == NULL) h = p = q;

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

  }

  return h;

  }

  void outlist(NODE *h)

  {NODE *p;

  p=h;

  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);

  head=fun(head);

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

  outlist(head);

  }

  解题答案:

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

  NODE* fun(NODE *h)

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

  r = q->next;

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

  q = r ;

  ****************************************** [NextPage]    二、改错题:给定程序MODI1.C中函数fun的功能是: 将s所指字符串中位于奇数位置的字符

  或ASCII码为偶数的字符放入t所指数组中(规定第一个字符放在第0位中)。

  例如, 字符串中的数据为: AABBCCDDEEFF,

  则输出应当是:ABBCDDEFF。

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

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

  给定源程序:

  #include

  #include

  #define N 80

  void fun(char *s, char t[])

  {int i, j=0;

  for(i=0; i<(int)strlen(s); i++)

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

  if(i%2 && s[i]%2==0)

  t[j++]=s[i];

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

  t[i]='\0';

  }

  main()

  {char s[N], t[N];

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

  fun(s, t);

  printf("\nThe result is : %s\n",t);

  }

  解题答案:

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

  if(i%2||s[i]%2==0)

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

  t[j]='\0';

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

[NextPage]   三、程序题:请编写函数fun, 函数的功能是: 将M行N列的二维数组中的数据, 按列的顺序依次放到一维数组中。函数fun中给出的语句仅供参考。

  例如, 二维数组中的数据为:

  33 33 33 33

  44 44 44 44

  55 55 55 55

  则一维数组中的内容应是:

  33 44 55 33 44 55 33 44 55 33 44 55。

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

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

  给定源程序:

  #include

  void fun(int s[][10], int b[], int *n, int mm, int nn)

  {

  }

  main()

  {int w[10][10]={{33,33,33,33},{44,44,44,44},{55,55, 55,55}},i,j;

  int a[100]={0}, n=0;void NONO ();

  printf("The matrix:\n");

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

  {for(j=0;j<4; j++)printf("=",w[i][j]);

  printf("\n");

  }

  fun(w,a,&n,3,4);

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

  for(i=0;i

  NONO();

  }

  void NONO ()

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

  FILE *rf, *wf ; int i, j, k ;

  int w[10][10], a[100], n = 0, mm, nn ;

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

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

  for(k = 0 ; k < 5 ; k++) {

  fscanf(rf, "%d %d", &mm, &nn);

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

  for(j = 0 ; j < nn ; j++) fscanf(rf, "%d", &w[i][j]);

  fun(w, a, &n, mm, nn);

  for(i = 0 ; i < n ; i++) fprintf(wf, "=", a[i]); fprintf(wf, "\n");

  }

  fclose(rf); fclose(wf);

  }

  参考答案:

  void fun(int (*s)[10], int *b, int *n, int mm, int nn)

  {

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

  int i,j,np=0; /* np用作b数组下标 */

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

  for(j = 0 ; j < mm ; j++) {

  b[np] = s[j][i] ;

  np = i * mm + j + 1;

  }

  *n=np;

  }

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