Advertisement
AlexandrTalchuk

Untitled

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