Advertisement
Guest User

Untitled

a guest
May 27th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     setlocale(LC_ALL, "Russian");
  10.  
  11.     vector<string> Sentences; //предложение, которые удовлетворяют требованиям
  12.  
  13.     string FileName;
  14.     cout << "Введите имя файла: ";
  15.     getline(cin, FileName);
  16.  
  17.     int WordsCount;
  18.     cout << "Кол-во слов: ";
  19.     cin >> WordsCount;
  20.  
  21.     ifstream TextFile(FileName);
  22.     if (!TextFile.is_open()) {
  23.         cout << "Невозможно открыть файл " << FileName;
  24.     } else {
  25.         char ch;
  26.         string content;
  27.         while (TextFile.get(ch)) {
  28.             content.push_back(ch); //записываем в строку содержимое файла
  29.         }
  30.         TextFile.close();
  31.  
  32.         //обрабатываем содержимое текстового файла
  33.         int LWordsCount = 1;
  34.         int start = 0;
  35.  
  36.         for (int a = start; a < content.length(); a++) {
  37.             if (content[a] == ' ') {
  38.                 LWordsCount++;
  39.             }
  40.            
  41.             if (content[a] == '.') {
  42.                 if (LWordsCount == WordsCount) {
  43.                     string SubSentence = "";
  44.                     for (int b = start; b <= a; b++) {
  45.                         SubSentence.push_back(content[b]);
  46.                     }
  47.                     Sentences.push_back(SubSentence);
  48.                 }
  49.                 start = a + 2;
  50.                 LWordsCount = 0;
  51.             }
  52.         }
  53.  
  54.         cout << endl << "Предложений: " << Sentences.size() << endl;
  55.         for (int a = 0; a < Sentences.size(); a++) {
  56.             cout << Sentences[a] << endl;
  57.         }      
  58.     }
  59.  
  60.     system("pause");
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement