liftchampion

314314

Oct 23rd, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <stdio.h>
  4. #include <unistd.h>
  5.  
  6. int         ft_putchar(char c)
  7. {
  8.     write(1, &c, 1);
  9.     return (0);
  10. }
  11.  
  12. int ft_check_base(char *base)
  13. {
  14.     unsigned int len;
  15.     char all_chars[256];
  16.     int i;
  17.    
  18.     len = 0;
  19.     i = 0;
  20.     while (i < 256)
  21.         all_chars[i++] = 0;
  22.     while (*base++)
  23.     {
  24.         len++;
  25.         if (*(base - 1) == '+' || *(base - 1) == '-')
  26.             return (0);
  27.         all_chars[*(base - 1)]++;
  28.         if (all_chars[*(base - 1)] > 1)
  29.             return (0);
  30.     }
  31.     if (len <= 1)
  32.         return (0);
  33.     return (len);
  34. }
  35.  
  36. int ft_check_str(char *str, char *base, int *len)
  37. {
  38.     int was_in;
  39.     char *base_begin;
  40.    
  41.     base_begin = base;
  42.     while (str[*len])
  43.     {
  44.         if (str[*len] == '-' || str[*len] == '+')
  45.         {
  46.             (*len)++;
  47.             continue;
  48.         }
  49.         base = base_begin;
  50.         was_in = 0;
  51.         while (*base)
  52.         {
  53.             if (*base == str[*len])
  54.                 was_in = 1;
  55.             base++;
  56.         }
  57.         if (!was_in)
  58.             return (0);
  59.         (*len)++;
  60.     }
  61.     return (1);
  62. }
  63.  
  64. int ft_pow(int a, int b)
  65. {
  66.     int res;
  67.     int i;
  68.  
  69.     i = 0;
  70.     res = 1;
  71.     if (a == 0)
  72.     {
  73.         return (0);
  74.     }
  75.     while (i < b)
  76.     {
  77.         res *= a;
  78.         i++;
  79.     }
  80.     return (res);
  81. }
  82.  
  83. int ft_get_pos(char c, char *base)
  84. {
  85.     int i = 0;
  86.     while (base[i] && c != base[i])
  87.     {
  88.         i++;
  89.     }
  90.     return (i);
  91. }
  92.  
  93. int ft_atoi_base(char *str, char *base)
  94. {
  95.     printf("@");
  96.     int res;
  97.     int sign;
  98.     int len;
  99.     int radix;
  100.     int i;
  101.  
  102.     len = 0;
  103.     res = 0;
  104.     sign = 1;
  105.     printf("@");
  106.     radix = ft_check_base(base);
  107.     printf("#");
  108.     i = 0;
  109.     if (radix == 0 || ft_check_str(str, base, &len) == 0)
  110.         return (0);
  111.     if (*str == '-')
  112.         sign = -1;
  113.     if (*str == '-' || *str == '+')
  114.         str++;
  115.     printf("*");
  116.     while (str[i++] != '\0' && str[i - 1] != '+' && str[i - 1] != '-');
  117.     while (*str != '\0' && *str != '+' && *str != '-')
  118.     {
  119.         res += ft_pow(radix, i) * ft_get_pos(*str++, *base);
  120.         i--;
  121.     }
  122.    
  123.    
  124.    
  125.     return (res);
  126. }
  127.  
  128. int main()
  129. {
  130.    
  131.     printf("%d", ft_atoi_base("12", "0123456789"));
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment