当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机操作题及答案(16)
发布时间:2011/7/4 11:02:02 来源:城市学习网 编辑:ziteng
  第16套
  填空题
  请补充fun函数,该函数的功能是:判断一个年份是否为闰年.
  例如,1900年不是闰年,2004是闰年.
  注意:部分源程序给出如下
  请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填如所编写的若干表达式或语句。
  试题程序:#include <stdio.h>
  #include <conio.h>
  int fun(int n)
  {
  int flag = 0;
  if (n%4 == 0)
  if (___1___)
  flag = 1;
  if (___2___)
  flag = 1;
  return ___3___;
  }
  main()
  {
  int year;
  printf("Input the year:");
  scanf("%d", &year);
  if (fun(year))
  printf("%d is a leap year.\n", year);
  else
  printf("%d is not a leap year.\n", year);
  }
  第1处填空:n%100 !=0或0 !=n%100
  第2处填空:N400==0或!(n%400)或0==n%400
  第3处填空:flag [NextPage]   改错题
  下列给定程序中的函数Creatlink的功能是:创建带头结点的单向链表,并为各结点数据域赋0到M-1的值。
  请改正程序中的错误,使它能得出正确结果。
  注意:不要改动MAIN函数,不得增行或删行,也不要更改程序的结构!
  试题程序:#include <stdio.h>
  #include <conio.h>
  #include <stdlib.h>
  typedef struct aa
  {
  int data;
  struct aa *next;
  } NODE;
  NODE *Creatlink(int n, int m)
  {
  NODE *h = NULL, *p, *s;
  int i;
  s = (NODE*)malloc(sizeof(NODE));
  h = s;
  /********found********/
  p->next = NULL;
  for (i=1; i<n; i++)
  {
  s = (NODE*)malloc(sizeof(NODE));
  /********found********/
  s->data = rand()%m;
  s->next = p->next;
  p->next = s;
  p = p->next;
  }
  s->next = NULL;
  /********found********/
  return p;
  }
  outlink(NODE *h)
  {
  NODE *p;
  p = h->next;
  printf("\n\nTHE LIST :\n\n HEAD");
  while (p)
  {
  printf("->%d ", p->data);
  p = p->next;
  }
  printf("\n");
  }
  main()
  {
  NODE *head;
  head = Creatlink(8, 22);
  outlink(head);
  }
  第1处:p->next=NULL;应改为p=s;
  第2处:s->data=rand()%m;应改为s->data=rand()%(m-1)
  第3处:return p;应改为return h;
 [NextPage]   编程题
  请编写一个函数float fun(double h),函数的功能是对变量h 中的值保留2位小数,并对第三位进行四舍五入(规定h中的值为正数)
  例如:若h值为8.32433,则函数返回8.32,若h值为8.32533,则函数返回8.33
  #include <stdio.h>
  #include <conio.h>
  float fun ( float h )
  {
  }
  main( )
  {
  float a;
  FILE *out;
  printf ( "Enter a: ");
  scanf ( "%f", &a );
  printf ( "The original data is: ");
  printf ( "%f \n\n", a );
  printf ( "The result : %f\n", fun ( a ) );
  out = fopen("out.dat", "w");
  fprintf(out, "%f" , fun(3.141593));
  fclose(out);
  }
  答案是:
  float fun(float h)
  {
  long t;
  float s;
  h=h*1000;
  t=(h+5)/10;
  s=(float)t/100.0;
  return s;
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved