当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机操作题及答案(53)
发布时间:2011/7/17 18:11:29 来源:城市学习网 编辑:ziteng
  第一题:请补充函数fun,该函数的功能是建立一个带头结点的单向链表并输出到文件“out53.dat”和屏幕上,各结点的值为对应的下标链表的结点数和输出的文件名作为参数传入。
  请不要改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
  #include
  #include
  typedef struct ss
  {
  int data;
  struct ss *next;
  }NODE;
  void fun(int n,char *filename)
  {
  NODE *h, *p, *s ;
  FILE *pf;
  int i;
  h=p=(NODE *)malloc(sizeof(NODE));
  h->data=0;
  for(i=1;i       {
  s=(NODE *)malloc(sizeof(NODE));
  s->data=___1___;
  ___2___;
  p=___3___;
  }
  p->next=NULL;
  if((pf=fopen(filename,"w"))==NULL)
  {
  printf("Can not open out53.dat!");
  exit(0);
  }
  p=h;
  fprintf(pf,"\n***THE LIST*** \n");
  printf("\n***THE LIST*** \n");
  while(p)
  {
  fprintf(pf,"=",p->data);
  printf("=",p->data);
  if(p->next!=NULL)
  {
  fprintf(pf,"->");
  printf("->");
  }
  p=p->next;
  }
  fprintf(pf,"\n");
  printf("\n");
  fclose(pf);
  p=h;
  while(p)
  {
  s=p;
  p=p->next;
  free(s);
  }
  }
  main()
  {
  char *filename="out53.dat";
  int n;
  printf("\nInput n:");
  scanf("%d",&n);
  fun(n,filename);
  }
  答案
  第一空: i
  第二空: p→next=s
  第三空:p→next [NextPage]   第二题:下列给定的程序,函数fun 的功能是:用递归法计算裴波拉契级数数列中第n项的值。从第1项起,裴波拉契级数序列为1、1、2、3、5、8、13、21…例如,若给予n输入7,该项的裴波拉契级数值为13。
  #include
  long fun(int  g)
  {
  /********found********/
  switch(g);
  {
  case 0:
  return 0;
  /********found********/
  case 1;
  case 2:
  return 1;
  }
  return (fun(g-1) + fun(g-2));
  }
  main()
  {
  long  fib;
  int  n;
  printf("Input n:  ");
  scanf("%d", &n);
  printf("n=%d\n", n);
  fib = fun(n);
  printf("fib = %d\n\n", fib);
  }
  答案:
  第一处:switch(g); 应改成switch(g)
  第二处:case 1;应改成case 1:
 [NextPage]   第三题:
  请编写函数fun,该函数的功能是:实现B=A+A’,即把矩阵A加A的转置,存放在矩阵B。计算结果在main函数中输出。
  例如,输入下面的矩阵:      其转置矩阵为:
  1  2   3               1   4   7
  4  5   6               2   5   8
  7  8   9               3   6   9
  则程序输出:
  2     6     10
  6     10    14
  10    14    18
  注意:部分源程序给出如下。
  请不要改动主函数main 各其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  #include
  #include
  void fun ( int a[3][3], int b[3][3])
  {
  }
  main( )
  {
  int a[3][3]={{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, t[3][3] ;
  int i, j ;
  FILE *out;
  fun(a, t) ;
  out=fopen("out.dat", "w");
  for (i = 0 ; i < 3 ; i++)
  {
  for (j = 0 ; j < 3 ; j++)
  {
  printf("}", t[i][j]) ;
  fprintf(out, "}", t[i][j]) ;
  }
  printf("\n") ;
  fprintf(out, "\n");
  }
  fclose(out);
  }
  答案:
  void fun( int a[3][3],int b[3][3])
  {int i,j,at[3][3];
  for(i=0;i<=2;i++)
  for(j=0;j<=2;j++)
  at[i][j]=a[j][i];
  for(i=0;i<3;i++)
  for(j=0;j<3;j++)
  b[i][j]=a[i][j]+at[i][j];
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved