Advertisement
Guest User

Untitled

a guest
May 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <string>
  6. #include <vector>
  7. #include <windows.h>
  8.  
  9. #undef max()
  10.  
  11. using namespace std;
  12.  
  13. HANDLE hStdOut;
  14.  
  15. void input(vector<wstring>&);
  16. void find(vector<wstring>&);
  17. void copy(vector<wstring>&);
  18. void del(vector<wstring>&);
  19. void printText(vector<wstring>&);
  20.  
  21. int main()
  22. {
  23. hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  24. wcin.imbue(locale("rus_rus.866"));
  25. wcout.imbue(locale("rus_rus.866"));
  26. vector<wstring> text;
  27. wstring buf;
  28. wcout << L"Введите строку:\n";
  29. getline(wcin, buf);
  30. text.emplace_back(buf);
  31. wcout << endl;
  32. for (;;)
  33. {
  34. int menu = 0;
  35. wcout << L"Выберите пункт меню:\n"
  36. L"1: Ввести следующую строку\n"
  37. L"2: Поиск символа (ов)\n"
  38. L"3: Копирование строк\n"
  39. L"4: Удаление символов с конца строки\n"
  40. L"5: Выход\n";
  41. wcin >> menu;
  42. switch (menu)
  43. {
  44. case 1:
  45. input(text);
  46. break;
  47. case 2:
  48. find(text);
  49. break;
  50. case 3:
  51. copy(text);
  52. break;
  53. case 4:
  54. del(text);
  55. break;
  56. case 5:
  57. return 0;
  58. default:
  59. wcout << L"Попробуйте снова\n";
  60. wcin.clear();
  61. wcin.ignore(numeric_limits<streamsize>::max(), '\n');
  62. continue;
  63. }
  64. }
  65. }
  66.  
  67. void input(vector<wstring>& t)
  68. {
  69. wstring buf;
  70. wcout << L"Введите строку:\n";
  71. wcin.clear();
  72. wcin.ignore(numeric_limits<streamsize>::max(), '\n');
  73. getline(wcin, buf);
  74. t.emplace_back(buf);
  75. printText(t);
  76. }
  77.  
  78. void find(vector<wstring>& t)
  79. {
  80. size_t count = 0;
  81. wstring str;
  82. wcout << L"Искать:\n";
  83. wcin >> str;
  84. for (wstring line : t)
  85. {
  86. for (int pos = 0, lastPos = 0; pos < line.length(); pos++)
  87. {
  88. lastPos = pos;
  89. pos = line.find(str, pos);
  90. wcout << line.substr(lastPos, pos - lastPos);
  91. if (pos != wstring::npos)
  92. {
  93. SetConsoleTextAttribute(hStdOut, FOREGROUND_GREEN);
  94. wcout << line.substr(pos, str.length());
  95. count++;
  96. }
  97. //else
  98. // SetConsoleTextAttribute(hStdOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
  99. SetConsoleTextAttribute(hStdOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
  100. }
  101. wcout << endl;
  102. }
  103. wcout << L"Найдено " << count << endl;
  104. }
  105.  
  106. void copy(vector<wstring>& t)
  107. {
  108. m1: size_t from = t.size() + 1;
  109. size_t to = from + 1;
  110. wcout << L"Из строки №";
  111. wcin >> from;
  112. wcout << L"В строку №";
  113. wcin >> to;
  114. from--;
  115. to--;
  116. if (from > t.size() - 1 || to > t.size())
  117. {
  118. wcout << L"Попробуйте снова\n";
  119. wcin.clear();
  120. wcin.ignore(numeric_limits<streamsize>::max(), '\n');
  121. goto m1;
  122. }
  123. t.insert(t.cbegin() + to, t[from]);
  124. printText(t);
  125. }
  126.  
  127. void del(vector<wstring>& t)
  128. {
  129. m2: size_t line = t.size() + 1;
  130. wcout << L"Из строки №";
  131. wcin >> line;
  132. size_t num = t[line].length() + 1;
  133. wcout << L"Количество символов: ";
  134. wcin >> num;
  135. line--;
  136. if (line > t.size() - 1 || num > t[line].length())
  137. {
  138. wcout << L"Попробуйте снова\n";
  139. wcin.clear();
  140. wcin.ignore(numeric_limits<streamsize>::max(), '\n');
  141. goto m2;
  142. }
  143. t[line].erase(t[line].length() - num, num);
  144. printText(t);
  145. }
  146.  
  147. void printText(vector<wstring>&t)
  148. {
  149. wcout << L"\nТекст:\n";
  150. for (wstring s : t)
  151. wcout << s << endl;
  152. wcout << endl;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement