Last active
December 4, 2018 14:35
-
-
Save Yepoleb/285b313b19a52e9a36c44f8cffd3af64 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
// Drawing the numbers left from the center | |
#define FLAG_LEFT (1 << 30) | |
// Drawing right from the center | |
#define FLAG_RIGHT (1 << 29) | |
// Used to remove flags from a number | |
#define NOFLAG_MASK ((1 << 29) - 1) | |
void draw_flags(int num) | |
{ | |
int num_noflag = num & NOFLAG_MASK; | |
if (num & FLAG_LEFT) { | |
if (num_noflag == 1) { | |
printf("1 "); | |
} else { | |
draw_flags(num - 1); | |
printf("%d ", num_noflag); | |
} | |
} else if (num & FLAG_RIGHT) { | |
if (num_noflag == 1) { | |
printf("1"); | |
} else { | |
printf("%d ", num_noflag); | |
draw_flags(num - 1); | |
} | |
} else { | |
draw_flags(num | FLAG_LEFT); | |
draw_flags(num | FLAG_RIGHT); | |
} | |
} | |
#include <stdio.h> | |
#include <stdbool.h> | |
#include <string.h> | |
// offset from stack_top to stack_bottom of the previous frame | |
#define FRAME_OFFSET 84 | |
void draw_stack(int num) | |
{ | |
char stack_top[0]; | |
// Buffer where we receive the string from the child function | |
char recv_buffer[1024]; | |
// Buffer for internal string manipulation | |
char buffer[1024]; | |
// Pointer to the parent buffer | |
char *parent_buffer = NULL; | |
// true if the function is at the top of the recursion chain | |
bool is_toplevel = false; | |
// Length of the received string | |
int recv_len; | |
// Center of the received string | |
int recv_center; | |
// Magic string to check if the function is at the top level | |
char magic[4] = {'h', 'e', 'l', 'p'}; | |
char stack_bottom[0]; | |
if (num == 1) { | |
snprintf(buffer, 1024, "1 1"); | |
} else { | |
// Writes the pattern of all the smaller numbers into | |
// recv_buffer | |
draw_stack(num - 1); | |
recv_len = strlen(recv_buffer); | |
// The length is always uneven, so add 1 to land on the right | |
// side | |
recv_center = recv_len / 2 + 1; | |
// Copy all characters from the start to the center | |
memcpy(buffer, recv_buffer, recv_center); | |
// Add the two new numbers | |
snprintf(buffer + recv_center, 1024 - recv_center, "%d %d ", | |
num, num); | |
// Copy the remaining characters and the zero termination | |
memcpy(buffer + strlen(buffer), recv_buffer + recv_center, | |
recv_len - recv_center + 1); | |
} | |
// Check if the magic string is in the parent stackframe to | |
// determine if this is the top level call | |
for (int magic_i = 0; magic_i < 4; magic_i++) { | |
if (stack_top[FRAME_OFFSET + magic_i] != magic[magic_i]) { | |
is_toplevel = true; | |
} | |
} | |
if (is_toplevel) { | |
puts(buffer); | |
} else { | |
// Set the parent buffer pointer and fills it with content | |
parent_buffer = stack_top + FRAME_OFFSET + | |
(recv_buffer - stack_bottom); | |
memcpy(parent_buffer, buffer, 1024); | |
} | |
} | |
int main() | |
{ | |
//draw_flags(3); | |
draw_stack(10); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment