当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机操作题及答案(30)
发布时间:2011/7/8 11:39:14 来源:城市学习网 编辑:ziteng
  第30套
  填空题
  请补充fun函数,该函数的功能是:把从主函数中输入的由数字字符组成的字符串转换成一个无符号长整数,并且倒序输出。结果有函数返回。
  例如:输入:“123456”,结果输出:654321。
  注意:部分源程序给出如下
  请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
  试题程序:#include <conio.h>
  #include <stdio.h>
  #include <string.h>
  unsigned long fun(char  *s)
  {
  unsigned long  t = 0;
  int  k;
  int  i = 0;
  i = strlen(s);
  for (___1___; i>=0; i--)
  {
  k = ___2___;
  t = ___3___;
  }
  return t;
  }
  main()
  {
  char  str[8];
  printf("Enter a string made up of '0' to '9' digital character :  \n");
  gets(str);
  printf("The string is :  %s\n", str);
  if (strlen(str) > 8)
  printf(" The string is too long  !");
  else
  printf("The result  :  %lu\n", fun(str));
  }
  第1处填空:--i或i—或i==1或i=i-1
  第2处填空:s[i]-‘0’
  第3处填空:t*10+k或k+t*10或10*t+k或k+10*t [NextPage]   改错题
  下列给定程序中,函数fun的功能是:计算正整数num的各位上的数字之积,例如若输入252,则输出应该是20,若输入202,则输出 应该是0。
  请改正程序中的错误,使其能得出正确结果。
  注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
  试题 程序:
  #include <stdio.h>
  #include <conio.h>
  long fun(long  num)
  {
  /********found********/
  long  k;
  do
  {
  k *= num%10;
  /********found********/
  num \= 10;
  } while (num);
  return (k);
  }
  main()
  {
  long  n;
  printf("\please enter a number:");
  scanf("%ld", &n);
  printf("\n%ld\n", fun(n));
  }
  第1处:long k;应改为long k=1;
  第2处:num\=10;应改为num/=10;
 [NextPage]   编程题
  请编写一个函数FUN,它的功能是:求出一个2*M整型二维数组中最大元素的值,并将最大值返回调用函数。
  注意:部分源程序给出如下。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  试题程序:
  #define M 4
  #include <stdio.h>
  fun (int a[][M])
  {
  }
  main()
  {
  int arr[2][M]={5,8,3,45,76,-4,12,82} ;
  FILE *out;
  printf("max=%d\n", fun(arr)) ;
  out=fopen("out.dat", "w");
  fprintf(out, "max=%d", fun(arr)) ;
  fclose(out);
  }
  答案是:
  int fun(int  a[][M])
  {
  int I,j,max;
  max=a[0][0];
  for(i=0;i<2;i++)
  for(j=0;j<M;j++)
  if(a[i][j]>max)
  max=a[i][j];
  return max;
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved