Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- void outputAnswer(string answer, int numOfWord) {
- bool isNotCorrect;
- string path;
- cout << "Самое короткое слово: " << answer << endl;
- cout << "Оно начинается с позиции: " << numOfWord << endl;
- do {
- isNotCorrect = false;
- cout << "Введите путь файла для вывода: " << endl;
- cin >> path;
- ofstream fout(path, fstream::app);
- if ((fout.is_open()) and ((path[size(path) - 1] == 't') and (path[size(path) - 2] == 'x') and
- (path[size(path) - 3] == 't') and (path[size(path) - 4] == '.'))) {
- cout << "Файл успешно открыт!" << endl;
- fout << "Самое короткое слово: " << answer << endl;
- fout << "Оно начинается с позиции: " << numOfWord << endl;
- }
- else {
- cout << "Ошибка открытия файла" << endl;
- isNotCorrect = true;
- }
- } while (isNotCorrect);
- cout << "Ответ записан в файл." << endl;
- }
- string inputFromConsole() {
- string str;
- bool isNotCorrect, isCorrect;
- do {
- isNotCorrect = false;
- cout << "Введите предложение: " << endl;
- cin.ignore();
- getline(cin, str);
- isCorrect = false;
- for (int i = 0; i < size(str); i++) {
- if ((str[i] != ' ') and !isCorrect) {
- isCorrect = true;
- }
- }
- if ((size(str) < 1) or !isCorrect) {
- isNotCorrect = true;
- cout << "Текст должен содержать минимум 1 символ!" << endl;
- }
- } while (isNotCorrect);
- return str;
- }
- string checkForShortestWord(string str, int& numOfWord) {
- string shortestWord;
- string currentWord;
- bool isFirstWord = true;
- for (int i = 0; i < str.length(); ++i) {
- char ch = str[i];
- if (ch == ' ') {
- if (!(currentWord == "")) {
- if (isFirstWord || currentWord.length() < shortestWord.length()) {
- shortestWord = currentWord;
- numOfWord = i;
- isFirstWord = false;
- }
- currentWord = "";
- }
- } else {
- currentWord += ch;
- }
- }
- if (!(currentWord == "" )&& (isFirstWord || currentWord.length() < shortestWord.length())) {
- shortestWord = currentWord;
- }
- return shortestWord;
- }
- void outputTask() {
- setlocale(LC_ALL, "Rus");
- cout << "Данная программа находит в предложении самое короткое слово и выводит позицию его начала." <<
- endl;
- }
- string choicePath() {
- string path;
- bool isNotCorrect;
- do {
- isNotCorrect = false;
- cout << "Введите путь файла: " << endl;
- cin >> path;
- ifstream fin(path);
- if (fin.is_open() and ((path[size(path) - 1] == 't') and (path[size(path) - 2] == 'x') and
- (path[size(path) - 3] == 't') and (path[size(path) - 4] == '.'))) {
- cout << "Файл успешно открыт!" << endl;
- }
- else {
- cout << "Ошибка открытия файла!" << endl;
- isNotCorrect = true;
- }
- fin.close();
- } while (isNotCorrect);
- return path;
- }
- string inputStrFromFile(string path) {
- string str;
- string word;
- bool isCorrect;
- ifstream fin(path);
- str = "";
- cout << "Считывание строки..." << endl;
- while (fin >> word) {
- str = str + word + " ";
- }
- isCorrect = false;
- for (int i = 0; i < size(str); i++) {
- if ((str[i] != ' ') and !isCorrect) {
- isCorrect = true;
- }
- }
- if ((size(str) < 1) or !isCorrect) {
- cout << "Строка должна иметь хотя бы 1 символ! Введите данные с клавиатуры." <<
- endl;
- str = inputFromConsole();
- }
- cout << "Введенное предложение: " << str << endl;
- fin.close();
- return str;
- }
- string inputFromFile() {
- string path, str;
- path = choicePath();
- str = inputStrFromFile(path);
- return str;
- }
- string choiceOfInput() {
- string str;
- int choice;
- bool isNotCorrect;
- do {
- isNotCorrect = false;
- cout << "Выберите, откуда будут вводиться данные. Введите 0, если с консоли; 1, если с файла" << endl;
- cin >> choice;
- if (cin.fail() or ((choice != 0) and (choice != 1))) {
- cout << "Неверный ввод данных!" << endl;
- isNotCorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isNotCorrect);
- if (choice == 0) {
- str = inputFromConsole();
- }
- else {
- str = inputFromFile();
- }
- return str;
- }
- int main() {
- setlocale(LC_ALL, "Rus");
- system("chcp 1251");
- string answer;
- string str;
- int numOfWord;
- outputTask();
- str = choiceOfInput();
- answer = checkForShortestWord(str, numOfWord);
- outputAnswer(answer, numOfWord);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement