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> +#+ +:+ +#+ # # By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/11/17 12:35:27 by adjoly #+# #+# # # 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_putnbr.c \
ft_putnbrbase.c \ ft_putnbrbase.c \
ft_putstr.c \ ft_putstr.c \
ft_strlen.c \
OBJS = $(SRCS:.c=.o) OBJS = $(SRCS:.c=.o)
FLAGS = -Werror -Wall -Wextra FLAGS = -Werror -Wall -Wextra
HEADER = libftprintf.h \ HEADER = libftprintf.h
$(NAME): $(OBJS) $(NAME): $(OBJS)
ar -rcs $(NAME) $(OBJS) ar -rcs $(NAME) $(OBJS)
%.o: %.cc %.o: %.c
$(CC) $(FLAGS) -I $(HEADER) $< -c -o $@ $(CC) $(FLAGS) -I $(HEADER) $< -c -o $@
all: $(NAME) all: $(NAME)
@ -37,7 +39,7 @@ clean:
rm -f $(OBJS) rm -f $(OBJS)
fclean: clean fclean: clean
rn -f $(NAME) rm -f $(NAME)
re: fclean all re: fclean all

View File

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

View File

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

BIN
ft_printf.o Normal file

Binary file not shown.

View File

@ -6,11 +6,11 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 10:49:00 by adjoly #+# #+# */ /* 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) 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> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 11:52:46 by adjoly #+# #+# */ /* 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; unsigned int nbr;
int i; int len;
len = 0;
if (n < 0) if (n < 0)
{ {
write(fd, "-", 1); write(1, "-", 1);
nbr = -n; nbr = -n;
} }
else else
nbr = n; nbr = n;
if (nbr < 10) if (nbr < 10)
{ len += write(1, &(char){nbr + '0'}, 1);
write(fd, &(char){nbr + '0'}, 1);
i++;
}
else else
{ {
ft_putnbr_fd(nbr / 10, fd); len += ft_putnbr(nbr / 10);
write(fd, &(char){nbr % 10 + '0'}, 1); 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> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 10:57:44 by adjoly #+# #+# */ /* 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; int base_len;
size_t base_len; int len;
base_len = ft_strlen(base); len = 0;
if (n < 0) base_len = (int)ft_strlen(base);
{ if (n < base_len)
write(fd, "-", 1); len += write(1, &base[n % base_len], 1);
nbr = -n;
}
else
nbr = n;
if (nbr < base_len)
write(fd, &base[nbr % base_len], 1);
else else
{ {
ft_putnbrbase_fd(nbr / base_len, base, fd); len += ft_putnbrbase(n / base_len, base);
write(fd, &base[nbr % base_len], 1); 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> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 11:45:55 by adjoly #+# #+# */ /* 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) 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.