Advertisement
Guest User

NAN MAIS OH !

a guest
Jun 26th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.28 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /*                                                                            */
  3. /*                                                        :::      ::::::::   */
  4. /*   ft_atoi_base.c                                     :+:      :+:    :+:   */
  5. /*                                                    +:+ +:+         +:+     */
  6. /*   By: bemoreau <bemoreau@student.42.fr>                    +#+  +:+       +#+        */
  7. /*                                                +#+#+#+#+#+   +#+           */
  8. /*   Created: 2019/06/26 22:36:46 by marvin            #+#    #+#             */
  9. /*   Updated: 2019/06/27 00:01:12 by bemoreau              ###   ########.fr       */
  10. /*                                                                            */
  11. /* ************************************************************************** */
  12.  
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17.  
  18. int  ft_strlen(char *str)
  19. {
  20.     int  len;
  21.  
  22.     len = 0;
  23.     if (!str)
  24.         return (0);
  25.     while (str[len])
  26.         len++;
  27.     return (len);
  28. }
  29.  
  30. int     ft_check(char *str, int nb)
  31. {
  32.     int i;
  33.     i = -1;
  34.     while (str[++i])
  35.     {
  36.         if (str[i] == '-' || str[i] == '+')
  37.             return (-1);
  38.         if (str[i] >= '0' && str[i] <= '9')
  39.         {
  40.             if (str[i] - '0' > nb-1)
  41.                 return (-1);
  42.         }
  43.         if (str[i] >= 'a')
  44.         {
  45.             if (str[i] - 'a' > nb-1)
  46.                 return (-1);
  47.         }
  48.     }
  49.     return (0);
  50. }
  51.  
  52. int ft_skipwspace(char *str)
  53. {
  54.     int i;
  55.  
  56.     i = 0;
  57.     while(str[i] == ' ' || str[i]== '\n' || str[i] == '\t'|| str[i] == '\v' ||
  58.                         str[i] == '\f' || str[i] == '\r')
  59.                         i++;
  60.     return(i);
  61. }
  62.  
  63. int     ft_power(int nb, int pow)
  64. {
  65.     int     result;
  66.  
  67.     result = 1;
  68.     if (pow == 0)
  69.         return (1);
  70.     while (--pow > 0 )
  71.         result *= nb;
  72.     return(result);
  73. }
  74.  
  75. int ft_convert_1(char *str, int base)
  76. {
  77.     int result;
  78.     int i;
  79.     int sign;
  80.     int pow;
  81.     int len;
  82.  
  83.     pow = 0;
  84.     sign = 0;
  85.     result = 0;
  86.     i = ft_skipwspace(str);
  87.         if (str[i] == '-')
  88.                 sign=1;
  89.         if (str[i] == '+' || str[i] == '-' )
  90.                 i++;
  91.     if (ft_check(str + i, base) == -1)
  92.         return(-1);
  93.     pow = ft_strlen(str+i);
  94.     while (str[i])
  95.     {
  96.         if (str[i] >= '0' && str[i] <= '9')
  97.             result = result + (str[i] - '0') * ft_power(base, pow);
  98.         if  (str[i] >= 'a' && str[i] <= ('a'+(base-11)))
  99.             result = result + (str[i] - 'a' +10) * ft_power(base, pow);
  100.         pow--;
  101.         i++;
  102.     }
  103.     if (sign == 1)
  104.         result = -result;
  105.     return (result);
  106. }
  107.  
  108.  
  109. void    ft_strlowercase(char *str)
  110. {
  111.     int i;
  112.     i=-1;
  113.     while(str[++i])
  114.     {
  115.         if (str[i] >= 'A' && str[i] <= 'Z')
  116.             str[i] = str[i] + 32;
  117.     }
  118. }
  119.  
  120. int main(int ac, char **av)
  121. {
  122.     char *str;
  123.  
  124.     if (ac == 2)
  125.     {
  126.         if (!(str = (char *)malloc(sizeof(char) * (ft_strlen(av[1])+1))))
  127.             return(0);
  128.         str=av[1];
  129.         ft_strlowercase(str);
  130.         printf("valeur de str(%s) en base (%d) est = %d\n", str, 2, ft_convert_1(str, 2));
  131.     }
  132.     return(0);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement