fork download
  1. #include <stddef.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <limits.h>
  5. #include <getopt.h>
  6.  
  7. #define spc(x, xs, c) \
  8.   ((x) % (xs) == 0 ? (c) : ' ')
  9.  
  10.  
  11. void line(size_t xwidth, size_t xlength, size_t space, const char* c)
  12. {
  13. size_t x;
  14.  
  15. for (x = 0; x < xwidth; ++x)
  16. {
  17. putchar(spc(x, space, c[0]));
  18. }
  19.  
  20. for (; x < xlength; ++x)
  21. {
  22. putchar(spc(x, space, c[1]));
  23. }
  24.  
  25. for (; x < xlength + xwidth; ++x)
  26. {
  27. putchar(spc(x, space, c[2]));
  28. }
  29.  
  30. for (; x < xlength * 2; ++x)
  31. {
  32. putchar(spc(x, space, c[3]));
  33. }
  34.  
  35. for (; x < xlength * 2 + xwidth; ++x)
  36. {
  37. putchar(spc(x, space, c[4]));
  38. }
  39.  
  40. putchar('\n');
  41. }
  42.  
  43.  
  44. void draw(size_t size, size_t thickness, size_t spacing, char c)
  45. {
  46. size_t xwidth = thickness * spacing;
  47. size_t xlength = size * spacing;
  48. size_t ywidth = thickness;
  49. size_t ylength = size;
  50. size_t y;
  51.  
  52. for (y = 0; y < ywidth; ++y)
  53. {
  54. char chars[] = {c, ' ', c, c, c};
  55. line(xwidth, xlength, spacing, chars);
  56. }
  57.  
  58. for (; y < ylength; ++y)
  59. {
  60. char chars[] = {c, ' ', c, ' ', ' '};
  61. line(xwidth, xlength, spacing, chars);
  62. }
  63.  
  64. for (; y < ylength + ywidth; ++y)
  65. {
  66. char chars[] = {c, c, c, c, c,};
  67. line(xwidth, xlength, spacing, chars);
  68. }
  69.  
  70. for (; y < ylength * 2; ++y)
  71. {
  72. char chars[] = {' ', ' ', c, ' ', c};
  73. line(xwidth, xlength, spacing, chars);
  74. }
  75.  
  76. for (; y < ylength * 2 + ywidth; ++y)
  77. {
  78. char chars[] = {c, c, c, ' ', c};
  79. line(xwidth, xlength, spacing, chars);
  80. }
  81. }
  82.  
  83.  
  84. size_t parse_number(const char* string)
  85. {
  86. size_t value;
  87. char* ptr;
  88.  
  89. value = strtoul(string, &ptr, 0);
  90. if (value == 0 || value == ULONG_MAX || ptr == NULL || *ptr != '\0')
  91. {
  92. fprintf(stderr, "Not a valid number: %s\n", optarg);
  93. exit(1);
  94. }
  95.  
  96. return value;
  97. }
  98.  
  99.  
  100. int main(int argc, char **argv)
  101. {
  102. size_t armlength = 3;
  103. size_t thickness = 2;
  104. size_t spacing = 2;
  105. char character = 'x';
  106. int opt;
  107.  
  108. while ((opt = getopt(argc, argv, "-:l:w:s:h")) != -1)
  109. {
  110. switch (opt)
  111. {
  112. case ':':
  113. fprintf(stderr, "Option -%c requires a value\n", optopt);
  114. exit(1);
  115.  
  116. case '?':
  117. fprintf(stderr, "Unknown option -%c\n", optopt);
  118. exit(1);
  119.  
  120. case 'l':
  121. armlength = parse_number(optarg);
  122. break;
  123.  
  124. case 'w':
  125. thickness = parse_number(optarg);
  126. break;
  127.  
  128. case 's':
  129. spacing = parse_number(optarg);
  130. break;
  131.  
  132. case 'h':
  133. fprintf(stderr, "Usage: %s [-l <arm length>] [-w <arm thickness>] [-s <horizontal spacing>]\n", argv[0]);
  134. exit(2);
  135. }
  136. }
  137.  
  138. draw(armlength, thickness, spacing, character);
  139. exit(0);
  140. }
  141.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
x x   x x x x x 
x x   x x x x x 
x x   x x       
x x x x x x x x 
x x x x x x x x 
      x x   x x 
x x x x x   x x 
x x x x x   x x