问题
问答题
给定程序中函数fun的功能是:将一个由八进制数字字符组成的字符串转换为与其值相等的十进制整数。规定输入的字符串最多只能包含5位八进制数字字符。
例如,若输入:77777,则输出将是:32767。
请改正程序中的错误,使它能得到正确结果。
[注意] 不要改动main函数,不得增行或删行,也不得更改程序的结构。
[试题源程序]
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int fun(char *p)
int n;
/**********found**********/
n=*p-’o’;
p++;
while(*p!=0)
/**********found**********/
n=n*8+*p-’o’;
p++;
return n;
main()
char s[6]; int i; int n;
printf("Enter a string(Ocatal 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 ocatal digits!\n\n", s[i]);
exit(0);
printf("The original string:");
puts(s);
n=fun(s);
printf("\n%s iS convered to integer number: %d\n\n", s, n);
答案
参考答案:(1)错误:*p
正确:*p
(2)错误:’o’;
正确:’o’;
解析: 错误1:函数的形参用的是小写p,而函数中调用参数时用了大写p, *p错写成了*p。
错误2:编译后可知,’o’错写成了’o’。