要求:输入"g2sh8iBof"输出值 10431

解答:考察字符串数组基础支持,这里还没学到指针,答案里没有使用指针。

#include <stdio.h>
#include <string.h>
#include <math.h>

void main(void)
{
    char szHex[4],szRaw[] = "g2sh8iBof";

    int i,j=0,iRet = 0 ,len = strlen(szRaw);

    for (i = 0 ; i < len; i++){
        
        if(//anscii 0-9
            szRaw[i] >= '0' && szRaw[i] <= '9' 
            //anscii A-F
            || szRaw[i] >= 'A' && szRaw[i] <= 'F'
            //anscii a-f
            || szRaw[i] >= 'a' && szRaw[i] <= 'f'
            ){
            szHex[j++] = szRaw[i];
        }

        
    }
    
    
    for(i = 0; i < j; i++){
        if(szHex[i] >= '0' && szHex[i] <= '9' )
            
            iRet += ( szHex[i] - '0' ) * pow(16, 4-i-1);
        
        if(szHex[i] >= 'A' && szHex[i] <= 'F')
            
            iRet += (szHex[i] - 'A' + 10) * pow(16, 4-i-1);                
        
        if(szHex[i] >= 'a' && szHex[i] <= 'f')
            
            iRet += (szHex[i] - 'a' + 10) * pow(16, 4-i-1);
    }
    
    printf("%d\n",iRet);
}

在线编译测试:https://ideone.com/ISg1Cr

posted on 2018-06-08 09:16  你不知道的浪漫  阅读(2291)  评论(0编辑  收藏  举报