当前位置:网站首页>[pointer] octal to decimal

[pointer] octal to decimal

2022-07-06 14:36:00 |Light|

requirement

Make up a function , Realize octal conversion to decimal .( Use a pointer to achieve )

Code

/* *  This function is used to convert an octal number to a decimal number  *  Octal numbers are stored in formal parameters as strings a In the one-dimensional character array pointed to  *  Please return the calculated decimal number as a function value  */
int oct_to_dec(char* a)
{
    
    int d=0; 
    int i;
    int k=strlen(a)-1;
    for(i=0;i<20;i++)
    {
    
        if(a[i]=='\0')
        {
    
           break;
        }
        else
       {
    
            d = d + ((a[i]-48)*pow(8,k));
            k--;
       }
    }
    return d;
}

main function

int main()
 {
    
    char a[20];
    int n;
    gets(a);
    n = oct_to_dec(a);
    printf("%d",n);
    return 0;
 }   

test

Test input
1234
Output
668

原网站

版权声明
本文为[|Light|]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060918542098.html