Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdio.h>
- #include <unistd.h>
- int ft_putchar(char c)
- {
- write(1, &c, 1);
- return (0);
- }
- int ft_check_base(char *base)
- {
- unsigned int len;
- char all_chars[256];
- int i;
- len = 0;
- i = 0;
- while (i < 256)
- all_chars[i++] = 0;
- while (*base++)
- {
- len++;
- if (*(base - 1) == '+' || *(base - 1) == '-')
- return (0);
- all_chars[*(base - 1)]++;
- if (all_chars[*(base - 1)] > 1)
- return (0);
- }
- if (len <= 1)
- return (0);
- return (len);
- }
- int ft_check_str(char *str, char *base, int *len)
- {
- int was_in;
- char *base_begin;
- base_begin = base;
- while (str[*len])
- {
- if (str[*len] == '-' || str[*len] == '+')
- {
- (*len)++;
- continue;
- }
- base = base_begin;
- was_in = 0;
- while (*base)
- {
- if (*base == str[*len])
- was_in = 1;
- base++;
- }
- if (!was_in)
- return (0);
- (*len)++;
- }
- return (1);
- }
- int ft_pow(int a, int b)
- {
- int res;
- int i;
- i = 0;
- res = 1;
- if (a == 0)
- {
- return (0);
- }
- while (i < b)
- {
- res *= a;
- i++;
- }
- return (res);
- }
- int ft_get_pos(char c, char *base)
- {
- int i = 0;
- while (base[i] && c != base[i])
- {
- i++;
- }
- return (i);
- }
- int ft_atoi_base(char *str, char *base)
- {
- printf("@");
- int res;
- int sign;
- int len;
- int radix;
- int i;
- len = 0;
- res = 0;
- sign = 1;
- printf("@");
- radix = ft_check_base(base);
- printf("#");
- i = 0;
- if (radix == 0 || ft_check_str(str, base, &len) == 0)
- return (0);
- if (*str == '-')
- sign = -1;
- if (*str == '-' || *str == '+')
- str++;
- printf("*");
- while (str[i++] != '\0' && str[i - 1] != '+' && str[i - 1] != '-');
- while (*str != '\0' && *str != '+' && *str != '-')
- {
- res += ft_pow(radix, i) * ft_get_pos(*str++, *base);
- i--;
- }
- return (res);
- }
- int main()
- {
- printf("%d", ft_atoi_base("12", "0123456789"));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment