组织机构号 校验算法

int    check_zzjgm(char *in_data)
{
    int  right[] = {3, 7, 9, 10, 5, 8, 4, 2};
    int  dwTmp   = 0;
    int  i       = 0;
    int  sum     = 0;
    int  index   = 0;
    char C9      = 0;

    if(9 != strlen(in_data) && 10 != strlen(in_data))
    {
        return -1;
    }

    for(i=0; i<8; i++)
    {
        if('A' <= in_data[i] && 'Z' >= in_data[i])
        {
            dwTmp = (in_data[i] - 'A' + 10) * right[i]; /* A=10 B=11 ... Z=35 */
        }
        else if('0' <= in_data[i] && '9' >= in_data[i])
        {
            dwTmp = (in_data[i] - '0') * right[i];
        }
        else
        {
            return -1;
        }
        sum += dwTmp;
    }

    switch(index = 11 - sum % 11)
    {
    case 10: C9 = 'X'; break;
    case 11: C9 = '0'; break;
    default: C9 = '0' + index; break;    
    }

    /* printf("in_data=%s C9=%c\n", in_data, C9); */
    return (C9 != in_data[ strlen(in_data) - 1]) ? -1 : 0;
}

你可能感兴趣的:(C,C++,c)