Advertisement
AlexandrTalchuk

Untitled

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