c08 - KimTaebin-ai/study_posts GitHub Wiki

ํ—ค๋”ํŒŒ์ผ ์ž‘์„ฑ ๋ฐฉ๋ฒ• ์ดํ•ด๋ฅผ ์š”๊ตฌ

ํŠนํžˆ norminette ๊ทœ์น™์„ ๋‹ค์‹œ ํ™•์ธํ•ด๋ด์•ผ ํ•œ๋‹ค

ex00

#ifndef FT_H
# define FT_H

void	ft_putchar(char c);
void	ft_swap(int *a, int *b);
void	ft_putstr(char *str);
int		ft_strlen(char *str);
int		ft_strcmp(char *s1, char *s2);

#endif

ex01

ifndef FT_BOOLEAN_H
# define FT_BOOLEAN_H

# include <unistd.h>
# define TRUE 1
# define FALSE 0
# define SUCCESS 0

typedef int	t_bool;
void	ft_putstr(char *str);
t_bool	ft_is_even(int nbr);

# define EVEN(nbr) (nbr % 2 == 0)
# define EVEN_MSG "I have an even number of arguments.\n"
# define ODD_MSG "I have an odd number of arguments.\n"

#endif

๊ฐœํ–‰๋ฌธ์ž ํ•„์ˆ˜

๋˜ํ•œ norminette์—๋Š” ifndef์™€ endif๊ตฌ๋ฌธ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋งคํฌ๋กœ ๊ตฌ๋ฌธ์—์„œ ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ•ด์•ผํ•œ๋‹ค

ํ™•์ธํ•˜๋Š” norminette์ด ๋‹ค๋ฅด๋‹ˆ ์–ด๋‘ ์˜ ๊ฒฝ๋กœ์˜ pdf๋กœ ํ™•์ธ ํ•„์š”ํ•˜๊ธด ํ•จ

์ข€ ์žฌ๋ฏธ์žˆ๋Š”๊ฒŒ ๋™๋ฃŒํ‰๊ฐ€๋กœ ๋ˆˆ ๋””๋ฒ„๊น…์„ ํ•˜๋ผ๊ณ  ๋ช…์‹œ๊ฐ€ ๋˜์–ด์žˆ๊ธฐ๋„ ํ•˜๋‹ค.

์ด ๋•Œ๋ถ€ํ„ฐ ํ‰๊ฐ€๋ฅผ ์ข€ ๊ผผ๊ผผํ•˜๊ฒŒ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ์•…ํ”Œ์ด ์ข€ ์žˆ๋‹ค... ๋ญ๊ฐ€ ์ข‹์€ ํ‰๊ฐ€์ผ๊นŒ

ex02

#ifndef FT_ABS_H
# define FT_ABS_H

# define ABS(nb) ((nb) < 0 ? -(nb) : (nb))

#endif

ABS๊ฐ€ ๋ญ”์ง€ ํ•œ ๋ฒˆ์— ๋ชป ๋– ์˜ฌ๋ฆฌ๋Š” ์‚ฌ๋žŒ์ด ๋‚˜ ๋ฐ–์— ์—†๋Š” ๋“ฏ;

์˜์–ด ๊ณต๋ถ€ ์ข€ ํ•˜์ž.....

ex03

#ifndef FT_POINT_H
# define FT_POINT_H

struct	s_point
{
	int	x;
	int	y;
};
typedef struct s_point	t_point;
void	set_point(t_point *point);

#endif

์—ฌ๊ธฐ์„œ๋ถ€ํ„ด ๊ตฌ์กฐ์ฒด์— ๋Œ€ํ•œ ์ดํ•ด ํ•„์š”

ex04

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int	ft_strlen(char *str)
{
	int	count;

	count = 0;
	while (str[count])
		count++;
	return (count);
}

char	*ft_strndup(char *src)
{
	int		i;
	char	*new_str;

	new_str = (char *)malloc(sizeof(char) * ft_strlen(src) + 1);
	if (new_str == NULL)
		return (NULL);
	i = 0;
	while (src[i])
	{
		new_str[i] = src[i];
		i++;
	}
	new_str[i] = '\0';
	return (new_str);
}

struct s_stock_str	*ft_strs_to_tab(int ac, char **av)
{
	int			i;
	t_stock_str	temp;
	t_stock_str	*strs;

	strs = malloc(sizeof(t_stock_str) * (ac + 1));
	if (strs == NULL)
		return (NULL);
	i = 0;
	while (i < ac)
	{
		temp.size = ft_strlen(av[i]);
		temp.str = av[i];
		temp.copy = ft_strndup(av[i]);
		if (temp.copy == NULL)
			return (NULL);
		strs[i++] = temp;
	}
	strs[ac].str = 0;
	return (strs);
}

์ด ๋ฌธ์ œ๋Š” ๊ตฌ์กฐ์ฒด ํ•จ์ˆ˜๋ฅผ ์ œ์ž‘ํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. ๊ตฌ์กฐ์ฒด ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜์—ฌ ๊ทธ ๋ฐฐ์—ด์•ˆ์— ์ ์ ˆํ•œ ๊ฐ’๋“ค์„ ์ง‘์–ด๋„ฃ๋Š”๊ฒƒ์ด๋‹ค. ์ด ๋ฌธ์ œ๋Š” ft_strndup์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ด์ „ ๋ฌธ์ œ์—์„œ ๊ตฌํ˜„ํ•œ strdup์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ copy๋ฅผ ํ•  ๋ฌธ์ž์—ด์˜ ์ •ํ™•ํ•œ ํฌ๊ธฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

ex05

#include <unistd.h>
#include "ft_stock_str.h"

void	ft_putchar(char c)
{
	write(1, &c, 1);
}

void	ft_putnbr(int nb)
{
	long	i;

	i = nb;
	if (i < 0)
	{
		ft_putchar('-');
		i = i * -1;
	}
	if (i > 9)
	{
		ft_putnbr(i / 10);
		ft_putnbr(i % 10);
	}
	else
		ft_putchar(i + '0');
}

void	ft_putstr(char *str)
{
	int	i;

	i = 0;
	while (*str)
	{
		ft_putchar(*str);
		str++;
	}
	ft_putchar('\n');
}

void	ft_show_tab(struct s_stock_str *par)
{
	int	i;

	i = 0;
	while (par[i].str)
	{
		ft_putstr(par[i].str);
		ft_putnbr(par[i].size);
		ft_putchar('\n');
		ft_putstr(par[i].copy);
		i++;
	}
}
โš ๏ธ **GitHub.com Fallback** โš ๏ธ