Advertisement
AlexandrTalchuk

Untitled

May 19th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.90 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. // Вывести информацию для заданного пункта назначения в порядке возрастаняи времени вылета. Ключ: пункт назначения
  3. #include <iostream>
  4. #include <io.h>
  5. #include <conio.h>
  6. #include<cstdio>
  7.  
  8. using namespace std;
  9.  
  10. void Add();
  11. void Watching();
  12. void Searching();
  13. void Sorting();
  14. void Searching();
  15.  
  16. struct Stack
  17. {
  18. int left, right;
  19. Stack* next;
  20. };
  21.  
  22. struct air
  23. {
  24. int number;
  25. char type[36];
  26. char area[36];
  27. double time;
  28. };
  29.  
  30.  
  31.  
  32.  
  33. int main()
  34. {
  35. setlocale(LC_ALL, "rus");
  36. int choice;
  37. while (true)
  38. {
  39. cout << "1. Добавление\n2. Просмотр\n3. Сортировка\n4. Поиск\n5. Выход" << endl;
  40. cin >> choice;
  41. switch (choice)
  42. {
  43. case 1:
  44. Add();
  45. cout << "Информация сохранена, нажмите любую кнопку" << endl;
  46. _getch();
  47. break;
  48. case 2:
  49. Watching();
  50. _getch();
  51. break;
  52. case 3:
  53. Sorting();
  54. _getch();
  55. break;
  56. case 4:
  57. Searching();
  58. _getch();
  59. break;
  60. case 5:
  61. cout << "Программа завершена" << endl;
  62. return 0;
  63. break;
  64. default:
  65. cout << "Повторите еще раз" << endl;
  66. return 0;
  67. break;
  68. }
  69. system("cls");
  70. }
  71. }
  72.  
  73. void Add()
  74. {
  75. FILE* f;
  76. air one;
  77. char filename[81];
  78. cout << "Введите имя файла" << endl;
  79. cin >> filename;
  80. f = fopen(filename, "a");
  81. if (f == NULL)
  82. {
  83. cout << "Данного файла не существует" << endl;
  84. return;
  85. }
  86. cout << "Введите номер рейса" << endl;
  87. cin >> one.number;
  88. cout << "Ведите тип самолета" << endl;
  89. cin >> one.type;
  90. cout << "Ведите пункт назначения" << endl;
  91. cin >> one.area;
  92. cout << "Ведите время вылета" << endl;
  93. cin >> one.time;
  94. fwrite(&one, sizeof(air), 1, f);
  95. fclose(f);
  96. }
  97.  
  98. void Watching()
  99. {
  100. FILE* f;
  101. air one;
  102. char filename[81];
  103. cout << "Введите имя файла" << endl;
  104. cin >> filename;
  105. f = fopen(filename, "r");
  106. if (f == NULL)
  107. {
  108. cout << "Данного файла не существует" << endl;
  109. return;
  110. }
  111. while (fread(&one, sizeof(air), 1, f) == 1)
  112. {
  113. cout << "Номер рейса " << one.number << endl;
  114. cout << "Тип самолета " << one.type << endl;
  115. cout << "Пункт назначения " << one.area << endl;
  116. cout << "Время вылета " << one.time << endl << endl;
  117. }
  118. fclose(f);
  119. return;
  120. }
  121.  
  122. void Searching()
  123. {
  124. FILE* f;
  125. air a1;
  126. char filename[81], place[81];
  127. int choice, middle=0;
  128. cout << "Введите имя файла" << endl;
  129. cin.ignore();
  130. cin.getline(filename, 81);
  131. f = fopen(filename, "r");
  132. if (f == NULL)
  133. {
  134. cout << "Данного файла не существует" << endl;
  135. return;
  136. }
  137. fseek(f, 0, SEEK_SET);
  138. int size = _filelength(_fileno(f));
  139. size /= sizeof(air);
  140. char* mas = new char[size];
  141. cout << "Введите ключ: ";
  142. cin.getline(place, 81);
  143. cin.ignore();
  144. cout << "1.Линейный поиск" << "\n2.Бинарный поиск" << endl;
  145. cin >> choice;
  146. for (int i = 0; i < size - 1; i++)
  147. {
  148. for (int j = i + 1; j < size; j++)
  149. {
  150. if (strcmp(&mas[i], &mas[j]) > 0)
  151. {
  152. char tmp = mas[i];
  153. mas[i] = mas[j];
  154. mas[j] = tmp;
  155. }
  156. }
  157. }
  158. switch (choice)
  159. {
  160. case 1:
  161. {
  162. int i_key = 0, kod = 0;
  163. for (int i = 0; i < size; i++)
  164. {
  165. air temp;
  166. fread(&temp, sizeof(air), 1, f);
  167. if (!strcmp(temp.area, place))
  168. {
  169. kod = 1;
  170. i_key = i;
  171. cout << "Интересуемый Вас элемент расположен в ячейке под номером: " << i_key << endl;
  172. cout << "Номер рейса " << temp.number << endl;
  173. cout << "Тип самолета " << temp.type << endl;
  174. cout << "Пункт назначения " << temp.area << endl;
  175. cout << "Время вылета " << temp.time << endl << endl;
  176. return;
  177. }
  178. }
  179. if (kod == 0)
  180. cout << "По вашему запросу ничего не найдено" << endl;
  181. return;
  182.  
  183. }
  184. case 2:
  185.  
  186. int left = 0; // задаем левую и правую границы поиска
  187. int right = size-1;
  188. int search = -1; // найденный индекс элемента равен -1 (элемент не найден)
  189. while (left <= right) // пока левая граница не "перескочит" правую
  190. {
  191. int mid = (left + right) / 2; // ищем середину отрезка
  192. if (place == &mas[mid]) { // если ключевое поле равно искомому
  193. search = mid; // мы нашли требуемый элемент,
  194. break; // выходим из цикла
  195. }
  196. if (place < &mas[mid]) // если искомое ключевое поле меньше найденной середины
  197. right = mid - 1; // смещаем правую границу, продолжим поиск в левой части
  198. else // иначе
  199. left = mid + 1; // смещаем левую границу, продолжим поиск в правой части
  200. }
  201. if (search == -1) // если индекс элемента по-прежнему -1, элемент не найден
  202. printf("Элемент не найден!\n");
  203. else // иначе выводим элемент, его ключ и значение
  204. cout << "Номер рейса " << .number << endl;
  205. cout << "Тип самолета " << .type << endl;
  206. cout << "Пункт назначения " << .area << endl;
  207. cout << "Время вылета " << .time << endl << endl;
  208. return;
  209. break;
  210. }
  211. }
  212.  
  213. void Sorting()
  214. {
  215. FILE* f;
  216. char filename[81];
  217. char place[81];
  218. int choice, cnt = 0, g = 0, k = 0;
  219. cout << "Введите имя файла" << endl;
  220. cin.ignore();
  221. cin.getline(filename, 81);
  222. f = fopen(filename, "r+");
  223. if (f == NULL)
  224. {
  225. cout << "Такого файла не обнаружено!" << endl;
  226. return;
  227. }
  228. int size = _filelength(_fileno(f));
  229. size /= sizeof(air);
  230.  
  231. cout << "Введите ключ: ";
  232. cin.getline(place, 81);
  233. cin.ignore();
  234.  
  235. for (int i = 0; i < size; i++)
  236. {
  237. air temp;
  238. fread(&temp, sizeof(air), 1, f);
  239. if (strcmp(temp.area, place) == 0) cnt++;
  240. }
  241. air* array = new air[cnt];
  242. air* array1 = new air[cnt];
  243. fseek(f, 0, SEEK_SET);
  244. for (int i = 0; i < size; i++)
  245. {
  246. air temp;
  247. fread(&temp, sizeof(air), 1, f);
  248. if (strcmp(temp.area, place) == 0) array[g++] = temp;
  249. else array1[k++] = temp;
  250. }
  251. cout << "1.QuickSort\n" << "2.Сортировка прямым выбором\n" << endl;
  252. cin >> choice;
  253. switch (choice)
  254. {
  255. case 1:
  256. {
  257. Stack* begin = new Stack;
  258. begin->left = 0;
  259. begin->right = cnt - 1;
  260. begin->next = NULL;
  261. int i, j, l1, r1;
  262. do
  263. {
  264. i = begin->left;
  265. j = begin->right;
  266. double x = array[(i + j) / 2].time;
  267. do
  268. {
  269. while (x > array[i].time) i++;
  270. while (x < array[j].time) j--;
  271. if (i <= j)
  272. {
  273. air k = array[i];
  274. array[i] = array[j];
  275. array[j] = k;
  276. i++;
  277. j--;
  278. }
  279. } while (i < j);
  280. l1 = begin->left;
  281. r1 = begin->right;
  282. Stack* t = begin;
  283. begin = begin->next;
  284. delete t;
  285. if (l1 < j)
  286. {
  287. Stack* t = new Stack;
  288. t->next = begin;
  289. t->left = l1;
  290. t->right = j;
  291. begin = t;
  292. }
  293. if (i < r1)
  294. {
  295. Stack* t = new Stack;
  296. t->next = begin;
  297. t->left = i;
  298. t->right = r1;
  299. begin = t;
  300. }
  301. } while (begin != NULL);
  302. cout << "Отсортировано!" << endl;
  303. }
  304. break;
  305. case 2:
  306. for (int i = 0; i < size - 1; i++)
  307. {
  308. int m = i;
  309. for (int j = i + 1; j < size; j++)
  310. if (array[i].time > array[j].time) m = j;
  311. if (m != i)
  312. {
  313. air r = array[m];
  314. array[m] = array[i];
  315. array[i] = r;
  316. }
  317. }
  318. cout << "Отсортировано!" << endl;
  319. break;
  320. default:
  321. cout << "Неверный ввод" << endl;
  322. return;
  323. break;
  324. }
  325. fseek(f, 0, SEEK_SET);
  326. fclose(f);
  327. fopen(filename, "w");
  328. fwrite(array, sizeof(air), cnt, f);
  329. fwrite(array1, sizeof(air), size - cnt, f);
  330. fclose(f);
  331. delete[] array;
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement