Advertisement
AlexandrTalchuk

Untitled

May 17th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.72 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, cnt = 0, g = 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.  
  141. cout << "Введите ключ" << endl;
  142. cin.getline(place, 81);
  143. cin.ignore();
  144. air temp;
  145. for (int i = 0; i < size; i++)
  146. {
  147. fread(&temp, sizeof(air), 1, f);
  148. if (strcmp(temp.area, place) == 0) cnt++;
  149. }
  150. air* array = new air[cnt];
  151. fseek(f, 0, SEEK_SET);
  152. for (int i = 0; i < size; i++)
  153. {
  154. fread(&temp, sizeof(air), 1, f);
  155. if (strcmp(temp.area, place) == 0) array[g++] = temp;
  156. }
  157.  
  158. cout << "1.Линейный поиск" << "\n2.Бинарный поиск" << endl;
  159. cin >> choice;
  160. switch (choice)
  161. {
  162. case 1:
  163. {
  164. int i_key = 0, kod = 0;
  165. for (int i = 0; i < size; i++)
  166. if (!strcmp(array[i].area, place))
  167. {
  168. kod = 1;
  169. i_key = i;
  170. cout << "Интересуемый Вас элемент расположен в ячейке под номером: " << i_key << endl;
  171. return;
  172.  
  173. }
  174. if (kod == 0)
  175. cout << "По вашему запросу ничего не найдено" << endl;
  176. return;
  177.  
  178. }
  179. }
  180. }
  181. void Sorting()
  182. {
  183. FILE* f;
  184. char filename[81];
  185. char place[81];
  186. int choice, cnt = 0, g = 0,k=0;
  187. cout << "Введите имя файла" << endl;
  188. cin.ignore();
  189. cin.getline(filename, 81);
  190. f = fopen(filename, "r+");
  191. if (f == NULL)
  192. {
  193. cout << "Такого файла не обнаружено!" << endl;
  194. return;
  195. }
  196. int size = _filelength(_fileno(f));
  197. size /= sizeof(air);
  198.  
  199. cout << "Введите ключ";
  200. cin.getline(place, 81);
  201. cin.ignore();
  202.  
  203. for (int i = 0; i < size; i++)
  204. {
  205. air temp;
  206. fread(&temp, sizeof(air), 1, f);
  207. if (strcmp(temp.area, place) == 0) cnt++;
  208. }
  209. air* array = new air[cnt];
  210. air* array1= new air[cnt];
  211. fseek(f, 0, SEEK_SET);
  212. for (int i = 0; i < size; i++)
  213. {
  214. air temp;
  215. fread(&temp, sizeof(air), 1, f);
  216. if (strcmp(temp.area, place) == 0) array[g++] = temp;
  217. else array1[k++] = temp;
  218. }
  219. cout << "1.QuickSort\n" << "2.Сортировка прямым выбором\n" << endl;
  220. cin >> choice;
  221. switch (choice)
  222. {
  223. case 1:
  224. {
  225. Stack* begin = new Stack;
  226. begin->left = 0;
  227. begin->right = cnt - 1;
  228. begin->next = NULL;
  229. int i, j, l1, r1;
  230. do
  231. {
  232. i = begin->left;
  233. j = begin->right;
  234. double x = array[(i + j) / 2].time;
  235. do
  236. {
  237. while (x > array[i].time) i++;
  238. while (x < array[j].time) j--;
  239. if (i <= j)
  240. {
  241. air k = array[i];
  242. array[i] = array[j];
  243. array[j] = k;
  244. i++;
  245. j--;
  246. }
  247. } while (i < j);
  248. l1 = begin->left;
  249. r1 = begin->right;
  250. Stack* t = begin;
  251. begin = begin->next;
  252. delete t;
  253. if (l1 < j)
  254. {
  255. Stack* t = new Stack;
  256. t->next = begin;
  257. t->left = l1;
  258. t->right = j;
  259. begin = t;
  260. }
  261. if (i < r1)
  262. {
  263. Stack* t = new Stack;
  264. t->next = begin;
  265. t->left = i;
  266. t->right = r1;
  267. begin = t;
  268. }
  269. } while (begin != NULL);
  270. }
  271. break;
  272. case 2:
  273. for (int i = 0; i < size - 1; i++)
  274. {
  275. int m = i;
  276. for (int j = i + 1; j < size; j++)
  277. if (array[i].time > array[j].time) m = j;
  278. if (m != i)
  279. {
  280. air r = array[m];
  281. array[m] = array[i];
  282. array[i] = r;
  283. }
  284. }
  285. cout << "Отсортировано!" << endl;
  286. break;
  287. default:
  288. cout << "Неверный ввод" << endl;
  289. return;
  290. break;
  291. }
  292. fseek(f, 0, SEEK_SET);
  293. fclose(f);
  294. fopen(filename, "w");
  295. fwrite(array, sizeof(air), cnt, f);
  296. fwrite(array1, sizeof(air), size-cnt, f);
  297. fclose(f);
  298. delete[] array;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement