Guest User

Untitled

a guest
Jan 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <getopt.h>
  4. #include "mylib.h"
  5. #include "tree.h"
  6.  
  7. /* A boolean type which can be TRUE or FALSE */
  8. typedef enum bool_e {FALSE, TRUE} bool_t;
  9.  
  10. static void usage(char *progname);
  11. static void setup(int argc, char **argv, char **filename, bool_t *rbt,
  12. bool_t *do_depth, bool_t *p_order, bool_t *output_graph);
  13. static void make_graph(char *filename, tree t);
  14.  
  15. static void usage(char *prog_name) {
  16. fprintf(stderr, "Usage: %s [OPTION]... <STDIN>nn%s%s", prog_name,
  17. "Perform various operations using a hash-table. By default readn"
  18. "words from stdin and print them with their frequencies to stdout.nn"
  19. " -d prints the tree depthn"
  20. " -p prints the tree in pre_ordern"
  21. " -o FILENAME prints output to a specified filenn"
  22. " -r creates a rbt treen"
  23. " -h Display this messagenn");
  24. }
  25.  
  26. static void print_key(char *key) {
  27. printf("%sn", key);
  28. }
  29.  
  30. static void make_graph(char *filename, tree t) {
  31. FILE *file = fopen(filename, "w");
  32. fprintf(stderr, "Creating dot file '%s'n", filename);
  33. tree_output_dot(t, file);
  34. fclose(file);
  35. }
  36.  
  37.  
  38.  
  39. /**
  40. * Handle options given on the command-line by setting a number of
  41. * variables appropriately. May call usage() if incorrect arguments
  42. * or -h given.
  43. *
  44. * @param argc the number of command-line arguments.
  45. * @param argv an array of strings contain the command-line arguments.
  46. * @param rbt is created if -r is given
  47. * @param the tree depth is printed if d is given
  48. * @param tree_outputfile_dot is called if -o output-filename is given
  49. */
  50.  
  51. int main(int argc, char **argv) {
  52. bool_t rbt = FALSE, do_depth = FALSE, p_order = FALSE, output_graph = FALSE;
  53. tree_t tree_type = BST;
  54. tree t;
  55. char word[256];
  56. char *dot_filename;
  57.  
  58. setup(argc, argv, &dot_filename, &rbt, &do_depth, &p_order, &output_graph);
  59. if (rbt) {
  60. t = tree_new(RBT);
  61. } else {
  62. t = tree_new(tree_type);
  63. }
  64. while ((getword(word, sizeof word, stdin)) != EOF) {
  65. t = tree_insert(t, word);
  66. }
  67.  
  68. if (do_depth) {
  69. printf("%dn", tree_depth(t));
  70. } else if (p_order) {
  71. tree_preorder(t, print_key);
  72. } else if (output_graph) {
  73. make_graph(dot_filename, t);
  74. } else {
  75. tree_inorder(t, print_key);
  76. }
  77.  
  78.  
  79. t = tree_free(t);
  80. return EXIT_SUCCESS;
  81. }
  82.  
  83.  
  84. static void setup(int argc, char **argv, char **filename, bool_t *rbt,
  85. bool_t *do_depth, bool_t *p_order, bool_t *output_graph) {
  86. const char *optstring = "do:prh";
  87.  
  88. char option;
  89.  
  90. while ((option = getopt(argc, argv, optstring)) != EOF) {
  91. switch (option) {
  92. case 'd':
  93. *do_depth = TRUE;
  94. break;
  95. case 'o':
  96. *filename = optarg;
  97. *output_graph = TRUE;
  98. break;
  99. case 'p':
  100. *p_order = TRUE;
  101. break;
  102. case 'r':
  103. *rbt = TRUE;
  104. break;
  105. case 'h':
  106. default:
  107. usage(argv[0]);
  108. exit(EXIT_SUCCESS);
  109. }
  110. }
  111. }
  112.  
  113. char *dot_filename = NULL;
Add Comment
Please, Sign In to add comment