Advertisement
AlexandrTalchuk

Untitled

May 17th, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. void Searching()
  2. {
  3. FILE* f;
  4. air a1;
  5. char filename[81],place[81];
  6. int choice, cnt = 0, g = 0;
  7. cout << "Введите имя файла" << endl;
  8. cin.ignore();
  9. cin.getline(filename, 81);
  10. f = fopen(filename, "r+");
  11. if (f == NULL)
  12. {
  13. cout << "Данного файла не существует" << endl;
  14. return;
  15. }
  16. fseek(f, 0, SEEK_SET);
  17. int size = _filelength(_fileno(f));
  18. size /= sizeof(air);
  19.  
  20. cout << "Введите ключ" << endl;
  21. cin.getline(place, 81);
  22. cin.ignore();
  23. air temp;
  24. for (int i = 0; i < size; i++)
  25. {
  26. fread(&temp, sizeof(air), 1, f);
  27. if (strcmp(temp.area, place) == 0) cnt++;
  28. }
  29. air* array = new air[cnt];
  30. fseek(f, 0, SEEK_SET);
  31. for (int i = 0; i < size; i++)
  32. {
  33. fread(&temp, sizeof(air), 1, f);
  34. if (strcmp(temp.area, place) == 0) array[g++] = temp;
  35. }
  36.  
  37. cout << "1.Линейный поиск" << "\n2.Бинарный поиск" << endl;
  38. cin >> choice;
  39. switch (choice)
  40. {
  41. case 1:
  42. {
  43. int i_key = 0, kod = 0;
  44. for (int i = 0; i < size; i++)
  45. if (array[i].area == place)
  46. {
  47. kod = 1;
  48. i_key = i;
  49. cout << "Интересуемый Вас элемент расположен в ячейке под номером: " << i_key << endl;
  50. return;
  51.  
  52. }
  53. if (kod == 0)
  54. cout << "По вашему запросу ничего не найдено" << endl;
  55. return;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement