c00 - KimTaebin-ai/study_posts GitHub Wiki

๊ธฐ๋ณธ์ ์ธ write ํ•จ์ˆ˜์™€ ์กฐ๊ฑด๋ฌธ์„ ๋‹ค๋ฃจ๋Š” ๋ฌธ์ œ

ex00

#include <unistd.h>

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

write ํ•จ์ˆ˜๋Š” unistd์—์„œ ์ง€์›ํ•˜๋Š” ํ‘œ์ค€ ์ถœ๋ ฅํ•จ์ˆ˜๋กœ ๋ฌธ์ž์—ด๋กœ๋งŒ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. write (a, b, c)๋Š” a๋Š” ์ถœ๋ ฅ๋ชจ๋“œ๋กœ, 0,1,2 ์ค‘์— ํ•˜๋‚˜์˜ ์ธ์ž๋ฅผ ๊ฐ–๋Š”๋ฐ 0,1์€ ํ‘œ์ค€์ž…์ถœ๋ ฅ์ด๊ณ , 2๋Š” ํ‘œ์ค€์˜ค๋ฅ˜์ด๋‹ค ๋”ฐ๋ผ์„œ ์•ž์œผ๋กœ ์ถœ๋ ฅ์„ ํ•ด์•ผํ•˜๋Š” ๊ฑฐ์˜ ๋ชจ๋“  ๋ฌธ์ œ์—์„œ๋Š” a์— ๋“ซ์–ด๊ฐˆ ์ˆซ์ž๋Š” 1์ผ ๊ฒƒ์ด๋‹ค. b๋Š” ๋‚ด๊ฐ€ ์ถœ๋ ฅํ•  ๋ฐ์ดํ„ฐ์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฅดํ‚ค๋Š” ๊ฒƒ์ด๋‹ค. c๋Š” ์ถœ๋ ฅํ•  ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

์ด ์ •๋„๋งŒ ์ดํ•ดํ•˜๊ณ  ๋‹ค์Œ ๋ฌธ์ œ๋กœ ๋„˜์–ด๊ฐ€์ž.

ex01

#include <unistd.h>

void	ft_print_alphabet(void)
{
	char	alpha;

	alpha = 'a';
	while (alpha <= 'z')
	{
		write(1, &alpha, 1);
		alpha++;
	}
}

์•ŒํŒŒ๋ฒณ์„ ์ถœ๋ ฅํ•ด์•ผํ•˜๋ฏ€๋กœ, ์•„์Šคํ‚ค ์ฝ”๋“œ๊ฐ’์„ ์ด์šฉํ•œ ์ถœ๋ ฅ์ด๋‹ค. ์ด์ „ ๋ฌธ์ œ์—์„œ ์‚ฌ์šฉํ•œ writeํ•จ์ˆ˜๋ฅผ ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ๋ฐ˜๋ณต๋ฌธ์€ ์™€์ผ๋ฌธ๋งŒ ํ—ˆ์šฉ๋œ๋‹ค. norminette๋ฅผ ํ•ญ์ƒ ์ฃผ์˜ํ•  ๊ฒƒ

ex02

#include <unistd.h>

void	ft_print_reverse_alphabet(void)
{
	char	a;

	a = 'z';
	while (a >= 'a')
	{
		write (1, &a, 1);
		a--;
	}
}

ex03

#include <unistd.h>

void	ft_print_numbers(void)
{
	char	n;

	n = '0';
	while (n <= '9')
	{
		write(1, &n, 1);
		n++;
	}
}

์•ž์„œ ์„ค๋ช…ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ writeํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด๋กœ๋งŒ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๊ธฐ์—, ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ”์•ผํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ๋‚˜๋Š” atoi ๊ตฌํ˜„์„ ๋– ์˜ฌ๋ ธ๋Š”๋ฐ ๋ฌธ์ œ์—์„œ๋Š” all digit์„ ์ถœ๋ ฅํ•˜๋ผ๊ณ  ๋ช…์‹œ๋˜์–ด ์žˆ๊ธฐ์— 012345789๋งŒ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค. ๊ทน๋‹จ์ ์œผ๋กœ๋Š” ๋ฐ˜๋ณต๋ฌธ ์—†์ด write(1, "0123456789", 10); ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•ด๋„ ๊ธฐ๊ณ„ํŽป๊ฐ€์—์„œ ํ†ต๊ณผ๊ฐ€ ๋œ๋‹ค.

ex04

#include <unistd.h>

void	ft_is_negative(int n)
{
	if (n >= 0)
	{
		write(1, &"P", 1);
	}
	else
	{
		write(1, &"N", 1);
	}
}

ex05

#include <unistd.h>

void	ft_print_comb(void)
{
	char	num[3];

	num[0] = '0';
	while (num[0] <= '7')
	{
		num[1] = num[0] + 1;
		while (num[1] <= '8')
		{
			num[2] = num[1] + 1;
			while (num[2] <= '9')
			{
				write(1, &num, 3);
				if (num[0] != '7')
				{
					write(1, &", ", 2);
				}
				num[2]++;
			}
			num[1]++;
		}
		num[0]++;
	}
}

๋งŽ์€ ๋ถ„๋“ค์ด ํ’€์ด๋ฅผ ์œ„ํ•ด ๊ณ ๋ฏผํ•˜๊ณ  ์‹œ๊ฐ„์ด ๊ฝค ๊ฑธ๋ฆฐ ๋ฌธ์ œ์ด๋‹ค

์ด๋Ÿฐ ์œ ํ˜•์˜ ๋ฌธ์ œ๋Š” ๊ทœ์น™์„ ์ฐพ์œผ๋ฉด ๋น ๋ฅด๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค

  1. 10์˜ ์ž๋ฆฌ ์ˆ˜๋Š” 100๋ณด๋‹ค ์ž‘์„ ์ˆ˜ ์—†์œผ๋ฉฐ ์ตœ์†Ÿ๊ฐ’์€ 100์˜ ์ž๋ฆฌ ์ˆ˜๋ณด๋‹ค 1์ด์ƒ ์ปค์•ผํ•จ
  2. ๋ฐฐ์—ด์˜ ๋์€ 789๋กœ ๊ณ ์ •. ์ฆ‰, 100์ž๋ฆฌ ์ตœ๋Œ€๊ฐ’์€ 7, 10์€ 8, 1์€ 9

์ตœ๋Œ“๊ฐ’์„ ์•Œ์•˜์œผ๋ฏ€๋กœ ํ•ด๋‹น ์กฐ๊ฑด์œผ๋กœ while ๋ฐ˜๋ณต๋ฌธ ์ž‘์„ฑ

์ƒ์œ„ (10์˜ ์ž๋ฆฌ๋ผ๋ฉด 100์˜ ์ž๋ฆฌ) ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ 1 ์˜ฌ๋ž๋‹ค๋ฉด 1๋ฒˆ ๊ทœ์น™์— ๋”ฐ๋ผ ์‹œ์ž‘๊ฐ’ ์„ ์–ธ

์–ด๋–ค ๋ถ„์€ num[0] = '0'; ์„ ์–ธ๊ณผ ๋™์‹œ์— 8, 9๋„ ์ž‘์„ฑํ–ˆ๋‹ค. ๋‹ค๋งŒ while ๋ฌธ์„ ํ†ตํ•ด ์ด๋ฏธ ์„ธํŒ… ๊ฐ’์ด๋ฏ€๋กœ ๋ถˆํ•„์š”ํ•œ ์ฝ”๋“œ์ง€๋งŒ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ๊ทธ๋ƒฅ ๋‘๊ธฐ๋„ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์—ˆ๋‹ค

ex06

#include <unistd.h>

void	ft_print_comb2(void)
{
	char	line[5];
	int		n1;
	int		n2;

	n1 = 0;
	while (n1 <= 98)
	{
		n2 = n1 + 1;
		while (n2 <= 99)
		{
			line[0] = '0' + n1 / 10;
			line[1] = '0' + n1 % 10;
			line[2] = ' ';
			line[3] = '0' + n2 / 10;
			line[4] = '0' + n2 % 10;
			write(1, &line, 5);
			if (n1 != 98)
			{
				write(1, &", ", 2);
			}
			n2++;
		}
		n1++;
	}
}

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ทœ์น™๋ถ€ํ„ฐ ์ฐพ๊ณ  ์‹œ์ž‘

  1. ๋‘๋ฒˆ์งธ ์ˆซ์ž๋Š” ์ฒซ๋ฒˆ์งธ ์ˆซ์ž๋ณด๋‹ค ์ž‘์„ ์ˆ˜ ์—†์Œ. ์ตœ์†Œ 1์ด์ƒ ์ปค์•ผํ•จ
  2. ๋‘๋ฒˆ์งธ ๊ฐ’์ด ๋ฌด์กฐ๊ฑด 99๋ฅผ ์ฐ์–ด์•ผ ์ฒซ๋ฒˆ์งธ ๊ฐ’์ด ์นด์šดํŒ…๋จ
  3. ์ตœ๋Œ“๊ฐ’ 98, 99

3๋ฒˆ ๊ทœ์น™์œผ๋กœ while๋ฌธ ์ž‘์„ฑ

1 2๋ฒˆ ๊ทœ์น™์œผ๋กœ ๊ฐ’ ์„ธํŒ…

๋‹ค๋งŒ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์žˆ์–ด 5์ž๋ฆฌ์˜ ๋ฐฐ์—ด์— ๊ฐ’ ์ €์žฅ

๊ตณ์ด ๋‚˜์ฒ˜๋Ÿผ ๊ตฌํ˜„ํ•  ํ•„์š”๋Š” ์—†๋‹ค. ๋‚˜๋Š” ๊ทธ์ € ๋ฌธ์ž์—ด๋กœ๋งŒ ์ถœ๋ ฅํ•ด์•ผํ•˜๋Š” write์˜ ํŠน์ง•์„ ์‚ด๋ฆฌ๊ธฐ ์œ„ํ•ด ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ง„ line ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ–ˆ์„ ๋ฟ์ด๋‹ค. ๋งŽ์€ ๋ถ„๋“ค์€ ๊ฐ ์ž๋ฆฟ์ˆ˜๋กœ ์ ‘๊ทผํ•˜์˜€๋‹ค.

ex07

#include <unistd.h>

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

void	ft_putnbr(int nb)
{
	if (nb == -2147483648)
	{
		write(1, "-2147483648", 11);
		return ;
	}
	if (nb < 0)
	{
		ft_putchar('-');
		nb = -nb;
	}
	if (nb >= 10)
	{
		ft_putnbr(nb / 10);
	}
	ft_putchar('0' + nb % 10);
}

int ๊ฐ’์œผ๋กœ ์ž…๋ ฅ ๋ฐ›์€ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•ด์ฃผ๋Š” ๋ฌธ์ œ

์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ write ๋กœ ๊ณ ์ •๋˜์–ด ์žˆ๋Š” ์ ์ด ํฌ์ธํŠธ. write๋Š” char ํ˜•ํƒœ๋กœ๋งŒ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ count ์ธ์ž ๋˜ํ•œ ์ž…๋ ฅํ•ด์ค˜์•ผ ํ•จ

๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅํ•˜๋Š” ๊ฑธ๋กœ ํ•ด๊ฒฐ

ex) 1234 โ‡’ 1234 / 10 = 123

123 โ‡’ 123 / 10 = 12

12 โ‡’ 12 / 10 = 1

print 1

์žฌ๊ท€ํ•จ์ˆ˜์˜ ๊ธฐ์ €๋ถ€๋ถ„๊นŒ์ง€ ๋„๋‹ฌํ–ˆ์œผ๋ฏ€๋กœ IF๋ฌธ์€ ๋งŒ๋‚˜์ง€ ์•Š์Œ

๋”ฐ๋ผ์„œ

12 โ‡’ 12%10 = 2

123 โ‡’ 123%10 = 3

1234 โ‡’ 1234%10 = 4

์ถœ๋ ฅ

ex08

#include <unistd.h>

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

void	print_combn(int *arr, int n, int is_last)
{
	int	i;

	i = 0;
	while (i < n)
	{
		print_char('0' + arr[i]);
		i++;
	}
	if (!is_last)
	{
		write(1, ", ", 2);
	}
}

void	comb_recursive(int *arr, int n, int index, int current)
{
	int	is_last;
	int	i;

	if (index == n)
	{
		is_last = (arr[0] == 10 - n);
		print_combn(arr, n, is_last);
		return ;
	}
	i = current;
	while (i <= 10 - n + index)
	{
		arr[index] = i;
		comb_recursive(arr, n, index + 1, i + 1);
		i++;
	}
}

void	ft_print_combn(int n)
{
	int	arr[10];

	if (n <= 0 || n >= 10)
	{
		return ;
	}
	comb_recursive(arr, n, 0, 0);
	write(1, "\n", 1);
}

์ด ๋˜ํ•œ ๊ทœ์น™๋ถ€ํ„ฐ ์ฐพ์ž

  1. n ๋ฒˆ์งธ ์ž๋ฆฌ ์ˆ˜๋Š” n + 1 ๋ฒˆ์งธ ์ž๋ฆฌ ์ˆ˜๋ณด๋‹ค ์ž‘์„ ์ˆ˜ ์—†์œผ๋ฉฐ ์ตœ์†Œ + 1์ž„
  2. 9๋ฅผ ๋งŒ๋‚˜๋ฉด ์ƒ์œ„ ์ž๋ฆฌ ์ˆ˜ + 1
  3. ์ตœ๋Œ“๊ฐ’์€ 9 - n + 1

์žฌ๊ท€์ ์œผ๋กœ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ

ํ˜„์žฌ index์— ํ•ด๋‹นํ•˜๋Š” ์ž๋ฆฌ๋ฅผ ์ฑ„์šฐ๊ณ  index+1 ๋กœ ๋„˜์–ด๊ฐ€๋„๋ก

์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœ, ์ด ๊ณผ์ •์ด n ์ž๋ฆฟ์ˆ˜์˜ ์กฐํ•ฉ์ด ์™„์„ฑ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

์ฒซ ํ˜ธ์ถœ comb_recursive(arr, 3, 0, 0)

index๊ฐ€ 0์ด๊ณ , current๊ฐ€ 0์œผ๋กœ ์‹œ์ž‘

๋ฐ˜๋ณต๋ฌธ์—์„œ i๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ arr[0] = 0์„ ์ €์žฅํ•œ ํ›„, comb_recursive(arr, 3, 1, 1)์„ ํ˜ธ์ถœ

๋‘ ๋ฒˆ์งธ ํ˜ธ์ถœ comb_recursive(arr, 3, 1, 1)

index๊ฐ€ 1์ด๊ณ , current๊ฐ€ 1๋กœ ์„ค์ •

๋ฐ˜๋ณต๋ฌธ์—์„œ i๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉฐ arr[1] = 1์„ ์ €์žฅํ•˜๊ณ , comb_recursive(arr, 3, 2, 2)๋ฅผ ํ˜ธ์ถœ

์„ธ ๋ฒˆ์งธ ํ˜ธ์ถœ comb_recursive(arr, 3, 2, 2)

index๋Š” 2์ด๊ณ , current๋Š” 2

๋ฐ˜๋ณต๋ฌธ์—์„œ i๋Š” 2๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ arr[2] = 2๋ฅผ ์ €์žฅํ•˜๊ณ , comb_recursive(arr, 3, 3, 3)์„ ํ˜ธ์ถœ

๋„ค ๋ฒˆ์งธ ํ˜ธ์ถœ comb_recursive(arr, 3, 3, 3)

index๊ฐ€ n(3)๊ณผ ๊ฐ™์•„์ง = 3์ž๋ฆฌ ์ˆซ์ž์˜ ์กฐํ•ฉ์„ ์™„์„ฑํ–ˆ๋‹ค๋Š” ์˜๋ฏธ

โ†’print_combn ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ํ˜„์žฌ์˜ ์กฐํ•ฉ์ธ 012๋ฅผ ์ถœ๋ ฅ

์—ฌ๊ธฐ์„œ ์žฌ๊ท€ ์ข…๋ฃŒ. ํ•จ์ˆ˜์ข…๋ฃŒ๋˜์–ด ์„ธ ๋ฒˆ์งธ ํ˜ธ์ถœ๋กœ ๊ท€ํ™˜

๋‹ค์Œ i ๊ฐ’์„ 3์œผ๋กœ ์„ค์ •ํ•˜๊ณ , arr[2] = 3์œผ๋กœ ๋ณ€๊ฒฝ

comb_recursive(arr, 3, 3, 4) ํ˜ธ์ถœ

013 ํ˜ธ์ถœ

์ด ๊ณผ์ •์€ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  index์™€ current ์กฐํ•ฉ์ด ์™„์„ฑ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

789์— ๋„๋‹ฌํ•˜๋ฉด is_last ํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ", "๋ฅผ ์ถœ๋ ฅ

โš ๏ธ **GitHub.com Fallback** โš ๏ธ