Advertisement
Guest User

main.c

a guest
May 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. #include "tree.h"
  2. #include "data.h"
  3. #include "ioutils.h"
  4. #include <stdio.h>
  5.  
  6. void menu()
  7. {
  8. printf( "1 - tree_init()\n"
  9. "2 - tree_clear()\n"
  10. "3 - tree_insert()\n"
  11. "4 - tree_delete()\n"
  12. "5 - tree_find_node()\n"
  13. "6 - tree_get_node_count()\n"
  14. "7 - tree_proces()\n"
  15. "8 - tree_get_height()\n"
  16. "9 - tree_get_width()\n"
  17. "M - zobraz toto menu\n"
  18. "K - konec\n"
  19. "Pro ukonceni stiskni CTRL+D (Linux) nebo CTRL+Z (Windows).\n\n" );
  20. }
  21.  
  22. void process_tree_node( TreeNode* node )
  23. {
  24. Data_Print( &node->data );
  25. printf( " | L -> " );
  26.  
  27. if( node->left ) {
  28. Data_Print( &node->left->data );
  29. } else {
  30. printf( "NULL" );
  31. }
  32.  
  33. printf( " | R -> " );
  34.  
  35. if( node->right ) {
  36. Data_Print( &node->right->data );
  37. } else {
  38. printf( "NULL" );
  39. }
  40.  
  41. printf( "\n" );
  42. }
  43.  
  44. int main( int argc, char** argv )
  45. {
  46. ( void ) argc; // unsed
  47. ( void ) argv; // unsed
  48.  
  49.  
  50. printf( "Strom v1.0\n" );
  51. printf( "----------\n\n" );
  52. char v;
  53. Tree tree = {NULL, 0};
  54. bool initialized = false;
  55. menu();
  56. bool running = true;
  57.  
  58. while( running ) {
  59. printf( "Vase volba: " );
  60. running = io_utils_get_char( &v );
  61.  
  62. if( running ) {
  63. printf( "%c\n", v );
  64. }
  65.  
  66. if( ( initialized || v > '9' || v == '1' ) && running ) {
  67. switch( v ) {
  68. case '1':
  69. if( initialized ) {
  70. printf( "Strom jiz byl inicializovan.\n" );
  71. break;
  72. }
  73.  
  74. initialized = Tree_Init( &tree );
  75.  
  76. if( initialized ) {
  77. printf( "Strom byl inicializovan.\n" );
  78. } else {
  79. printf( "Strom nebyl inicializovan.\n" );
  80. }
  81.  
  82. break;
  83.  
  84. case '2':
  85. Tree_Clear( &tree );
  86. printf( "Obsah stromu byl odstranen.\n" );
  87. break;
  88.  
  89. case '3': {
  90. printf( "Data uzlu stromu:\n" );
  91. Data_t data;
  92. running = Data_Get( &data );
  93.  
  94. if( !running ) {
  95. continue;
  96. }
  97.  
  98. if( !Tree_Insert( &tree, data ) ) {
  99. printf( "Zadana hodnota je jiz ve strome obsazena!\n" );
  100. }
  101.  
  102. break;
  103. }
  104.  
  105. case '4':
  106. printf( "Zadej mazana data:\n" );
  107. Data_t data;
  108. running = Data_Get( &data );
  109.  
  110. if( !running ) {
  111. continue;
  112. }
  113.  
  114. Tree_Delete( &tree, data );
  115. break;
  116.  
  117. case '5': {
  118. printf( "Zadej hledana data:\n" );
  119. Data_t data;
  120. running = Data_Get( &data );
  121.  
  122. if( !running ) {
  123. continue;
  124. }
  125.  
  126. TreeNode *node = Tree_Find_Node( tree, data );
  127.  
  128. if( node ) {
  129. printf( "Data byla nalezena v uzlu\n" );
  130. } else {
  131. printf( "Data nebyla ve strome nalezena.\n" );
  132. }
  133.  
  134. break;
  135. }
  136.  
  137. case '6':
  138. printf( "Pocet uzlu stromu: %ld\n", Tree_Get_Count( tree ) );
  139. break;
  140.  
  141. case '7': {
  142. long mode = 0;
  143. printf( "Pruchod:\n1 - preorder\n2 - inorder\n3 - postorder\n\nVase volba: " );
  144. running = io_utils_get_long( &mode );
  145.  
  146. if( !running ) {
  147. continue;
  148. }
  149.  
  150. printf( "%ld\n\n", mode );
  151.  
  152. if( mode >= 1 && mode <= 3 ) {
  153. Tree_Process( tree, ( TreeNodeProc )process_tree_node, ( TreeProcessMode )mode );
  154. } else {
  155. printf( "Neznama volba!\n" );
  156. }
  157.  
  158. break;
  159. }
  160.  
  161. case '8':
  162. printf("Hloubka stromu je: %d\n", Tree_Get_Height(&tree));
  163. break;
  164.  
  165. case '9':
  166. printf("Sirka stromu je: %d\n", Tree_Get_Width(&tree));
  167. break;
  168.  
  169. case 'm':
  170. case 'M':
  171. menu();
  172. break;
  173.  
  174. case 'k':
  175. case 'K':
  176. running = false;
  177. printf( "Konec.\n" );
  178. break;
  179.  
  180. default:
  181. printf( "Neznama volba!\n" );
  182. break;
  183. }
  184. } else if( !initialized ) {
  185. printf( "Strom neni inicializovan!\n" );
  186. }
  187.  
  188. printf( "\n" );
  189. }
  190.  
  191. if( initialized ) {
  192. Tree_Clear( &tree );
  193. }
  194.  
  195. return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement