Advertisement
dxvmxnd

с++ 2 test

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