프로젝트/42Seoul

42서울 라피신 3주차 (C07~C08)

soohykim 2025. 4. 9. 13:16
728x90
반응형

📒 C07

when

  • 23.01.23 월 10:00 ~ 22:30
  • 23.01.24 화 10:00 ~ 22:30
  • 23.01.25 수 09:30 ~ 12:00

1. ex00) ft_strdup

  • str을 malloc 사용해서 복사
#include <stdlib.h>

int	ft_strlen(char *src)
{
	int	i;

	i = 0;
	while (*(src + i))
		i++;
	return (i);
}

char	*ft_strdup(char *src)
{
	char	*dest;
	int		n;
	int		i;

	i = 0;
	n = ft_strlen(src);
	dest = (char *)malloc(sizeof(char) * (n + 1));
	if (!dest)
		return (0);
	while (*(src + i))
	{
		*(dest + i) = *(src + i);
		i++;
	}
	*(dest + i) = '\0';
	return (dest);
}

2. ex01) ft_range

  • 최소값부터 최대값까지 출력
#include <stdlib.h>

int	*ft_range(int min, int max)
{
	int	*range;
	int	i;

	if (min >= max)
		return ((int *)malloc(0));
	range = (int *)malloc(sizeof(int) * (max - min));
	if (!range)
		return (NULL);
	i = 0;
	while (i + min < max)
	{
			*(range + i) = i + min;
			i++;
	}
	return (range);
}

3. ex02) ft_ultimate_range

  • malloc 이용하여 동적할당 한 후 최소값 ~ 최대값 출력
#include <stdlib.h>

int	ft_ultimate_range(int **range, int min, int max)
{
	int	*range2;
	int	i;

	if (min >= max)
	{
		*range = 0;
		return (0);
	}
	range2 = (int *)malloc(sizeof(*range) * (max - min));
	if (!range2)
		return (-1);
	i = 0;
	while (i + min < max)
	{
		*(range2 + i) = min + i;
		i++;
	}
	*range = range2;
	return (max - min);
}

4. ex03) ft_strjoin

  • 구분문자 포함해서 두 문자열 연결
#include <stdlib.h>

int	ft_strlen(char *str)
{
	int	i;

	i = 0;
	while (*(str + i))
		i++;
	return (i);
}

char	*ft_strncat(char *dest, char *src, unsigned int nb)
{
	unsigned int	d;
	unsigned int	s;

	d = 0;
	s = 0;
	while (*(dest + d))
		d++;
	while (*(src + s) && s < nb)
	{
		*(dest + d) = *(src + s);
		d++;
		s++;
	}
	dest[d] = '\0';
	return (dest);
}

char	*ft_strjoin(int size, char **strs, char *sep)
{
	char	*join;
	int		len1;
	int		len2;
	int		i;

	len1 = ft_strlen(*strs);
	len2 = ft_strlen(sep);
	join = (char *)malloc(sizeof(char) * (len1 + len2 + 1));
	if (!join)
		return (NULL);
	if (size == 0)
	{
		join = "";
		return (join);
	}
	i = 0;
	while (i < size)
	{
		len1 = ft_strlen(strs[i]);
		join = ft_strncat(join, strs[i], len1);
		if (i != size - 1)
			join = ft_strncat(join, sep, len2);
		i++;
	}
	return (join);
}

5. ex04) ft_convert_base

  • ft_convert_base : 문자열 -> 정수
int	ft_strlen(char *str)
{
	int	i;

	i = 0;
	while (*(str + i))
		i++;
	return (i);
}

int	ft_error_base(char *base)
{
	int		i;
	int		j;
	char	c;

	i = 0;
	if (base[0] == '\0' || base[1] == '\0')
		return (0);
	while (*(base + i))
	{
		c = *(base + i);
		if (c == '+' || c == '-' || c == ' ' || c < 32 || c > 126)
			return (0);
		j = i + 1;
		while (*(base + j))
		{
			if (base[i] == base[j])
				return (0);
			j++;
		}
		i++;
	}
	return (1);
}

int	ft_atoi_str(char *str, int *s)
{
	int	sign;

	sign = 1;
	while (str[*s] == ' ' || str[*s] == '\r' || str[*s] == '\f'
		|| str[*s] == '\t' || str[*s] == '\v' || str[*s] == '\n')
			*s += 1;
	while (str[*s] == '-' || str[*s] == '+')
	{
		if (str[*s] == '-')
			sign *= -1;
		*s += 1;
	}
	return (sign);
}

char	ft_index_str(char c, char *base)
{
	int	i;

	i = 0;
	while (base[i])
	{
		if (c == base[i])
			return (i);
		i++;
	}
	return (-1);
}

int	ft_atoi_base(char *str, char *base)
{
	long long	value;
	int			sign;
	int			n_base;
	int			start;
	char		tmp;

	value = 0;
	start = 0;
	n_base = ft_strlen(base);
	sign = ft_atoi_str(str, &start);
	tmp = ft_index_str(str[start], base);
	while (tmp != -1)
	{
		value = (value * n_base) + tmp;
		start++;
		tmp = ft_index_str(str[start], base);
	}
	return ((int)(value * sign));
}
  • ft_convert_base2 : 정수 -> 문자열
#include <stdlib.h>
#include <unistd.h>

int	ft_strlen(char *str);
int	ft_error_base(char *base);
int	ft_atoi_base(char *str, char *base);

int	ft_base_size(long long nbr, int n)
{
	int	i;

	i = 0;
	if (nbr == 0)
		return (1);
	if (nbr < 0)
	{
		nbr *= -1;
		i++;
	}
	while (nbr > 0)
	{
		nbr /= n;
		i++;
	}
	return (i);
}

char	*ft_putnbr_base(long long nbr, char *base)
{
	int			n;
	int			i;
	int			size;
	char		*result;

	n = 0;
	n = ft_strlen(base);
	size = ft_base_size(nbr, n);
	result = (char *)malloc(sizeof(char) * (size + 1));
	i = size - 1;
	if (nbr < 0)
	{
		result[0] = '-';
		nbr *= -1;
	}
	else if (nbr == 0)
		result[0] = base[0];
	result[size] = '\0';
	while (nbr > 0)
	{
		result[i] = base[nbr % n];
		nbr /= n;
		i--;
	}
	return (result);
}

char	*ft_convert_base(char *nbr, char *base_from, char *base_to)
{
	char		*result;
	int			n;
	int			size;
	long long	temp;

	if (ft_error_base(base_from) == 0 || ft_error_base(base_to) == 0)
		return (NULL);
	temp = (long long)ft_atoi_base(nbr, base_from);
	result = ft_putnbr_base(temp, base_to);
	return (result);
}

6. ex05) ft_split

  • 구분자 기준 문자열 분리
#include <stdlib.h>

int	is_seperator(char c, char *charset)
{
	int		i;

	i = 0;
	while (charset[i])
	{
		if (c == charset[i])
			return (1);
		i++;
	}
	return (0);
}

int	ft_count_str(char *str, char *charset)
{
	int	i;
	int	count;

	i = 0;
	count = 0;
	while (str[i])
	{
		while (str[i] != 0 && is_seperator(str[i], charset))
			i++;
		if (str[i] != 0 && !is_seperator(str[i], charset))
			count++;
		while (str[i] != 0 && !is_seperator(str[i], charset))
			i++;
	}
	return (count);
}

char	*ft_split_str(char *str, char *charset)
{
	int		n;
	int		i;
	char	*split_str;

	n = 0;
	i = 0;
	while (str[n])
		n++;
	split_str = (char *)malloc(sizeof(char) * (n + 1));
	if (!split_str)
		return (0);
	while (str[i] != 0 && !is_seperator(str[i], charset))
	{
		split_str[i] = str[i];
		i++;
	}
	split_str[i] = 0;
	return (split_str);
}

char	**ft_split(char *str, char *charset)
{
	int		i;
	int		index;
	int		count;
	char	**s_str;

	i = 0;
	index = 0;
	count = ft_count_str(str, charset);
	s_str = (char **)malloc(sizeof(char *) * (count + 1));
	if (!s_str)
		return (0);
	while (str[i])
	{
		while (str[i] != 0 && is_seperator(str[i], charset))
			i++;
		if (str[i] != 0 && !is_seperator(str[i], charset))
		{
			s_str[index] = ft_split_str(&str[i], charset);
			index++;
		}
		while (str[i] != 0 && !is_seperator(str[i], charset))
			i++;
	}
	s_str[index] = 0;
	return (s_str);
}

C07 main.c

1. ex00) ft_strdup

#include <stdio.h>

char	*ft_strdup(char *src);

int	main(void)
{
	printf("%s\n", ft_strdup("exercise00 is finished"));
}

2. ex01) ft_range

#include <stdio.h>

int	*ft_range(int min, int max);

int main(void)
{
	int i;
	int	min;
	int	max;
	int *arr;

	i = 0;
	min = -6;
	max = 4;
	arr = ft_range(min, max);
	while (min < max)
	{
		printf("최소 : -6, 최대 : 4 = %d\n", arr[i++]);
		min++;
	}
	printf("\n\n");
	i = 0;
	min = 4;
	max = 0;
	arr = ft_range(min, max);
	printf("최소 : 4, 최대 : 0 = %d\n", arr[i]);

}

3. ex02) ft_ultimate_range

#include <stdio.h>

int	ft_ultimate_range(int **range, int min, int max);

int main(void)
{
	int	*arr2;
	int	min2;
	int max2;
	int	i;
	int	result;

	min2 = -6;
	max2 = 4;
	i = 0;
	result = ft_ultimate_range(&arr2, min2, max2);
	while (min2 < max2)
	{
		printf("최소 : -6, 최대 : 4 = %d\n", arr2[i]);
		i++;
		min2++;
	}
	printf("result = %d \n\n", result);
	min2 = 4;
	max2 = 0;
	i = 0;
	result = ft_ultimate_range(&arr2, min2, max2);
	printf("result = %d\n", result);
}

4. ex03) ft_strjoin

#include <stdio.h>

char	*ft_strjoin(int size, char **strs, char *sep);

int	main(void)
{
	char	*strs[] = {"My", "Name", "is", "soohyun", "!"};
	char	*sep = "+-+";
	char	*joined;
	joined = ft_strjoin(5, strs, sep);
	printf("ft_strjoin(5, strs, sep)\n");
	printf("%s\n\n", joined);
	joined = ft_strjoin(0, strs, sep);
	printf("ft_strjoin(0, strs, sep)\n");
	printf("%s\n", joined);
}

5. ex04) ft_convert_base

#include <stdio.h>

int ft_strlen(char *str);
int ft_error_base(char *base);
int ft_atoi_str(char *str, int *s);
char    ft_index_str(char c, char *base);
int ft_atoi_base(char *str, char *base);

int ft_base_size(long long nbr, int n);
char    *ft_putnbr_base(long long nbr, char *base);
char *ft_convert_base(char *nbr, char *base_from, char *base_to);

int	main(void)
{
	printf("%s\n", ft_convert_base("--2147483648", "0123456789", "0123456789abcdef"));
	printf("%s\n", ft_convert_base("  +2147483647", "0123456789", "0123456789abcdef"));
	printf("%s\n", ft_convert_base("-0", "0123456789", "01"));
}

6. ex05) ft_split

#include <stdio.h>

char **ft_split(char *str, char *charset);

int	main(void)
{
	char *s1;
	char *s2;
	char **words;

	s1 = "asdf2439#85723RETV#WYWER5w%^YW#$%6";
	s2 = "";
	words = ft_split(s1, s2);
	if (!*words) printf("(blank)\n");
	while (*words)
		printf("#1 - %s\n", *(words++));
	printf("\n");

	s1 = "asdf2439#85723RETV#WYWER5w%^YW#$%6";
	s2 = " ";
	words = ft_split(s1, s2);

	if (!*words) printf("(blank)\n");
	while (*words)
		printf("#2 - %s\n", *(words++));
	printf("\n");

	s1 = "";
	s2 = "";
	words = ft_split(s1, s2);

	if (!*words) printf("(blank)\n");
	while (*words)
		printf("#3 - %s\n", *(words++));
	printf("\n");

	s1 = "    ! asf as!adg asdf asf    asdf     ";
	s2 = " !";
	words = ft_split(s1, s2);

	if (!*words) printf("(bank)\n");
	while (*words)
		printf("#4 - %s\n", *(words++));
	printf("\n");

	s1 = " hi!my name!   is soon!hyung.";
	s2 = " ";
	words = ft_split(s1, s2);

	if (!*words) printf("(blank)\n");
	while (*words)
		printf("#5 - %s\n", *(words++));
	printf("\n");
}

📒 C08

when

  • 23.01.26 목 03:15 ~ 21:00
  • 23.01.28 토 10:00 ~ 22:00
  • 23.01.29 일 09:00 ~

📁 헤더파일.h

공통으로 사용하는 부분은 헤더 파일에 넣고, 함수들은 기능별로 파일을 분리 (공통 사용 함수, 구조체, 전역변수, 매크로, 다른 헤더파일 통합 관리)

#ifndef HEADER_H       // 정의되어 있지 않으면 
# define HEADER_H      // 매크로를 정의함

[include할 다른 헤더 파일 명시] 
 - #include <stdio.h>   // 표준 라이브러리 포함하는 전처리
 - #include "header.h"  // 사용자 정의 헤더를 포함하는 전처리
[매크로 정의]
 - # define PI 3.14
 - # define SQRT(X) X*X
[사용자 struct, type 정의]
 - typedef	struct_Person {
 		char	name[10];
        int		age;
        char	address[128];
    }			Person;
    
  - typedef long int file_size_t
[전역 변수 선언]
  - extern long long arr_len;
[함수 선언]
  - int	f_isalpha(int c);
  - int f_isdigit(int c);

#endif                  // 매크로 정의 끝
  • 파일명 : 소문자 -> 대문자
  • 마침표 -> 언더바
  • 파일 이름 앞뒤로 2개 언더바
  • 전처리 문법 사용시 들여쓰기로 구분

📁 컴파일

  • 컴파일 : 소스코드로부터 모듈 생성 (중간 코드/기계어로 변환)
    • .o : object file
    • .a : archieve file
    • .so : shared file
    • .sl : shared lib
  • 링크 : 중간코드를 결합하여 실행파일 생성
  • 분할 컴파일 : 모든 소스코드를 파일로 작성하는 것 대신 여러 파일로 나누어 작업

1. ex00) ft.h

#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

2. ex01) ft_boolean.h

  • typedef (부여하는 타입) (이름)
#ifndef FT_BOOLEAN_H
# define FT_BOOLEAN_H
# include <unistd.h>
# define EVEN(nbr) nbr % 2 == 0
# define TRUE 1
# define FALSE 0
# define SUCCESS 0
# define EVEN_MSG "I have an even number of arguments.\n"
# define ODD_MSG "I have an odd number of arguments.\n"

typedef int	t_bool;
#endif

3. ex02) ft_abs.h

  • 절대값
#ifndef FT_ABS_H
# define FT_ABS_H
# define ABS(n) ((n >= 0) * n + (n < 0) * -n)
#endif

4. ex03) ft_point.h

  • 구조체 선언
#ifndef FT_POINT_H
# define FT_POINT_H

typedef struct s_point
{
	int	x;
	int	y;
}	t_point;
#endif

5. ex04) ft_strs_to_tab

  • 인자 값 넘겨받아 구조체에 할당
#include <stdlib.h>
#include "ft_stock_str.h"

int	ft_strlen(char *src)
{
	int	i;

	i = 0;
	while (*(src + i))
		i++;
	return (i);
}

char	*ft_strdup(char *src)
{
	char	*dest;
	int		n;
	int		i;

	i = 0;
	n = ft_strlen(src);
	dest = (char *)malloc(sizeof(char) * (n + 1));
	if (!dest)
		return (0);
	while (*(src + i))
	{
		*(dest + i) = *(src + i);
		i++;
	}
	*(dest + i) = '\0';
	return (dest);
}

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

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

6. ex05) ft_show_tab

  • 구조체 배열 받아서 출력
#include <unistd.h>
#include "ft_stock_str.h"

void	ft_putstr(char	*str)
{
	int	i;

	i = 0;
	while (str[i])
	{
		write(1, &str[i], 1);
		i++;
	}
}

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

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

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

	i = 0;
	while (par[i].str)
	{
		ft_putstr(par[i].str);
		write(1, "\n", 1);
		ft_putnbr(par[i].size);
		write(1, "\n", 1);
		ft_putstr(par[i].copy);
		write(1, "\n", 1);
		i++;
	}
}

C08 main.c

ex04 - ex05

  • 헤더 파일
#include <stdlib.h>
#include "ft_stock_str.h"

int	ft_strlen(char *src)
{
	int	i;

	i = 0;
	while (*(src + i))
		i++;
	return (i);
}

char	*ft_strdup(char *src)
{
	char	*dest;
	int		n;
	int		i;

	i = 0;
	n = ft_strlen(src);
	dest = (char *)malloc(sizeof(char) * (n + 1));
	if (!dest)
		return (0);
	while (*(src + i))
	{
		*(dest + i) = *(src + i);
		i++;
	}
	*(dest + i) = '\0';
	return (dest);
}

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

	ptr = (t_stock_str *)malloc(sizeof(t_stock_str) * (ac + 1));
	if (ptr == NULL)
		return (NULL);
	i = 0;
	while (i < ac)
	{
		ptr[i].size = ft_strlen(av[i]);
		ptr[i].str = av[i];
		ptr[i].copy = ft_strdup(av[i]);
		i++;
	}
	ptr[i].str = 0;
	return (ptr);
}
  • main.c
#include "ft_stock_str.h"
#include "ft_strs_to_tab.c"
#include "ft_show_tab.c"

int	main(void)
{
    char *str[10] = {"abc","cdf","aaa","ahh"};
    t_stock_str *s_str;

	s_str = ft_strs_to_tab(4, str);
    ft_show_tab(s_str);
}

Makefile

C 보충

C 보충

c 보충

728x90
반응형