Advertisement
dxvmxnd

c++_2_correct

Dec 19th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. void outputAnswer(string answer, int numOfWord) {
  8.  
  9. bool isNotCorrect;
  10. string path;
  11. cout << "Самое короткое слово: " << answer << endl;
  12. cout << "Оно начинается с позиции: " << numOfWord << endl;
  13.  
  14. do {
  15. isNotCorrect = false;
  16. cout << "Введите путь файла для вывода: " << endl;
  17. cin >> path;
  18. ofstream fout(path, fstream::app);
  19. if ((fout.is_open()) and ((path[size(path) - 1] == 't') and (path[size(path) - 2] == 'x') and
  20. (path[size(path) - 3] == 't') and (path[size(path) - 4] == '.'))) {
  21. cout << "Файл успешно открыт!" << endl;
  22. fout << "Самое короткое слово: " << answer << endl;
  23. fout << "Оно начинается с позиции: " << numOfWord << endl;
  24. }
  25. else {
  26. cout << "Ошибка открытия файла" << endl;
  27. isNotCorrect = true;
  28. }
  29. } while (isNotCorrect);
  30.  
  31. cout << "Ответ записан в файл." << endl;
  32.  
  33. }
  34.  
  35. string inputFromConsole() {
  36. string str;
  37. bool isNotCorrect, isCorrect;
  38.  
  39. do {
  40. isNotCorrect = false;
  41. cout << "Введите предложение: " << endl;
  42. cin.ignore();
  43. getline(cin, str);
  44.  
  45. isCorrect = false;
  46. for (int i = 0; i < size(str); i++) {
  47. if ((str[i] != ' ') and !isCorrect) {
  48. isCorrect = true;
  49. }
  50. }
  51. if ((size(str) < 1) or !isCorrect) {
  52. isNotCorrect = true;
  53. cout << "Текст должен содержать минимум 1 символ!" << endl;
  54. }
  55. } while (isNotCorrect);
  56.  
  57. return str;
  58. }
  59.  
  60.  
  61. string checkForShortestWord(string str, int& numOfWord) {
  62. string shortestWord;
  63. string currentWord;
  64. bool isFirstWord = true;
  65.  
  66. for (int i = 0; i < str.length(); ++i) {
  67. char ch = str[i];
  68. if (ch == ' ') {
  69. if (!(currentWord == "")) {
  70. if (isFirstWord || currentWord.length() < shortestWord.length()) {
  71. shortestWord = currentWord;
  72. numOfWord = i;
  73. isFirstWord = false;
  74. }
  75. currentWord = "";
  76. }
  77. } else {
  78. currentWord += ch;
  79. }
  80. }
  81.  
  82. if (!(currentWord == "" )&& (isFirstWord || currentWord.length() < shortestWord.length())) {
  83. shortestWord = currentWord;
  84. }
  85.  
  86. return shortestWord;
  87. }
  88.  
  89. void outputTask() {
  90. setlocale(LC_ALL, "Rus");
  91. cout << "Данная программа находит в предложении самое короткое слово и выводит позицию его начала." <<
  92. endl;
  93. }
  94.  
  95.  
  96. string choicePath() {
  97. string path;
  98. bool isNotCorrect;
  99.  
  100. do {
  101. isNotCorrect = false;
  102. cout << "Введите путь файла: " << endl;
  103. cin >> path;
  104. ifstream fin(path);
  105.  
  106. if (fin.is_open() and ((path[size(path) - 1] == 't') and (path[size(path) - 2] == 'x') and
  107. (path[size(path) - 3] == 't') and (path[size(path) - 4] == '.'))) {
  108. cout << "Файл успешно открыт!" << endl;
  109. }
  110. else {
  111. cout << "Ошибка открытия файла!" << endl;
  112. isNotCorrect = true;
  113. }
  114. fin.close();
  115. } while (isNotCorrect);
  116.  
  117. return path;
  118. }
  119.  
  120. string inputStrFromFile(string path) {
  121. string str;
  122. string word;
  123. bool isCorrect;
  124.  
  125. ifstream fin(path);
  126. str = "";
  127.  
  128.  
  129. cout << "Считывание строки..." << endl;
  130. while (fin >> word) {
  131. str = str + word + " ";
  132. }
  133.  
  134. isCorrect = false;
  135. for (int i = 0; i < size(str); i++) {
  136. if ((str[i] != ' ') and !isCorrect) {
  137. isCorrect = true;
  138. }
  139. }
  140. if ((size(str) < 1) or !isCorrect) {
  141. cout << "Строка должна иметь хотя бы 1 символ! Введите данные с клавиатуры." <<
  142. endl;
  143. str = inputFromConsole();
  144. }
  145.  
  146. cout << "Введенное предложение: " << str << endl;
  147. fin.close();
  148.  
  149. return str;
  150. }
  151.  
  152.  
  153. string inputFromFile() {
  154. string path, str;
  155.  
  156. path = choicePath();
  157.  
  158. str = inputStrFromFile(path);
  159.  
  160. return str;
  161. }
  162.  
  163.  
  164. string choiceOfInput() {
  165. string str;
  166. int choice;
  167. bool isNotCorrect;
  168.  
  169. do {
  170. isNotCorrect = false;
  171. cout << "Выберите, откуда будут вводиться данные. Введите 0, если с консоли; 1, если с файла" << endl;
  172. cin >> choice;
  173. if (cin.fail() or ((choice != 0) and (choice != 1))) {
  174. cout << "Неверный ввод данных!" << endl;
  175. isNotCorrect = true;
  176. cin.clear();
  177. while (cin.get() != '\n');
  178. }
  179. } while (isNotCorrect);
  180. if (choice == 0) {
  181. str = inputFromConsole();
  182. }
  183. else {
  184. str = inputFromFile();
  185. }
  186.  
  187. return str;
  188. }
  189.  
  190. int main() {
  191. setlocale(LC_ALL, "Rus");
  192. system("chcp 1251");
  193.  
  194. string answer;
  195. string str;
  196. int numOfWord;
  197.  
  198.  
  199. outputTask();
  200. str = choiceOfInput();
  201. answer = checkForShortestWord(str, numOfWord);
  202. outputAnswer(answer, numOfWord);
  203.  
  204.  
  205. }
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement