Advertisement
Guest User

habibi

a guest
Nov 19th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #include "ui.h"
  2. #include "io.h"
  3. #include "analyze.h"
  4.  
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7.  
  8. //
  9. // Private
  10. //
  11. static void ui_invalid_input()
  12. {
  13. printf("info> incorrect input \n");
  14. }
  15.  
  16. static void ui_exit()
  17. {
  18. printf("info> bye\n");
  19. }
  20.  
  21. static char ui_get_choice()
  22. {
  23. char buf[3];
  24.  
  25. printf("input> ");
  26. return read_line(buf, 3) ? buf[0] : 0;
  27. }
  28.  
  29. static void ui_line(char c, int n)
  30. {
  31. while (n-- > 0) {
  32. putchar(c);
  33. }
  34. putchar('\n');
  35. }
  36.  
  37. static void ui_menu_options(const char *options[], int num_options)
  38. {
  39. int i;
  40.  
  41. for (i=0; i<num_options; i++) {
  42. printf(" %c) %s\n", 'a'+i, options[i]);
  43. }
  44. }
  45.  
  46. static void ui_menu()
  47. {
  48. const char *options[] = {
  49. "Menu",
  50. "Exit\n",
  51. "Bubble sort best case",
  52. "Bubble sort average case",
  53. "Bubble sort worst case",
  54. "Quick sort best case",
  55. "Quick sort average case",
  56. "Quick sort worst case",
  57. "Binary search best case",
  58. "Binary search average case",
  59. "Binary search worst case",
  60. "Linear search best case",
  61. "Linear search average case",
  62. "Linear search worst case",
  63. "Insertion sort best case",
  64. "Insertion sort average case",
  65. "Insertion sort worst case",
  66. };
  67.  
  68. ui_line('=', MENU_WIDTH);
  69. ui_menu_options(options, sizeof(options) / sizeof(char *));
  70. ui_line('-', MENU_WIDTH);
  71. }
  72.  
  73. //
  74. // Public
  75.  
  76. void skriv(result_t *buf)
  77. {
  78. int i;
  79. for(i=0;i<RESULT_ROWS;i++)
  80. {
  81. printf("%.6e\n" , buf[i].time);
  82.  
  83. }
  84. }
  85.  
  86. //
  87. void ui_run()
  88. {
  89. bool running, show_menu;
  90. result_t result[RESULT_ROWS];
  91.  
  92. show_menu = true;
  93. running = true;
  94. while (running) {
  95. if (show_menu) {
  96. show_menu = false;
  97. ui_menu();
  98. }
  99. switch (ui_get_choice()) {
  100. // House keeping
  101. case 'a':
  102. show_menu = true;
  103. break;
  104. case 'b':
  105. running = false;
  106. break;
  107. // Bubble sort
  108. case 'c':
  109. //start=clock();
  110. benchmark(bubble_sort_t, best_t, result, RESULT_ROWS);
  111. //stop=clock();
  112. printf("todo> implemenet BE + present results in FE\n");
  113. break;
  114. // Invalid input
  115. case 'd':
  116. benchmark(bubble_sort_t, average_t, result, RESULT_ROWS);
  117. printf("hej");
  118. break;
  119. case 'e':
  120. benchmark(bubble_sort_t, worst_t, result, RESULT_ROWS);
  121. break;
  122. case 'f':
  123. benchmark(quick_sort_t, best_t, result, RESULT_ROWS);
  124. break;
  125. case 'g':
  126. benchmark(quick_sort_t, average_t, result, RESULT_ROWS);
  127. break;
  128. case 'h':
  129. benchmark(quick_sort_t, worst_t, result, RESULT_ROWS);
  130. break;
  131. case 'i':
  132. benchmark(binary_search_t, best_t, result, RESULT_ROWS);
  133. break;
  134. case 'j':
  135. benchmark(binary_search_t, average_t, result, RESULT_ROWS);
  136. break;
  137. case 'k':
  138. benchmark(binary_search_t, worst_t, result, RESULT_ROWS);
  139. break;
  140. case 'l':
  141. benchmark(linear_search_t, best_t, result, RESULT_ROWS);
  142. break;
  143. case 'm':
  144. benchmark(linear_search_t, average_t, result, RESULT_ROWS);
  145. break;
  146. case 'n':
  147. benchmark(linear_search_t, worst_t, result, RESULT_ROWS);
  148. break;
  149. case 'o':
  150. benchmark(insertion_sort_t, best_t, result, RESULT_ROWS);
  151. break;
  152. case 'p':
  153. benchmark(insertion_sort_t, average_t, result, RESULT_ROWS);
  154. break;
  155. case 'q':
  156. benchmark(insertion_sort_t, worst_t, result, RESULT_ROWS);
  157. break;
  158. case 'r':
  159. skriv(result);
  160. break;
  161.  
  162. default:
  163. show_menu = false;
  164. ui_invalid_input();
  165. break;
  166. }
  167. }
  168. ui_exit();
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement