Advertisement
aketo

Untitled

Jul 17th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.99 KB | None | 0 0
  1. # **************************************************************************** #
  2. #                                                           LE - /             #
  3. #                                                               /              #
  4. #    Makefile                                         .::    .:/ .      .::    #
  5. #                                                  +:+:+   +:    +:  +:+:+     #
  6. #    By: humoyne <marvin@le-101.fr>                 +:+   +:    +:    +:+      #
  7. #                                                  #+#   #+    #+    #+#       #
  8. #    Created: 2018/07/16 13:15:14 by humoyne      #+#   ##    ##    #+#        #
  9. #    Updated: 2018/07/16 14:29:36 by humoyne     ###    #+. /#+    ###.fr      #
  10. #                                                          /                   #
  11. #                                                         /                    #
  12. # **************************************************************************** #
  13.  
  14. NAME = libft.a
  15. PATH_SRC = ./srcs/
  16. HEADER = ./includes/
  17. FLAG = -Wall -Wextra -Werror
  18. OPTION = -c -I $(HEADER)
  19. SRC = $(PATH_SRC)ft_putchar.c $(PATH_SRC)ft_putstr.c $(PATH_SRC)ft_strcmp.c \
  20. $(PATH_SRC)ft_strlen.c $(PATH_SRC)ft_swap.c
  21. OBJ = ft_putchar.o ft_putstr.o ft_strcmp.o ft_strlen.o ft_swap.o
  22.  
  23. all: $(NAME)
  24.     ar rc $(NAME) $(OBJ)
  25.  
  26. $(NAME):
  27.     gcc $(FLAG) $(OPTION) $(SRC)
  28.  
  29. clean:
  30.     /bin/rm -f $(OBJ)
  31.  
  32. fclean: clean
  33.     /bin/rm -f $(NAME)
  34.  
  35. re: fclean all
  36.     /bin/rm -f $(NAME) $(OBJ)
  37. /* ************************************************************************** */
  38. /*                                                          LE - /            */
  39. /*                                                              /             */
  40. /*   ft_foreach.c                                     .::    .:/ .      .::   */
  41. /*                                                 +:+:+   +:    +:  +:+:+    */
  42. /*   By: humoyne <marvin@le-101.fr>                 +:+   +:    +:    +:+     */
  43. /*                                                 #+#   #+    #+    #+#      */
  44. /*   Created: 2018/07/16 14:33:28 by humoyne      #+#   ##    ##    #+#       */
  45. /*   Updated: 2018/07/16 15:07:32 by humoyne     ###    #+. /#+    ###.fr     */
  46. /*                                                         /                  */
  47. /*                                                        /                   */
  48. /* ************************************************************************** */
  49.  
  50. void    ft_foreach(int *tab, int length, void (*f)(int))
  51. {
  52.     int i;
  53.  
  54.     i = 0;
  55.     while (i < length)
  56.     {
  57.         f(tab[i]);
  58.         i++;
  59.     }
  60. }
  61. /* ************************************************************************** */
  62. /*                                                          LE - /            */
  63. /*                                                              /             */
  64. /*   ft_map.c                                         .::    .:/ .      .::   */
  65. /*                                                 +:+:+   +:    +:  +:+:+    */
  66. /*   By: humoyne <marvin@le-101.fr>                 +:+   +:    +:    +:+     */
  67. /*                                                 #+#   #+    #+    #+#      */
  68. /*   Created: 2018/07/16 15:12:37 by humoyne      #+#   ##    ##    #+#       */
  69. /*   Updated: 2018/07/16 15:26:24 by humoyne     ###    #+. /#+    ###.fr     */
  70. /*                                                         /                  */
  71. /*                                                        /                   */
  72. /* ************************************************************************** */
  73.  
  74. #include <stdlib.h>
  75.  
  76. int *ft_map(int *tab, int length, int (*f)(int))
  77. {
  78.     int i;
  79.     int *s;
  80.  
  81.     i = 0;
  82.     if ((s = (int*)malloc(sizeof(int) * length)) == NULL)
  83.         return (NULL);
  84.     while (i < length)
  85.     {
  86.         s[i] = f(tab[i]);
  87.         i++;
  88.     }
  89.     return (s);
  90. }
  91. /* ************************************************************************** */
  92. /*                                                          LE - /            */
  93. /*                                                              /             */
  94. /*   ft_any.c                                         .::    .:/ .      .::   */
  95. /*                                                 +:+:+   +:    +:  +:+:+    */
  96. /*   By: humoyne <marvin@le-101.fr>                 +:+   +:    +:    +:+     */
  97. /*                                                 #+#   #+    #+    #+#      */
  98. /*   Created: 2018/07/16 15:28:13 by humoyne      #+#   ##    ##    #+#       */
  99. /*   Updated: 2018/07/16 16:03:09 by humoyne     ###    #+. /#+    ###.fr     */
  100. /*                                                         /                  */
  101. /*                                                        /                   */
  102. /* ************************************************************************** */
  103.  
  104. int ft_any(char **tab, int (*f)(char*))
  105. {
  106.     int i;
  107.  
  108.     i = 0;
  109.     while (tab[i] != '\0')
  110.     {
  111.         if (f(tab[i]) == 1)
  112.             return (1);
  113.         i++;
  114.     }
  115.     return (0);
  116. }
  117. /* ************************************************************************** */
  118. /*                                                          LE - /            */
  119. /*                                                              /             */
  120. /*   ft_count_if.c                                    .::    .:/ .      .::   */
  121. /*                                                 +:+:+   +:    +:  +:+:+    */
  122. /*   By: humoyne <marvin@le-101.fr>                 +:+   +:    +:    +:+     */
  123. /*                                                 #+#   #+    #+    #+#      */
  124. /*   Created: 2018/07/16 16:06:33 by humoyne      #+#   ##    ##    #+#       */
  125. /*   Updated: 2018/07/16 16:28:03 by humoyne     ###    #+. /#+    ###.fr     */
  126. /*                                                         /                  */
  127. /*                                                        /                   */
  128. /* ************************************************************************** */
  129.  
  130. int ft_count_if(char **tab, int (*f)(char*))
  131. {
  132.     int i;
  133.     int c;
  134.  
  135.     i = 0;
  136.     c = 0;
  137.     while (tab[i] != '\0')
  138.     {
  139.         if (f(tab[i]))
  140.             c++;
  141.         i++;
  142.     }
  143.     return (c);
  144. }
  145. /* ************************************************************************** */
  146. /*                                                          LE - /            */
  147. /*                                                              /             */
  148. /*   ft_is_sort.c                                     .::    .:/ .      .::   */
  149. /*                                                 +:+:+   +:    +:  +:+:+    */
  150. /*   By: humoyne <marvin@le-101.fr>                 +:+   +:    +:    +:+     */
  151. /*                                                 #+#   #+    #+    #+#      */
  152. /*   Created: 2018/07/16 16:32:22 by humoyne      #+#   ##    ##    #+#       */
  153. /*   Updated: 2018/07/16 16:48:12 by humoyne     ###    #+. /#+    ###.fr     */
  154. /*                                                         /                  */
  155. /*                                                        /                   */
  156. /* ************************************************************************** */
  157.  
  158. int ft_is_sort(int *tab, int length, int (*f)(int, int))
  159. {
  160.     int i;
  161.     int res;
  162.  
  163.     i = 0;
  164.     res = 0;
  165.     while (i < length - 1 && res <= 0)
  166.     {
  167.         res = f(tab[i], tab[i + 1]);
  168.         i++;
  169.     }
  170.     res = (res <= 0) ? 1 : 0;
  171.     return (res);
  172. }
  173. # **************************************************************************** #
  174. #                                                           LE - /             #
  175. #                                                               /              #
  176. #    Makefile                                         .::    .:/ .      .::    #
  177. #                                                  +:+:+   +:    +:  +:+:+     #
  178. #    By: humoyne <marvin@le-101.fr>                 +:+   +:    +:    +:+      #
  179. #                                                  #+#   #+    #+    #+#       #
  180. #    Created: 2018/07/16 17:28:25 by humoyne      #+#   ##    ##    #+#        #
  181. #    Updated: 2018/07/17 15:52:33 by humoyne     ###    #+. /#+    ###.fr      #
  182. #                                                          /                   #
  183. #                                                         /                    #
  184. # **************************************************************************** #
  185.  
  186. NAME = do-op
  187. PATH_SRC = ./
  188. HEADER = ./
  189. FLAG = -Wall -Wextra -Werror
  190. OPTION = -o $(NAME)
  191. SRC = $(PATH_SRC)op.c $(PATH_SRC)utility.c $(PATH_SRC)main.c
  192.  
  193. all: $(NAME)
  194.  
  195. $(NAME):
  196.     gcc $(FLAG) $(OPTION) $(SRC)
  197.  
  198. clean:
  199.     /bin/rm -f $(NAME)
  200.  
  201. fclean: clean
  202.     /bin/rm -f $(NAME)
  203.  
  204. re: fclean all
  205.     /bin/rm -f $(NAME) $(OBJ)
  206. /* ************************************************************************** */
  207. /*                                                          LE - /            */
  208. /*                                                              /             */
  209. /*   header.h                                         .::    .:/ .      .::   */
  210. /*                                                 +:+:+   +:    +:  +:+:+    */
  211. /*   By: humoyne <marvin@le-101.fr>                 +:+   +:    +:    +:+     */
  212. /*                                                 #+#   #+    #+    #+#      */
  213. /*   Created: 2018/07/17 07:36:37 by humoyne      #+#   ##    ##    #+#       */
  214. /*   Updated: 2018/07/17 08:38:06 by humoyne     ###    #+. /#+    ###.fr     */
  215. /*                                                         /                  */
  216. /*                                                        /                   */
  217. /* ************************************************************************** */
  218.  
  219. #ifndef HEADER_H
  220. # define HEADER_H
  221. # include <unistd.h>
  222.  
  223. void    ft_putchar(char c);
  224. void    ft_putstr(char *str);
  225. void    ft_putnbr(int nb);
  226. int     ft_atoi_da_shit(char *str);
  227. int     doop(char op, int nb1, int nb2);
  228. int     add(int a, int b);
  229. int     div(int a, int b);
  230. int     sub(int a, int b);
  231. int     multy(int a, int b);
  232. int     mod(int a, int b);
  233. int     doop(char op, int nb1, int nb2);
  234. int     is_numeric_char(char c);
  235.  
  236. #endif
  237. /* ************************************************************************** */
  238. /*                                                          LE - /            */
  239. /*                                                              /             */
  240. /*   main.c                                           .::    .:/ .      .::   */
  241. /*                                                 +:+:+   +:    +:  +:+:+    */
  242. /*   By: humoyne <marvin@le-101.fr>                 +:+   +:    +:    +:+     */
  243. /*                                                 #+#   #+    #+    #+#      */
  244. /*   Created: 2018/07/16 17:41:07 by humoyne      #+#   ##    ##    #+#       */
  245. /*   Updated: 2018/07/17 12:07:10 by humoyne     ###    #+. /#+    ###.fr     */
  246. /*                                                         /                  */
  247. /*                                                        /                   */
  248. /* ************************************************************************** */
  249.  
  250. #include "header.h"
  251.  
  252. int main(int argc, char **argv)
  253. {
  254.     int nb[2];
  255.     int result;
  256.  
  257.     if (argc == 4)
  258.     {
  259.         if (!argv[1] || !*argv[1] || !argv[2] || !*argv[2] || !argv[3] ||
  260.                 !*argv[3])
  261.         {
  262.             ft_putchar('\n');
  263.             return (0);
  264.         }
  265.         nb[0] = ft_atoi_da_shit(argv[1]);
  266.         nb[1] = ft_atoi_da_shit(argv[3]);
  267.         if ((argv[2][0] == '/') && !(nb[1]))
  268.             ft_putstr("Stop : division by zero\n");
  269.         else if ((argv[2][0] == '%') && !(nb[1]))
  270.             ft_putstr("Stop : modulo by zero\n");
  271.         else
  272.         {
  273.             result = doop(argv[2][0], nb[0], nb[1]);
  274.             ft_putnbr(result);
  275.             ft_putchar('\n');
  276.         }
  277.     }
  278.     return (0);
  279. }
  280. /* ************************************************************************** */
  281. /*                                                          LE - /            */
  282. /*                                                              /             */
  283. /*   op.c                                             .::    .:/ .      .::   */
  284. /*                                                 +:+:+   +:    +:  +:+:+    */
  285. /*   By: humoyne <marvin@le-101.fr>                 +:+   +:    +:    +:+     */
  286. /*                                                 #+#   #+    #+    #+#      */
  287. /*   Created: 2018/07/16 17:12:43 by humoyne      #+#   ##    ##    #+#       */
  288. /*   Updated: 2018/07/17 08:54:50 by humoyne     ###    #+. /#+    ###.fr     */
  289. /*                                                         /                  */
  290. /*                                                        /                   */
  291. /* ************************************************************************** */
  292.  
  293. #include "header.h"
  294.  
  295. int     add(int a, int b)
  296. {
  297.     return (a + b);
  298. }
  299.  
  300. int     div(int a, int b)
  301. {
  302.     return (a / b);
  303. }
  304.  
  305. int     sub(int a, int b)
  306. {
  307.     return (a - b);
  308. }
  309.  
  310. int     multy(int a, int b)
  311. {
  312.     return (a * b);
  313. }
  314.  
  315. int     mod(int a, int b)
  316. {
  317.     return (a % b);
  318. }
  319. /* ************************************************************************** */
  320. /*                                                          LE - /            */
  321. /*                                                              /             */
  322. /*   utility.c                                        .::    .:/ .      .::   */
  323. /*                                                 +:+:+   +:    +:  +:+:+    */
  324. /*   By: humoyne <marvin@le-101.fr>                 +:+   +:    +:    +:+     */
  325. /*                                                 #+#   #+    #+    #+#      */
  326. /*   Created: 2018/07/16 17:25:48 by humoyne      #+#   ##    ##    #+#       */
  327. /*   Updated: 2018/07/17 15:53:28 by humoyne     ###    #+. /#+    ###.fr     */
  328. /*                                                         /                  */
  329. /*                                                        /                   */
  330. /* ************************************************************************** */
  331.  
  332. #include "header.h"
  333.  
  334. void    ft_putchar(char c);
  335.  
  336. int     is_numeric_char(char c)
  337. {
  338.     if (c >= '0' && c <= '9')
  339.         return (1);
  340.     else if (c == '-' || c == '+')
  341.         return (1);
  342.     else
  343.         return (0);
  344. }
  345.  
  346. void    ft_putstr(char *str)
  347. {
  348.     int i;
  349.  
  350.     i = 0;
  351.     while (str[i])
  352.     {
  353.         ft_putchar(str[i]);
  354.         i++;
  355.     }
  356. }
  357.  
  358. void    ft_putnbr(int nb)
  359. {
  360.     if (nb == -2147483648)
  361.     {
  362.         ft_putstr("-2147483648");
  363.         return ;
  364.     }
  365.     if (nb < 0)
  366.     {
  367.         ft_putchar('-');
  368.         nb = nb * -1;
  369.     }
  370.     if (nb >= 10)
  371.     {
  372.         ft_putnbr(nb / 10);
  373.     }
  374.     ft_putchar((nb % 10) + '0');
  375. }
  376.  
  377. int     ft_atoi_da_shit(char *str)
  378. {
  379.     int i;
  380.     int negative;
  381.     int result;
  382.  
  383.     i = 0;
  384.     result = 0;
  385.     negative = 0;
  386.     while ((str[i] != '\0') && (is_numeric_char(str[i])))
  387.     {
  388.         result *= 10;
  389.         if (str[i] == '-' && !(negative))
  390.             negative = 1;
  391.         if (str[i] >= '0' && str[i] <= '9')
  392.             result += str[i] - '0';
  393.         i++;
  394.     }
  395.     if (negative)
  396.         return (result * -1);
  397.     return (result);
  398. }
  399.  
  400. int     doop(char op, int nb1, int nb2)
  401. {
  402.     int     result;
  403.     int     i;
  404.     int     (*opfunc[5])(int, int);
  405.     char    operator[5];
  406.  
  407.     opfunc[0] = &add;
  408.     operator[0] = '+';
  409.     opfunc[1] = &sub;
  410.     operator[1] = '-';
  411.     opfunc[2] = &div;
  412.     operator[2] = '/';
  413.     opfunc[3] = &multy;
  414.     operator[3] = '*';
  415.     opfunc[4] = &mod;
  416.     operator[4] = '%';
  417.     result = 0;
  418.     i = 0;
  419.     while (i < 5)
  420.     {
  421.         if (operator[i] == op)
  422.             result = opfunc[i](nb1, nb2);
  423.         i++;
  424.     }
  425.     return (result);
  426. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement