본문 바로가기

Programming/Algorithm (C++)

c08

/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_show_tab.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: minjeoki <minjeoki@student.42seoul.kr>     +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2021/03/11 12:12:22 by minjeoki          #+#    #+#             */
/*   Updated: 2021/03/13 13:54:38 by minjeoki         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

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


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

void	ft_putnbr(int nb)
{
	if (nb > 9)
	{
		ft_putnbr(nb / 10);
		nb = nb % 10;
	}
	if (0 <= nb && nb <= 9)
		ft_write(nb + '0');
	if (nb < 0 && nb != -2147483648)
	{
		nb = -nb;
		write(1, "-", 1);
		ft_putnbr(nb);
	}
	if (nb == -2147483648)
	{
		write(1, "-", 1);
		write(1, "2147483648", 10);
	}
}

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

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

int	main(void)
{
	t_stock_str *b;
	t_stock_str a;

	b = (char **)malloc(sizeof(char*) * 3);
	b[0].data = (char*)malloc(sizeof(char) * 1024);
	b[1].data = (char*)malloc(sizeof(char) * 1024);
	b[2].data = (char*)malloc(sizeof(char) * 1024);

	b = ft_show_tab(a);


	return (0);
}
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_strs_to_tab.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: minjeoki <minjeoki@student.42seoul.kr>     +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2021/03/11 12:05:09 by minjeoki          #+#    #+#             */
/*   Updated: 2021/03/11 21:58:10 by minjeoki         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <stdio.h>
#include <stdlib.h>
#include "ft_stock_str.h"

int			ft_len(char *str)
{
	int		i;

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

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

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

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

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

int	main(void)
{
	char **a = (char**)malloc(sizeof(char*)*3);
	a[0] = (char*)malloc(sizeof(char) * 1024);
	a[1] = (char*)malloc(sizeof(char) * 1024);
	a[2] = (char*)malloc(sizeof(char) * 1024);
	a[0]="asdf";
	a[1]="bwe";
	a[2]="bewrq";
	ft_stock_str(3, a);

}

/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_strs_to_tab.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: minjeoki <minjeoki@student.42seoul.kr>     +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2021/03/11 12:05:09 by minjeoki          #+#    #+#             */
/*   Updated: 2021/03/11 23:42:04 by minjeoki         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ft_stock_str.h"

int			ft_len(char *str)
{
	int		i;

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

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

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

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

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

int	main(void)
{
	t_stock_str *b;

	char **a;
	a = (char **)malloc(sizeof(char*) * 3);
	a[0] = (char*)malloc(sizeof(char) * 1024);
	a[1] = (char*)malloc(sizeof(char) * 1024);
	a[2] = (char*)malloc(sizeof(char) * 1024);
	strcpy(a[0], "abc");
	strcpy(a[1], "edf");
	strcpy(a[2], "seg");
	b = ft_strs_to_tab(3, a);

	printf("b[0].copy %s\n", b[0].copy);
	printf("b[1].copy %s\n", b[1].copy);
	for(int i=0; i<3; i++)
		printf("%s - %s | %d\n", b[i].str, b[i].copy, b[i].size);
	return 0;
}

'Programming > Algorithm (C++)' 카테고리의 다른 글

[백준 3085] c++ :: 사탕 게임  (0) 2021.04.07
rush02 정리  (0) 2021.03.16
c09  (0) 2021.03.11
c04l  (0) 2021.03.09
c06  (0) 2021.03.04