Archived
1
0

counting done - printf don't work to fix

This commit is contained in:
Adam Joly
2023-11-20 16:29:38 +01:00
parent 4188dfeb84
commit 0d74dd1917
15 changed files with 91 additions and 63 deletions

View File

@ -6,7 +6,7 @@
# By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/17 12:35:27 by adjoly #+# #+# #
# Updated: 2023/11/20 10:07:45 by adjoly ### ########.fr #
# Updated: 2023/11/20 15:55:52 by adjoly ### ########.fr #
# #
# **************************************************************************** #
@ -19,16 +19,18 @@ SRCS = ft_printf.c \
ft_putnbr.c \
ft_putnbrbase.c \
ft_putstr.c \
ft_strlen.c \
OBJS = $(SRCS:.c=.o)
FLAGS = -Werror -Wall -Wextra
HEADER = libftprintf.h \
HEADER = libftprintf.h
$(NAME): $(OBJS)
ar -rcs $(NAME) $(OBJS)
%.o: %.cc
%.o: %.c
$(CC) $(FLAGS) -I $(HEADER) $< -c -o $@
all: $(NAME)
@ -37,7 +39,7 @@ clean:
rm -f $(OBJS)
fclean: clean
rn -f $(NAME)
rm -f $(NAME)
re: fclean all

View File

@ -6,26 +6,34 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/17 16:48:37 by adjoly #+# #+# */
/* Updated: 2023/11/20 11:53:52 by adjoly ### ########.fr */
/* Updated: 2023/11/20 16:09:16 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "ft_printf.h"
void ft_putaddr(void *ptr)
int ft_putaddr(void *ptr)
{
ft_putstr((char *)ptr);
int r;
write(1, "0x", 2);
r = ft_putnbrbase((long unsigned int)ptr, "0123456789abcdef");
return (2 + r);
}
void ft_putnbrulong(unsigned long n)
int ft_putnbrulong(unsigned long n)
{
int len;
len = 0;
if (n < 10)
write(1, &(char){n + '0'}, 1);
len += write(1, &(char){n + '0'}, 1);
else
{
ft_putnbrulong(n / 10);
write(1, &(char){n % 10 + '0'}, 1);
len += ft_putnbrulong(n / 10);
len += write(1, &(char){n % 10 + '0'}, 1);
}
return (len);
}
int ft_printconversion(char conversion, va_list args)
@ -34,33 +42,33 @@ int ft_printconversion(char conversion, va_list args)
count = 0;
if (conversion == '%')
ft_putchar('%');
count = ft_putchar('%');
else if (conversion == 's')
ft_putstr(va_arg(args, char *));
count = ft_putstr(va_arg(args, char *));
else if (conversion == 'c')
ft_putchar(va_arg(args, char *)[0]);
count = ft_putchar(va_arg(args, char *)[0]);
else if (conversion == 'i' || conversion == 'd')
ft_putnbr(va_arg(args, int));
count = ft_putnbr(va_arg(args, int));
else if (conversion == 'u')
ft_putnbrulong(va_arg(args, unsigned long));
count = ft_putnbrulong(va_arg(args, unsigned long));
else if (conversion == 'p')
ft_putstr((char *)va_arg(args, void *));
count = ft_putaddr(va_arg(args, void *));
else if (conversion == 'x')
ft_putnbrbase(va_arg(args, int), "0123456789abcdef");
count = ft_putnbrbase(va_arg(args, unsigned int), "0123456789abcdef");
else if (conversion == 'X')
ft_putnbrbase(va_arg(args, int), "0123456789ABCDEF");
count = ft_putnbrbase(va_arg(args, unsigned int), "0123456789ABCDEF");
return (count);
}
int ft_printf(const char *format, ...)
{
int i;
int j;
va_list args;
int count;
va_start(args, format);
count = 0;
i = 0;
while (format[i])
{
if (format[i] == '%')
@ -69,7 +77,7 @@ int ft_printf(const char *format, ...)
count += ft_printconversion(format[i], args);
}
else
ft_putchar(format[i]);
count += ft_putchar(format[i]);
i++;
}
va_end(args);

View File

@ -1,17 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libftprintf.h :+: :+: :+: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/17 16:50:36 by adjoly #+# #+# */
/* Updated: 2023/11/20 10:04:12 by adjoly ### ########.fr */
/* Updated: 2023/11/20 15:52:59 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFTPRINTF_H
# define LIBFTPRINTF_H
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdlib.h>
# include <stdarg.h>
@ -19,12 +19,13 @@
int ft_printf(const char *format, ...);
int ft_printconversion(char conversion, va_list args);
void ft_putnbrulong(unsigned long n);
void ft_putaddr(void *ptr);
int ft_putnbrulong(unsigned long n);
int ft_putaddr(void *ptr);
int ft_putstr(char *s);
void ft_putnbrbase(int n, char *base);
int ft_putnbrbase(int n, char *base);
int ft_putchar(char c);
void ft_putnbr(int n);
int ft_putnbr(int n);
size_t ft_strlen(const char *s);
#endif

BIN
ft_printf.o Normal file

Binary file not shown.

View File

@ -6,11 +6,11 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 10:49:00 by adjoly #+# #+# */
/* Updated: 2023/11/20 10:04:41 by adjoly ### ########.fr */
/* Updated: 2023/11/20 15:51:04 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "ft_printf.h"
int ft_putchar(char c)
{

BIN
ft_putchar.o Normal file

Binary file not shown.

View File

@ -6,32 +6,31 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 11:52:46 by adjoly #+# #+# */
/* Updated: 2023/11/20 10:40:57 by adjoly ### ########.fr */
/* Updated: 2023/11/20 16:00:22 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "ft_printf.h"
void ft_putnbr_fd(int n, int fd)
int ft_putnbr(int n)
{
unsigned int nbr;
int i;
int len;
len = 0;
if (n < 0)
{
write(fd, "-", 1);
write(1, "-", 1);
nbr = -n;
}
else
nbr = n;
if (nbr < 10)
{
write(fd, &(char){nbr + '0'}, 1);
i++;
}
len += write(1, &(char){nbr + '0'}, 1);
else
{
ft_putnbr_fd(nbr / 10, fd);
write(fd, &(char){nbr % 10 + '0'}, 1);
len += ft_putnbr(nbr / 10);
len += write(1, &(char){nbr % 10 + '0'}, 1);
}
return (len);
}

BIN
ft_putnbr.o Normal file

Binary file not shown.

View File

@ -1,35 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbrbase_fd.c :+: :+: :+: */
/* ft_putnbrbase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 10:57:44 by adjoly #+# #+# */
/* Updated: 2023/11/18 11:10:32 by adjoly ### ########.fr */
/* Updated: 2023/11/20 16:16:34 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "ft_printf.h"
void ft_putnbrbase_fd(int n, char *base, int fd)
int ft_putnbrbase(int n, char *base)
{
unsigned int nbr;
size_t base_len;
int base_len;
int len;
base_len = ft_strlen(base);
if (n < 0)
{
write(fd, "-", 1);
nbr = -n;
}
else
nbr = n;
if (nbr < base_len)
write(fd, &base[nbr % base_len], 1);
len = 0;
base_len = (int)ft_strlen(base);
if (n < base_len)
len += write(1, &base[n % base_len], 1);
else
{
ft_putnbrbase_fd(nbr / base_len, base, fd);
write(fd, &base[nbr % base_len], 1);
len += ft_putnbrbase(n / base_len, base);
len += write(1, &base[n % base_len], 1);
}
return (len);
}

BIN
ft_putnbrbase.o Normal file

Binary file not shown.

View File

@ -6,11 +6,11 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 11:45:55 by adjoly #+# #+# */
/* Updated: 2023/11/20 10:07:08 by adjoly ### ########.fr */
/* Updated: 2023/11/20 15:51:38 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "ft_printf.h"
int ft_putstr(char *s)
{

BIN
ft_putstr.o Normal file

Binary file not shown.

23
ft_strlen.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 18:15:57 by adjoly #+# #+# */
/* Updated: 2023/11/20 15:51:51 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
size_t ft_strlen(const char *s)
{
int i;
i = 0;
while (s[i])
i++;
return (i);
}

BIN
ft_strlen.o Normal file

Binary file not shown.

BIN
libftprintf.a Normal file

Binary file not shown.