模拟试题

3773考试网计算机等级考试模拟试题正文

2014年3月全国计算机二级C语言上机模拟试题一之2

来源:2exam.com 2013-10-30 10:39:52

给定源程序: 

  #include <stdio.h> 

  double fun ( int n ) 

  { double result = 1.0 ; 

  if n = = 0 

  return 1.0 ; 

  while( n >1 && n < 170 ) 

  result *= n-- 

  return result ; 

  } 

  main ( ) 

  { int n ; 

  printf("Input N:") ; 

  scanf("%d", &n) ; 

  printf("\n\n%d! =%lf\n\n", n, fun(n)) ; 

  } 

  解题思路: 

  第一处:条件语句书写格式错误,应改为:if (n==0)。 

  第二处:语句后缺少分号。 

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

  请编写一个函数fun,它的功能是:将一个数字字符串转换为一个整数(不得调用C语言提供的将字符串转换为整数的。例如,若输入字符串"-1234",则函数把它转换为整数值 -1234。函数fun中给出的语句仅供参考。 

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

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

  给定源程序: 

  #include <stdio.h> 

  #include <string.h> 

  long fun ( char *p) 

  {int i, len, t; /* len为串长,t为正负标识 */ 

  long x=0; 

  len=strlen(p); 

  if(p[0]==’-’) 

  { t=-1; len--; p++; } 

  else t=1; 

  /* 以下完成数字字符串转换为一个数字 */ 

  return x*t; 

  } 

  main() /* 主函数 */ 

  { char s[6]; 

  long n; 

  printf("Enter a string:\n") ; 

  gets(s); 

  n = fun(s); 

  printf("%ld\n",n); 

  NONO ( ); 

  } 

  NONO ( ) 

  {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ 

  FILE *fp, *wf ; 

  int i ; 

  char s[20] ; 

  long n ; 

  fp = fopen("c:\\test\\in.dat","r") ; 

  wf = fopen("c:\\test\\out.dat","w") ; 

  for(i = 0 ; i < 10 ; i++) { 

  fscanf(fp, "%s", s) ; 

  n = fun(s); 

  fprintf(wf, "%ld\n", n) ; 

  } 

  fclose(fp) ; 

  fclose(wf) ; 

  } 

  解题思路: 

  本题是将一个数字字符串转换为一个整数。

触屏版 电脑版
3773考试网 琼ICP备12003406号-1