diff --git a/21st_century_c/Makefile b/21st_century_c/Makefile index 81d5edf..95bbb28 100644 --- a/21st_century_c/Makefile +++ b/21st_century_c/Makefile @@ -1,17 +1,20 @@ CFLAGS = --std=c11 -Wall -O3 +boxes : boxes.c + $(CC) $(CFLAGS) $@.c -o $@ + show_tree : process_dir.h process_dir.c show_tree.c $(CC) $(CFLAGS) process_dir.c $@.c -o $@ simple_cplx : cplx.h complex.c simple_cplx.c $(CC) `pkg-config --cflags --libs gsl` $(CFLAGS) complex.c $@.c -o $@ - .PHONY : all -all : show_tree simple_cplx +all : boxes show_tree simple_cplx .PHONY : clean clean : + rm -f boxes rm -f show_tree rm -f simple_cplx diff --git a/21st_century_c/boxes.c b/21st_century_c/boxes.c new file mode 100644 index 0000000..6ad27bb --- /dev/null +++ b/21st_century_c/boxes.c @@ -0,0 +1,69 @@ +#include +#include +#include + + + +typedef struct { + char *name; + int left, right, up, down; +} direction_s; + + +void this_row(direction_s d); +void draw_box(direction_s d); + + +int main() { + + direction_s D = {.name = "left", .left = 1}; + draw_box(D); + + // name is first, so we can omit label + D = (direction_s) {"upper right", .up = 1, .right = 1}; + draw_box(D); + + // all struct elements set to zero + draw_box((direction_s){}); + +} + + +void this_row(direction_s d) { + + char s[] = ".*.\n"; + + if(d.left) { + strlcpy(s, "*..\n", sizeof(s)); + } else if(d.right) { + strlcpy(s, "..*\n", sizeof(s)); + } + + printf("%s", s); + +} + + +void draw_box(direction_s d) { + + printf("%s:\n", (d.name ? d.name : "a box")); + + + if(d.up) { + this_row(d); + } else { + printf("...\n"); + } + if(!d.up && !d.down) { + this_row(d); + } else { + printf("...\n"); + } + if(d.down) { + this_row(d); + } else { + printf("...\n"); + } + printf("\n"); + +} \ No newline at end of file