2016计算机三级考试试题网络技术精选试题(3)_第2页

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2016年2月18日
  3.已知在文件IN11.DAT中存有若干个(个数<200)4位数字的正整数,函数ReadDat() 的功能是读取这若干个正整数并存入数组xx中。请编制函数CalValue(),其功能要求:(1)求出该文件中共有多少个正整数totNum;(2)求这些数右移1位后,产生的新数是偶数的数的个数totCnt,以及满足此条件的这些数(右移前的值)的算术平均值totPjz,最后调用函数WriteDat()把所求的结果输出到文件OUT11.DAT中。
  注意:部分源程序已给出。
  请勿改动主函数main()、读函数ReadDat()和写函数WriteDat()的内容。
  #include
  #include
  #define MAXNUM 200
  int xx[MAXNUM] ;
  int totNum = 0 ; /* 文件IN11.DAT中共有多少个正整数 */
  int totCnt = 0 ; /* 符合条件的正整数的个数 */
  double totPjz = 0.0 ; /* 平均值 */
  int ReadDat(void) ;
  void Writedat(void) ;
  void CalValue(void)
  {
  int i; /*定义循环控制变量*/
  int data; /*用于保存处理后产生的新数*/
  for(i=0;i<200;i++) /*逐个取数组xx中的数进行统计*/
  if(xx[i]>0) /*判断是否正整数*/
  {
  totNum++; /*统计正整数的个数*/
  data=xx[i]>>1; /*将数右移一位*/
  if(data%2==0) /*如果产生的新数是偶数*/
  {
  totCnt++; /*统计这些数的个数*/
  totPjz+=xx[i]; /*并将满足条件的原数求和*/
  }
  }
  totPjz/=totCnt; /*求满足条件的这些数(右移前的值)的算术平均值*/
  }
  void main()
  {
  int i ;
  system("CLS");
  for(i = 0 ; i < MAXNUM ; i++)
  xx[i] = 0 ;
  if (ReadDat ())
  {
  printf("数据文件IN11.DAT不能打开!\007\n");
  return ;
  }
  CalValue() ;
  printf("文件IN11.DAT中共有正整数= %d 个\n", totNum);
  printf("符合条件的正整数的个数= %d 个\n", totCnt);
  printf("平均值=%.2lf\n", totPjz);
  Writedat() ;
  }
  int ReadDat(void)
  {
  FILE *fp;
  int i = 0 ;
  if((fp = fopen ("IN11.DAT", "r")) == NULL)
  return 1 ;
  while(! feof(fp))
  {
  fscanf(fp, "%d,", &xx[i++]) ;
  }
  fclose(fp) ;
  return 0 ;
  }
  void Writedat(void)
  {
  FILE *fp;
  fp = fopen("OUT11.DAT", "w") ;
  fprintf(fp, "%d\n%d\n%.2lf\n", totNum, totCnt, totPjz) ;
  fclose(fp) ;
  }
  4.已知数据文件IN12.DAT中存有300个4位数,并已调用读函数readDat()把这些数存入数组a中。请编制函数jsValue(),其功能是:求出千位数上的数加个位数上的数等于百位数上的数加十位数上的数的个数cnt,再把所有满足此条件的4位数依次存入数组b中,然后对数组b的4位数按从小到大的顺序进行排序,最后调用写函数writeDat()把数组b中的数输出到OUT12.DAT文件中。
  例如:6712,6+2=7+1,则该数满足条件,存入数组b中,且个数cnt=cnt+1。
  8129,8+9≠1+2,则该数不满足条件,忽略。
  注意:部分源程序已给出。
  程序中已定义数组:a[300],b[300],已定义变量:cnt。
  请勿改动主函数main()、读函数readDat()和写函数writeDat()的内容。
  #include
  int a[300], b[300], cnt=0;
  void readDat();
  void writeDat();
  void jsValue()
  {
  int i,j; /*定义循环控制变量*/
  int a1,a2,a3,a4; /*定义变量保存4位数的每位数字*/
  int temp; /*定义数据交换时的暂存变量*/
  for(i=0;i<300;i++) /*逐个取每一个4位数*/
  {
  a4=a[i]/1000; /*求4位数的千位数字*/
  a3=a[i]%1000/100; /*求4位数的百位数字*/
  a2=a[i]%100/10; /*求4位数的十位数字*/
  a1=a[i]%10; /*求4位数的个位数字*/
  if(a4+a1==a3+a2) /*如果千位数加个位数等于百位数加十位数*/
  {
  b[cnt]=a[i]; /*将满足条件的数存入数组b中*/
  cnt++; /*统计满足条件的数的个数cnt*/
  }
  }
  for(i=0;i
  for(j=i+1;j
  if(b[i]>b[j])
  {
  temp=b[i];
  b[i]=b[j];
  b[j]=temp;
  }
  }
  void main()
  {
  int i;
  readDat();
  jsValue();
  writeDat();
  printf("cnt=%d\n", cnt);
  for(i=0; i
  printf("b[%d]=%d\n", i, b[i]);
  }
  void readDat()
  {
  FILE *fp;
  int i;
  fp = fopen("IN12.DAT", "r");
  for(i=0; i<300; i++)
  fscanf(fp, "%d,", &a[i]);
  fclose(fp);
  }
  void writeDat()
  {
  FILE *fp;
  int i;
  fp = fopen("OUT12.DAT", "w");
  fprintf (fp, "%d\n",cnt);
  for(i=0; i
  fprintf(fp, "%d,\n", b[i]);
  fclose(fp);
  }
首页 1 2 尾页

相关文章