Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- void ft_putchar(char c)
- {
- write(1, &c, 1);
- }
- void print_spaces(int chars, int max_chars)
- {
- int index;
- index = 0;
- while( index < ((max_chars - chars + 2) / 2 ) - 1) {
- ft_putchar(' ');
- index++;
- }
- }
- int max_chars_size(int size)
- {
- int stage;
- int line;
- int chars_to_add;
- int chars_to_print;
- stage = 0;
- line = 0;
- chars_to_add = 6;
- chars_to_print = 1;
- while (stage < size)
- {
- line = 0;
- while (line < stage + 3)
- {
- if (line != 0)
- chars_to_print += 2;
- line++;
- }
- stage++;
- chars_to_print += chars_to_add;
- if (stage % 2 == 0 && stage != 0)
- chars_to_add += 2;
- }
- chars_to_print = chars_to_print - chars_to_add;
- if (stage % 2 == 0 && stage != 0)
- return chars_to_print + 2;
- else
- return chars_to_print;
- }
- int is_door(int stage, int size, int line)
- {
- int door_size = (size % 2 == 0) ? size - 1 : size;
- if (stage == size - 1)
- {
- if (line >= line - door_size)
- return door_size;
- }
- return 0;
- }
- // index == pipe_start + door - 2 && door > 3
- int is_knob(int index, int door_size, int chars, int max_chars)
- {
- int pipe_start;
- pipe_start = (chars - door_size) / 2;
- // vertical check
- if (chars == max_chars - door_size + 1)
- {
- // horizontal check
- if (index == pipe_start + door_size - 2 && door_size > 3)
- return 1;
- }
- return 0;
- }
- void print_line(int size, int chars_to_print, int door) // door = 0 || door = 1
- {
- int index;
- int pipe_start;
- int max_chars;
- index = 0;
- max_chars = max_chars_size(size);
- pipe_start = (chars_to_print - door) / 2;
- print_spaces(chars_to_print, max_chars);
- ft_putchar('/');
- while (index < chars_to_print)
- {
- if (chars_to_print > max_chars - (door * 2))
- {
- if (index >= pipe_start && index < pipe_start + door)
- if (is_knob(index, door, chars_to_print, max_chars))
- ft_putchar('$');
- else
- ft_putchar('|');
- else
- ft_putchar('*');
- }
- else
- {
- ft_putchar('*');
- }
- index++;
- }
- ft_putchar('\\');
- ft_putchar('\n');
- }
- void sastantua(int size)
- {
- int stage;
- int line;
- int chars_to_add;
- int chars_to_print;
- stage = 0;
- line = 0;
- chars_to_add = 6;
- chars_to_print = 1;
- while (stage < size)
- {
- line = 0;
- while (line < stage + 3)
- {
- if (line != 0)
- chars_to_print += 2;
- print_line(size, chars_to_print, is_door(stage, size, line));
- line++;
- }
- stage++;
- chars_to_print += chars_to_add;
- if (stage % 2 == 0 && stage != 0)
- chars_to_add += 2;
- }
- }
- int main(int argc, char *argv[])
- {
- int size;
- size = atoi(argv[1]);
- if (argc) sastantua(size);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement