# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    Makefile                                           :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: jayang <[email protected]>                      +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2022/02/17 13:05:15 by jayang            #+#    #+#              #
#    Updated: 2022/02/17 18:33:27 by jayang           ###   ########.fr        #
#                                                                              #
# **************************************************************************** #

CC = gcc
CHLAGS = -Wall -Wextra -Werror

TARGET = do-op
SRCS = srcs/main.c srcs/ft_atoi.c srcs/ft_putstr.c srcs/ft_putchar.c srcs/ft_calculrator.c srcs/ft_putnbr.c
OBJS = ${SRCS:.c=.o}
HEADER = includes

%.o : %.c
	${CC} ${CHLAGS} -c -I ${HEADER} $< -o $@

${TARGET} : ${OBJS}
	${CC} ${CHLAGS} $^ -o $@

all : ${TARGET}

clean:
	rm -rf ${OBJS}

fclean: clean
	rm -rf ${TARGET}

re: fclean all
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   oper.h                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jayang <[email protected]>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/02/17 15:17:25 by jayang            #+#    #+#             */
/*   Updated: 2022/02/17 19:15:25 by jayang           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef OPER_H
# define OPER_H

typedef void	(*t_calc_ptr)(int, int);

#endif
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   main.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jayang <[email protected]>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/02/17 13:42:52 by jayang            #+#    #+#             */
/*   Updated: 2022/02/17 21:05:22 by jayang           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "oper.h"

void	ft_putchar(char c);
void	ft_add(int a, int b);
void	ft_sub(int a, int b);
void	ft_mul(int a, int b);
void	ft_div(int a, int b);
void	ft_mod(int a, int b);
int		ft_atoi(char *str);

int	ft_exception_handling(int argc, char **argv)
{
	if (argc != 4)
		return (0);
	if (argv[1] == 0 && argv[3] == 0)
		return (0);
	if (!(*argv[2] == '+' || *argv[2] == '-' || *argv[2] == '*' || \\
		*argv[2] == '/' || *argv[2] == '%'))
	{
		ft_putchar('0');
		ft_putchar('\\n');
		return (0);
	}
	return (1);
}

void	ft_calculrator(int nb1, int nb2, char oper)
{
	t_calc_ptr	calc;

	if (oper == '+')
		calc = ft_add;
	else if (oper == '-')
		calc = ft_sub;
	else if (oper == '*')
		calc = ft_mul;
	else if (oper == '/')
		calc = ft_div;
	else
		calc = ft_mod;
	calc(nb1, nb2);
}

int	main(int args, char *argv[])
{
	int		nb1;
	int		nb2;

	if (ft_exception_handling(args, argv))
	{
		nb1 = ft_atoi(argv[1]);
		nb2 = ft_atoi(argv[3]);
		ft_calculrator(nb1, nb2, *(argv[2]));
		ft_putchar('\\n');
	}
	return (0);
}
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_putstr.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jayang <[email protected]>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/02/17 15:55:39 by jayang            #+#    #+#             */
/*   Updated: 2022/02/17 17:41:16 by jayang           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

void	ft_putchar(char c);

void	ft_putstr(char *str)
{
	while (*str)
	{
		ft_putchar(*str);
		str++;
	}
}
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_putnbr.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jayang <[email protected]>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/02/17 15:52:07 by jayang            #+#    #+#             */
/*   Updated: 2022/02/17 19:08:13 by jayang           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

void	ft_putstr(char *str);
void	ft_putchar(char c);

void	ft_putnbr(int nb)
{
	if (nb == -2147483648)
	{
		ft_putstr("-2147483648");
		return ;
	}
	else if (nb < 0)
	{
		ft_putchar('-');
		nb *= -1;
	}
	if (nb > 0 && (nb / 10 != 0))
		ft_putnbr(nb / 10);
	ft_putchar(nb % 10 + '0');
}
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_putchar.c                                       :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jayang <[email protected]>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/02/17 13:44:28 by jayang            #+#    #+#             */
/*   Updated: 2022/02/17 18:34:03 by jayang           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <unistd.h>

void	ft_putchar(char c)
{
	write(1, &c, 1);
}
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_calculrator.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jayang <[email protected]>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/02/17 15:16:51 by jayang            #+#    #+#             */
/*   Updated: 2022/02/17 19:22:52 by jayang           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "oper.h"

void	ft_putnbr(int nb);
void	ft_putstr(char *str);

void	ft_add(int a, int b)
{
	ft_putnbr(a + b);
}

void	ft_sub(int a, int b)
{
	ft_putnbr(a - b);
}

void	ft_mul(int a, int b)
{
	ft_putnbr(a * b);
}

void	ft_div(int a, int b)
{
	if (b != 0)
		ft_putnbr(a / b);
	else
		ft_putstr("Stop : division by zero");
}

void	ft_mod(int a, int b)
{
	if (b != 0)
		ft_putnbr(a % b);
	else
		ft_putstr("Stop : modulo by zero");
}
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_atoi.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jayang <[email protected]>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/02/17 14:19:32 by jayang            #+#    #+#             */
/*   Updated: 2022/02/17 21:08:24 by jayang           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

char	*ft_process_str(char *str, int *minus)
{
	while (*str == ' ' || (9 <= *str && *str <= 13))
		str++;
	*minus = 1;
	while (*str == '+' || *str == '-')
	{
		if (*str == '-')
			*minus *= -1;
		str++;
	}
	return (str);
}

int	ft_atoi(char *str)
{
	int	minus;
	int	result;

	str = ft_process_str(str, &minus);
	result = 0;
	while ('0' <= *str && *str <= '9')
	{
		result = (result * 10) + (*str - '0');
		str++;
	}
	return (minus * result);
}