Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4.  
  5. void ft_putchar(char c)
  6. {
  7.     write(1, &c, 1);
  8. }
  9.  
  10. void print_spaces(int chars, int max_chars)
  11. {
  12.     int index;
  13.  
  14.     index = 0;
  15.     while( index < ((max_chars - chars + 2) / 2 ) - 1) {
  16.         ft_putchar(' ');
  17.         index++;
  18.     }
  19. }
  20.  
  21. int max_chars_size(int size)
  22. {
  23.     int stage;
  24.     int line;
  25.     int chars_to_add;
  26.     int chars_to_print;
  27.  
  28.     stage = 0;
  29.     line = 0;
  30.     chars_to_add = 6;
  31.     chars_to_print = 1;
  32.     while (stage < size)
  33.     {
  34.         line = 0;
  35.         while (line < stage + 3)
  36.         {
  37.             if (line != 0)
  38.                 chars_to_print += 2;
  39.             line++;
  40.         }
  41.         stage++;
  42.         chars_to_print += chars_to_add;
  43.         if (stage % 2 == 0 && stage != 0)
  44.             chars_to_add += 2;
  45.     }
  46.     chars_to_print = chars_to_print - chars_to_add;
  47.     if (stage % 2 == 0 && stage != 0)
  48.         return chars_to_print + 2;
  49.     else
  50.         return chars_to_print;
  51. }
  52.  
  53. int is_door(int stage, int size, int line)
  54. {
  55.     int door_size = (size % 2 == 0) ? size - 1 : size;
  56.  
  57.     if (stage == size - 1)
  58.     {
  59.         if (line >= line - door_size)
  60.             return door_size;
  61.     }
  62.     return 0;
  63. }
  64. // index == pipe_start + door - 2 && door > 3
  65.  
  66. int is_knob(int index, int door_size, int chars, int max_chars)
  67. {
  68.     int pipe_start;
  69.  
  70.     pipe_start = (chars - door_size) / 2;
  71.  
  72.     // vertical check
  73.     if (chars == max_chars - door_size + 1)
  74.     {
  75.         // horizontal check
  76.         if (index == pipe_start + door_size - 2 && door_size > 3)
  77.             return 1;
  78.     }
  79.     return 0;
  80. }
  81.  
  82. void print_line(int size, int chars_to_print, int door) // door = 0 || door = 1
  83. {
  84.     int index;
  85.     int pipe_start;
  86.     int max_chars;
  87.  
  88.    
  89.     index = 0;
  90.     max_chars = max_chars_size(size);
  91.     pipe_start = (chars_to_print - door) / 2;
  92.     print_spaces(chars_to_print, max_chars);
  93.     ft_putchar('/');
  94.     while (index < chars_to_print)
  95.     {
  96.         if (chars_to_print > max_chars - (door * 2))
  97.         {
  98.             if (index >= pipe_start && index < pipe_start + door)
  99.                 if (is_knob(index, door, chars_to_print, max_chars))
  100.                     ft_putchar('$');
  101.                 else
  102.                     ft_putchar('|');
  103.             else
  104.                 ft_putchar('*');
  105.         }
  106.         else
  107.         {
  108.             ft_putchar('*');
  109.         }
  110.         index++;
  111.     }
  112.     ft_putchar('\\');
  113.     ft_putchar('\n');
  114. }
  115.  
  116. void sastantua(int size)
  117. {
  118.     int stage;
  119.     int line;
  120.     int chars_to_add;
  121.     int chars_to_print;
  122.  
  123.     stage = 0;
  124.     line = 0;
  125.     chars_to_add = 6;
  126.     chars_to_print = 1;
  127.     while (stage < size)
  128.     {
  129.         line = 0;
  130.         while (line < stage + 3)
  131.         {
  132.             if (line != 0)
  133.                 chars_to_print += 2;
  134.             print_line(size, chars_to_print, is_door(stage, size, line));
  135.             line++;
  136.         }
  137.         stage++;
  138.         chars_to_print += chars_to_add;
  139.         if (stage % 2 == 0 && stage != 0)
  140.             chars_to_add += 2;
  141.     }
  142. }
  143.  
  144. int main(int argc, char *argv[])
  145. {
  146.     int size;
  147.  
  148.     size = atoi(argv[1]);
  149.     if (argc) sastantua(size);
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement