Advertisement
andruhovski

C++ string demo 02

Feb 6th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <fstream>
  3. #include <sstream>
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7. bool isLimit(char c) {
  8.     char lim[] = { ' ', '\t', '\n' };
  9.  
  10.     for (int i = 0; i < sizeof(lim); ++i)
  11.         if (c == lim[i]) return true;
  12.     return false;
  13. }
  14.  
  15. int main() {
  16.     ifstream fin("infile.txt");
  17.     if (!fin) { cout << "Ошибка открытия файла." << endl; return 1; }
  18.  
  19.     int count = 0;
  20.     string word;
  21.     ostringstream sentence;
  22.     while (!fin.eof()) {
  23.         char symb;
  24.         while (isLimit(symb = fin.peek())) {
  25.             sentence << symb;
  26.             if (symb == '\n') break;
  27.             fin.seekg(1, ios::cur);
  28.         }
  29.  
  30.         fin >> word;
  31.         sentence << word;
  32.         char last = word[word.size() - 1];
  33.         if ((last == '.') || (last == '!')) {
  34.             sentence.str(""); // очистка потока
  35.             continue;
  36.         }
  37.         if (last == '?') {
  38.             cout << sentence.str();
  39.             sentence.str("");
  40.             count++;
  41.         }
  42.     }
  43.     if (!count) cout << "Вопросительных предложений нет.";
  44.     cout << endl;
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement