C언어 in 42Seoul (라피신)

시험 대비 c 00

밍동니 2021. 2. 19. 07:33
#include <unistd.h>

int		main(void)
{
	write(1, &"a", 1);
}
#include <unistd.h>

int		main(void)
{
	char	letter;
	int		index;

	letter = 'z';
	index = 1;
	while (letter >= 'a')
	{
		if (index++ % 2 == 0)
		{
			letter -= 32;
			write(1, &letter, 1);
			letter += 32;
		}
		else
		{
			write(1, &letter, 1);
		}
		letter--;
	}
	write(1, &"\n", 1);
}
int		ft_str_length(char *str)
{
	int index;

	index = 0;
	while (str[index++] != '\0');
	return (index - 1);
}

char	*ft_strrev(char *str)
{
	int size;
	int index;
	char tmp;

	size = ft_str_length(str);
	index = 0;
	while (index != size / 2)
	{
		tmp = str[index];
		str[index] = str[size - 1 - index];
		str[size - 1 - index] = tmp;
		index++;
	}
	return (str);
}

 

#include <stdbool.h>

int		ft_str_length(const char *str)
{
	int index;

	index = 0;
	while (str[index++] != '\0');
	return (index - 1);
}

int		ft_pow(int a, int b)
{
	int	result;

	result = 1;
	while (a-- != 0 && a > 0)
	{
		result *= b;
	}
	return (result);
}

void	ft_remove_excess(int *number, int index, int str_length)
{
	while ((str_length - index) != 0)
	{
		*number /= 10;
		index++;
	}
}

int		ft_atoi(const char* str)
{
	int		result;
	int		str_length;
	int		index;
	bool    negative;

	result = 0;
	str_length = ft_str_length(str);
	negative = str[0] == '-';
	index = negative;
	while(index <= str_length - 1)
	{
		if (!(str[index] >= '0' && str[index] <= '9'))
		{
			ft_remove_excess(&result, index, str_length);
			break ;
		}
		result += (str[index] - '0') * ft_pow(str_length - index, 10);
		index++;
	}
	if (negative)
		result = -result;
	return (result);
}

 

vim ~/.zshrc 

 

만들어서 

 

USER=minjeoki

export USER

MAIL=minjeoki@student.42seoul.kr

export MAIL

 

라고 입력하고 나오기

 

source ~/.zshrc 치고 엔터

 

esc 모드에서 :Stdheader

 

 

 

ex00

 

wonillism.tistory.com/151?category=885291

 

[C in Linux] gcc 그리고 unistd.h

gcc란? gcc(GNU Compiler Collection), 자유 소프트웨어 중에 가장 잘 알려진 GCC는 원래 C만을 지원했던 컴파일러로 GNU C Compiler 였지만, 현재는 C++, Java, 포트란, 에이다 등 여러 언어를 컴파일할 수 있게..

wonillism.tistory.com

#include <unistd.h>

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

ex01

#incldue <unistd.h>

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

ex02

 

#include <unistd.h>

void	ft_print_reverse_alphabet(void)
{
	char alpha;

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

ex03

 

#include <unistd.h>

void	ft_print_numbers(void)
{
	write(1, "0123456789", 10);
}

ex04

#include <unistd.h>

void	ft_negative(int n)
{
	if(n<0)
    	write(1, "N" , 1);
    else
    	write(1, "P", 1);
}

 

ex05

#include <unistd.h>

void	ft_print_comb(void)
{
	char buf[5];

	buf[0] = '0' - 1;
	buf[3] = ',';
	buf[4] = ' ';
	while (++buf[0] <= '6')
	{
		buf[1] = buf[0];
		while (++buf[1] <= '8')
		{
			buf[2] = buf[1];
			while (++buf[2] <= '9')
			{
				write(1, buf, 5);
			}
		}
	}
	write(1, "789", 3);
}

ex06

#include <unistd.h>

void	update(char *buf, int a, int b)
{
	write(1, buf, 7);
	if (buf[b] == '9')
	{
		++buf[a];
		buf[b] = '0';
	}
	else
		++buf[b];
}

void	ft_print_comb2(void)
{
	char buf[7];

	buf[0] = '0';
	buf[1] = '0';
	buf[2] = ' ';
	buf[5] = ',';
	buf[6] = ' ';
	while (buf[0] != '9' || buf[1] != '8')
	{
		buf[3] = buf[0];
		if (buf[1] == '9')
		{
			++buf[3];
			buf[4] = '0';
		}
		else
			buf[4] = buf[1] + 1;
		while (buf[3] != '9' || buf[4] != '9')
			update(buf, 3, 4);
		update(buf, 0, 1);
	}
	write(1, "98 99", 5);
}

ex07

#include <unistd.h>

void	rec(int nb)
{
	char ch;

	if (nb == 0)
		return ;
	rec(nb / 10);
	ch = '0' + nb % 10;
	write(1, &ch, 1);
}

void	ft_putnbr(int nb)
{
	char ch;

	if (nb < 0)
	{
		write(1, "-", 1);
		rec(-(nb / 10));
		ch = '0' - nb % 10;
	}
	else
	{
		rec(nb / 10);
		ch = '0' + nb % 10;
	}
	write(1, &ch, 1);
}

ex08

#include <unistd.h>

int g_len;
char g_buf[10];

void	rec(int prev, int n)
{
	int i;

	if (n == g_len)
	{
		write(1, g_buf, g_len);
		write(1, ", ", 2);
		return ;
	}
	if (prev == 9)
		return ;
	i = prev;
	while (++i <= 10 - g_len + n)
	{
		g_buf[n] = '0' + i;
		rec(i, n + 1);
	}
}

void	print_last(int i)
{
	char c;

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

void	ft_print_combn(int n)
{
	int i;

	i = 0;
	g_len = n;
	while (i < 10 - n)
	{
		g_buf[0] = '0' + i;
		rec(i++, 1);
	}
	print_last(i);
}