Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. /*
  2. ** EPITECH PROJECT, 2017
  3. ** my_printf
  4. ** File description:
  5. ** my_printf
  6. */
  7. #include <stdarg.h>
  8.  
  9. int my_putstr(char const *str)
  10. {
  11. int i = 0;
  12. while (str[i] != '\0') {
  13. my_putchar(str[i]);
  14. i = i + 1;
  15. }
  16. }
  17.  
  18. int my_putnbr(int nbr)
  19. {
  20. int modulo = 0;
  21.  
  22. if (nbr <= 9 && nbr >= 0)
  23. my_putchar(nbr + '0');
  24. if (nbr < 0) {
  25. my_putchar('-');
  26. nbr = nbr * (-1);
  27. if (nbr <= 9 && nbr >= 0)
  28. my_putnbr(nbr);
  29. }
  30. if (nbr > 9) {
  31. modulo = nbr % 10;
  32. my_putnbr(nbr / 10);
  33. my_putchar(modulo + '0');
  34. }
  35. }
  36.  
  37. void my_putchar(char c)
  38. {
  39. write(1, &c, 1);
  40. }
  41.  
  42. typedef struct pair_char_fptr
  43. {
  44. char c;
  45. int (*pointeur_fonction)(va_list);
  46. } pair_char_fptr;
  47.  
  48. pair_char_fptr myput[3] = { {'c', &my_putchar},
  49. {'d', &my_putnbr},
  50. {'s', &my_putstr} };
  51.  
  52. int my_printf(char *str, ...)
  53. {
  54. va_list arguments;
  55. va_start(arguments, str);
  56. int i = 0;
  57.  
  58. while(str[i])
  59. {
  60. if (str[i] == '%') {
  61. i++;
  62.  
  63. for(int j=0; j<3; j++)
  64. {
  65. if(str[i] == myput[j].c)
  66. myput[j].pointeur_fonction(va_arg(arguments, int));
  67. }
  68. }
  69. i++;
  70. }
  71. va_end(arguments);
  72. }
  73.  
  74. int main()
  75. {
  76. my_printf("%s", "Ca marche!");
  77. return (0);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement