Advertisement
Guest User

Untitled

a guest
May 24th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <iomanip>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <fstream>
  8. #include <windows.h>
  9. #include <codecvt>
  10. #undef max
  11.  
  12. HANDLE hStdOut;
  13.  
  14. void ReadFile(std::vector<std::wstring>& text);
  15. void Search(std::vector<std::wstring>& text);
  16. void SearchInString(std::wstring& str, std::wstring tgt);
  17.  
  18. int main()
  19. {
  20. hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); // хэндл для изменения цвета текста
  21. std::wcin.imbue(std::locale("rus_rus.866")); // рус яз
  22. std::wcout.imbue(std::locale("rus_rus.866"));
  23. std::vector<std::wstring> text; // массив строк
  24. std::wstring buffer;
  25. std::wstring path;
  26. for (;;)
  27. {
  28. std::wcout << L"Введите путь к файлу с текстом:" << std::endl;
  29. std::getline(std::wcin, path);
  30. if (_waccess_s(path.c_str(), 4)) // проверка файла
  31. {
  32. std::wcout << L"Не удалось получить доступ к файлу" << std::endl;
  33. continue;
  34. }
  35. break;
  36. }
  37. bool isBom = false;
  38. std::wifstream checkBom(path);
  39. wchar_t bom[3];
  40. checkBom.read(bom, 3);
  41. if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)
  42. isBom = true;
  43. checkBom.close();
  44. std::wifstream file(path); // создание файлового потока
  45. if (isBom)
  46. file.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t, 0x10ffff, std::consume_header>));
  47. else
  48. file.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
  49. while (std::getline(file, buffer)) // читаем и добавляем в массив
  50. text.push_back(buffer);
  51. std::wcout << L"\nПолучившийся текст:" << std::endl;
  52. for (const auto& line : text) // печатаем
  53. {
  54. //line.erase(std::remove_if(line.))
  55. std::wcout << line << std::endl;
  56. }
  57. std::wcout << std::endl;
  58. if (text.empty())
  59. {
  60. std::wcout << L"Файл пуст" << std::endl;
  61. system("pause");
  62. return 0;
  63. }
  64. int line, menu = 0;
  65. for (;;)
  66. {
  67. std::wcout << L"Выберите действие:" << std::endl;
  68. std::wcout << L"1) поиск подстроки во всем тексте" << std::endl;
  69. std::wcout << L"2) поиск подстроки в строке" << std::endl;
  70. std::wcout << L"3) вставка текста из файла" << std::endl;
  71. std::wcout << L"4) выход\n" << std::endl;
  72. if (!(std::wcin >> menu))
  73. {
  74. std::wcout << L"Ошибка ввода" << std::endl;
  75. std::wcin.clear();
  76. std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
  77. continue;
  78. }
  79. switch (menu)
  80. {
  81. case 1:
  82. Search(text);
  83. break;
  84. case 2:
  85. for (;;)
  86. {
  87. std::wcout << L"Номер строки:" << std::endl;
  88. if (!(std::wcin >> line) || line < 1 || line > text.size())
  89. {
  90. std::wcout << L"Ошибка ввода" << std::endl;
  91. std::wcin.clear();
  92. std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
  93. continue;
  94. }
  95. break;
  96. }
  97. std::wcout << L"Искомая подстрока:" << std::endl;
  98. std::wcin.clear();
  99. std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
  100. std::getline(std::wcin, buffer);
  101. SearchInString(text[line - 1], buffer);
  102. break;
  103. case 3:
  104. ReadFile(text);
  105. break;
  106. case 4:
  107. system("pause");
  108. return 0;
  109. default:
  110. std::wcout << L"Ошибка! Такого пункта не существует" << std::endl;
  111. }
  112. }
  113. }
  114.  
  115. void ReadFile(std::vector<std::wstring>& text)
  116. {
  117. std::wstring path;
  118. for (;;)
  119. {
  120. std::wcout << L"Введите путь к файлу с текстом:" << std::endl;
  121. std::wcin.clear();
  122. std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
  123. std::getline(std::wcin, path);
  124. if (_waccess_s(path.c_str(), 4)) // проверка файла
  125. {
  126. std::wcout << L"Не удалось получить доступ к файлу" << std::endl;
  127. continue;
  128. }
  129. break;
  130. }
  131. std::wstring buffer;
  132. bool isBom = false;
  133. std::wifstream checkBom(path);
  134. wchar_t bom[3];
  135. checkBom.read(bom, 3);
  136. if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)
  137. isBom = true;
  138. checkBom.close();
  139. std::wifstream file(path); // создание файлового потока
  140. if (isBom)
  141. file.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t, 0x10ffff, std::consume_header>));
  142. else
  143. file.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
  144. while (std::getline(file, buffer)) // читаем и добавляем в массив
  145. text.push_back(buffer);
  146. std::wcout << L"\nПолучившийся текст:" << std::endl;
  147. for (const auto& line : text) // печатаем
  148. std::wcout << line << std::endl;
  149. std::wcout << std::endl;
  150. }
  151.  
  152. void Search(std::vector<std::wstring>& text)
  153. {
  154. std::wstring target;
  155. std::wcout << L"Искомая подстрока:" << std::endl;
  156. std::wcin.clear();
  157. std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
  158. std::getline(std::wcin, target);
  159. std::wcout << std::endl;
  160. for (auto& str : text)
  161. SearchInString(str, target);
  162. std::wcout << std::endl;
  163. }
  164.  
  165. void SearchInString(std::wstring& str, std::wstring tgt)
  166. {
  167. int prevPos = 0; // предыдущая позиция, в которой нашли символ
  168. for (int pos = 0 - tgt.length();;)
  169. {
  170. pos = str.find(tgt, pos + tgt.length()); // ищем строку
  171. if (pos == std::wstring::npos) // если не нашли
  172. {
  173. std::wcout << str.substr(prevPos) << std::endl; // печатаем все, что осталось
  174. return;
  175. }
  176. std::wcout << str.substr(prevPos, pos - prevPos); // печатаем до найденного
  177. SetConsoleTextAttribute(hStdOut, FOREGROUND_GREEN); // делаем зеленый цвет
  178. std::wcout << str.substr(pos, tgt.length()); // саму найденную подстроку
  179. SetConsoleTextAttribute(hStdOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); // возвращаем цвет
  180. prevPos = pos + tgt.length();
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement