2013年计算机二级C语言上机题库十一_第2页

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2013年4月12日
  改错题
  下列给定程序中,函数FUN的功能是:将一个由八进制数字字符组成的字符串转换为与其面值相等的十进制整数。规定输入的字符串最多只能包含5位8进制数字。例如,若输入77777,则输出将是32767。
  请改正程序中的错误,使它能得出正确结果。
  注意:不要改动MAIN函数,不得增行或删行,也不要更改程序的结构!
  试题程序:#include
  #include
  #include
  int fun(char *p)
  {
  int n;
  /********found********/
  n=*p-'o';
  p++;
  /********found********/
  while (*p != 0)
  {
  n=n*7+*p-'o';
  p++;
  }
  return n;
  }
  main()
  {
  char s[6];
  int i;
  int n;
  printf("Enter a string (octal digits): ");
  gets(s);
  if (strlen(s) > 5)
  {
  printf("Error:string too longer !\n\n");
  exit(0);
  }
  for (i=0; s[i]; i++)
  if (s[i]<'0' || s[i]>'7')
  {
  printf("Error: %c not is octal digits!\n\n", s[i]);
  exit(0);
  }
  printf("The original string: ");
  puts(s);
  n = fun(s);
  printf("\n%s is convered to intege number: %d\n\n", s, n);
  }
  第1处:n=*p-‘o’;应改为n=*p-‘0’;
  第2处:n=n*7+*p-‘o’;应改为n=n*8+*p-‘0’;

相关文章