Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
945 B

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. typedef struct {
  5. char *name;
  6. int left, right, up, down;
  7. } direction_s;
  8. void this_row(direction_s d);
  9. void draw_box(direction_s d);
  10. int main() {
  11. direction_s D = {.name = "left", .left = 1};
  12. draw_box(D);
  13. // name is first, so we can omit label
  14. D = (direction_s) {"upper right", .up = 1, .right = 1};
  15. draw_box(D);
  16. // all struct elements set to zero
  17. draw_box((direction_s){});
  18. }
  19. void this_row(direction_s d) {
  20. char *c;
  21. if(d.left) {
  22. c = "*..";
  23. } else if(d.right) {
  24. c = "..*";
  25. } else {
  26. c = ".*.";
  27. }
  28. printf("%s\n", c);
  29. }
  30. void draw_box(direction_s d) {
  31. printf("%s:\n", (d.name ? d.name : "a box"));
  32. if(d.up) {
  33. this_row(d);
  34. } else {
  35. printf("...\n");
  36. }
  37. if(!d.up && !d.down) {
  38. this_row(d);
  39. } else {
  40. printf("...\n");
  41. }
  42. if(d.down) {
  43. this_row(d);
  44. } else {
  45. printf("...\n");
  46. }
  47. printf("\n");
  48. }