int ft_atoi_base(char *str, char *base)

// 쉐도코드 
#include <stdio.h>

int ft_check(char *str, char *base)
{
    if (base = '\\0')
    while (base[i] != '\\0')
    {
        while (base[j] != '\\0')
        if base[i] == base[j] 
        return 0;
    }
    if base == '+' || base == '-';
}

int ft_atoi_base(char *str, char *base)
{
    i * strlen(base) + [i+1]
}
#include <stdio.h>

int ft_strlen(char *str)
{
    int cnt;

    cnt = 0;
    while (str[cnt] != '\\0')
    {
        cnt++;        
    }
    return (cnt);
}

int check(char *base) // problem;
{
    int i;
    int j;
    int cnt;
    
    i = 0;
    cnt = 1;
    if (base[0] == '\\0')
        cnt = 0;
    while (base[i] != '\\0')
    {
        j = i + 1;
        while (base[j] != '\\0')
        {
            if (base[i] == base[j])
                cnt = 0;
            j++;
        }
        i++;
    }
    return (cnt);
}

int ft_search(char str, char *base) // edited;
{
    int j;

    j = 0;
    while (base[j] != '\\0')
    {
        if (str == base[j])
        {
            return (j);
            break;
        }
        j++;
    }
    return (j);
}

int ft_atoi_base(char *str, char *base)
{
    int i;
    int minus;
    int index;
    int result;
    
    i = 0;
    minus = 1;
    index = 0;
    result = 0;
    if (check(base) == 0)
        result = 0;
    while (str[i] != '\\0')
    {
        index = ft_search(str[i], base);
        result = ft_strlen(base) * result;
        result = result + index * minus;
        i++;
    }
    return (result);
}

int main() {
    printf("%d",ft_atoi_base("1", "0123456789"));
}

코드는 정상적으로 동작하지만, base의 사이즈가 1이거나 base에 +, -, white_space가 포함되어 있는 경우에 0을 리턴하도록 하는 것은 아직 구현하지 않았다.

추가적으로 음수를 넣으면 수가 잘못 나오는 오류가 존재하는데 수정해야 할 필요가 있다.

지금 다시 체크해보니 base에 중복된 글자가 있거나 +, -, white_space가 있어도 진법 변환이 이루어진다.

알고보니 check 함수에서 base[i], base[j]가 아닌 i, j를 써야 했다.

int check(char *base) // problem;
{
    int i;
    int j;
    int base_len = ft_strlen(base);
    
    i = 0;
    while (i < (base_len - 1))
    {
        j = i + 1;
        while (j < base_len)
        {
            if (base[i] == base[j])
            {
                return (0);
                break ;
            }
            j++;
        }
        i++;
    }
    return (1);
}

수정한 코드 1

#include <stdio.h>

int ft_strlen(char *str)
{
    int cnt;

    cnt = 0;
    while (str[cnt] != '\\0')
    {
        cnt++;        
    }
    return (cnt);
}

int check(char *base)
{
    int i;
    int j;
    int base_len = ft_strlen(base);
    
    i = 0;
    while (i < (base_len - 1))
    {
        j = i + 1;
        while (j < base_len)
        {
            if (base[i] == base[j])
            {
                return (0);
                break ;
            }
            j++;
        }
        i++;
    }
    return (1);
}

int ft_search(char str, char *base) // edited;
{
    int j;

    j = 0;
    while (base[j] != '\\0')
    {
        if (str == base[j])
        {
            return (j);
            break;
        }
        j++;
    }
    return (j);
}

int ft_atoi_base(char *str, char *base)
{
    int i;
    int minus;
    int index;
    int result;
    
    i = 0;
    minus = 1;
    index = 0;
    result = 0;
    if (check(base) == 0)
        result = 0;
    else
    {
        while (str[i] != '\\0')
        {
            index = ft_search(str[i], base);
            result = ft_strlen(base) * result;
            result = result + index * minus;
            i++;
        }
    }
    return (result);
}

int main() {
    printf("%d",ft_atoi_base("64", "eerg"));
}

base에 중복된 문자가 있을 때 0을 출력하도록 수정했다.

문제는 추가 조건을 작성하면 함수가 길어진다는 점인데, 이건 check02 함수를 만들어서 추가 조건을 적용해야 할 것 같다.

수정한 코드 2

#include <stdio.h>

int ft_strlen(char *str)
{
    int cnt;

    cnt = 0;
    while (str[cnt] != '\\0')
    {
        cnt++;        
    }
    return (cnt);
}

int check01(char *base)
{
    int i;
    int j;
    int base_len = ft_strlen(base);
    
    i = 0;
    while (i < (base_len - 1))
    {
        j = i + 1;
        while (j < base_len)
        {
            if (base[i] == base[j])
            {
                return (0);
                break ;
            }
            j++;
        }
        i++;
    }
    return (1);
}

int check02(char *base)
{
    int i;

    i = 0;
    if (ft_strlen(base) <= 1)
    {
        return (0);
    }
    while(base[i] != '\\0')
    {   
        if ((base[i] >= 9 && base[i] <= 13) || (base[i] == 32))
        {
            return (0);
            break;
        }
        else if (base[i] == '-' || base[i] == '+')
        {
            return (0);
            break;
        }
        i++;
    }    
    return (1);
}

int ft_search(char str, char *base) // edited;
{
    int j;

    j = 0;
    while (base[j] != '\\0')
    {
        if (str == base[j])
        {
            return (j);
            break;
        }
        j++;
    }
    return (j);
}

int ft_atoi_base(char *str, char *base)
{
    int i;
    int minus;
    int index;
    int result;
    
    i = 0;
    minus = 1;
    index = 0;
    result = 0;
    if ((check01(base) == 0) || (check02(base) == 0))
        result = 0;
    else
    {
        while (str[i] != '\\0')
        {
            index = ft_search(str[i], base);
            result = ft_strlen(base) * result;
            result = result + index * minus;
            i++;
        }
    }
    return (result);
}

int main() {
    printf("%d",ft_atoi_base("10101", "01"));
}

생각해보니 아직 음수 값 처리랑 src에 dest에 없는 문자가 있을 때, 그 전까지만 읽는 것을 구현을 하지 못했다.