fork download
  1. /* ACM Mid-Central Regional Programming Contest */
  2. /* Solution to Problem F, "Instruens Fabulam" */
  3. /* by Dr. Eric Shade, Computer Science Dept. */
  4. /* Southwest Missouri State University */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #define LEFT '<'
  11. #define RIGHT '>'
  12. #define CENTER '='
  13.  
  14. enum {MAX_ROWS = 21; MAX_COLS = 19};
  15. enum {HEADER; ROW; BOTTOM};
  16.  
  17. FILE *in;
  18. FILE *out;
  19.  
  20. int rows; /* number of rows in the table */
  21. int columns; /* number of columns in the table */
  22.  
  23. char align[MAX_COLS + 1]; /* alignment of each column */
  24. int max_width[MAX_COLS]; /* maximum width of column */
  25.  
  26. char *entry[MAX_ROWS][MAX_COLS]; /* text of each entry */
  27. int width[MAX_ROWS][MAX_COLS]; /* width of each entry */
  28.  
  29. char line[80 + 1]; /* the last line read from the file */
  30. int line_type; /* HEADER, ROW, or BOTTOM */
  31.  
  32.  
  33.  
  34. int read_line(void)
  35. {
  36. fgets(line, sizeof(line), in);
  37. line[strlen(line) - 1] = '\0';
  38.  
  39. if (line[0] == '*')
  40. line_type = BOTTOM;
  41. else if (line[0]==LEFT || line[0]==RIGHT || line[0]==CENTER)
  42. line_type = HEADER;
  43. else
  44. line_type = ROW;
  45.  
  46. return line_type;
  47. }
  48.  
  49.  
  50.  
  51. void process_header(void)
  52. {
  53. int c;
  54.  
  55. columns = strlen(line);
  56. strcpy(align, line);
  57.  
  58. for (c = 0; c < columns; ++c)
  59. max_width[c] = 0;
  60. }
  61.  
  62.  
  63.  
  64. void read_rows(void)
  65. {
  66. rows = 0;
  67.  
  68. while (read_line() == ROW) {
  69. char *s;
  70. int c = 0;
  71.  
  72. for (s = strtok(line, "&"); s != NULL; s = strtok(NULL, "&")) {
  73. int n = strlen(s);
  74.  
  75. entry[rows][c] = strdup(s);
  76. width[rows][c] = n;
  77.  
  78. if (n > max_width[c]) max_width[c] = n;
  79.  
  80. ++c;
  81. }
  82.  
  83. ++rows;
  84. }
  85. }
  86.  
  87.  
  88.  
  89. void print_char(int ch, int n)
  90. {
  91. int i;
  92.  
  93. for (i = 0; i < n; ++i) fputc(ch, out);
  94. }
  95.  
  96.  
  97.  
  98. void print_row(int r)
  99. {
  100. int c;
  101.  
  102. for (c = 0; c < columns; ++c) {
  103. int n = width[r][c];
  104. int pad = max_width[c] - n;
  105.  
  106. fprintf(out, "| ");
  107. switch (align[c]) {
  108. case LEFT:
  109. fprintf(out, "%s", entry[r][c]);
  110. print_char(' ', pad);
  111. break;
  112. case RIGHT:
  113. print_char(' ', pad);
  114. fprintf(out, "%s", entry[r][c]);
  115. break;
  116. case CENTER:
  117. print_char(' ', pad / 2);
  118. fprintf(out, "%s", entry[r][c]);
  119. print_char(' ', pad - (pad/2));
  120. }
  121. fprintf(out, " ");
  122. }
  123.  
  124. fprintf(out, "|\n");
  125. }
  126.  
  127.  
  128.  
  129. void print_separator(int outer_char, int inner_char)
  130. {
  131. int c;
  132.  
  133. print_char(outer_char, 1);
  134. print_char('-', max_width[0] + 2);
  135.  
  136. for (c = 1; c < columns; ++c) {
  137. print_char(inner_char, 1);
  138. print_char('-', max_width[c] + 2);
  139. }
  140.  
  141. print_char(outer_char, 1);
  142. fputc('\n', out);
  143. }
  144.  
  145.  
  146.  
  147. int main(void)
  148. {
  149. in = fopen("fab.in", "r");
  150. out = fopen("fab.out", "w");
  151.  
  152. read_line();
  153. while (line_type != BOTTOM) {
  154. int r;
  155.  
  156. process_header();
  157. read_rows();
  158.  
  159. print_separator('@', '-');
  160. print_row(0);
  161. print_separator('|', '+');
  162.  
  163. for (r = 1; r < rows; ++r) print_row(r);
  164.  
  165. print_separator('@', '-');
  166. }
  167.  
  168. fclose(in);
  169. fclose(out);
  170.  
  171. return 0;
  172. }
  173.  
Runtime error #stdin #stdout 0.01s 4272KB
stdin
Standard input is empty
stdout
Standard output is empty