给定程序中函数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);