Advertisement
_Mazur

C++/2010MPR

Mar 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <algorithm>
  2. #include <array>
  3. #include <fstream>
  4. #include <functional>
  5. #include <iostream>
  6. #include <iterator>
  7. #include <string>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. class LineOfWords {
  13.  
  14. public:
  15.  
  16.     bool HasWordsOfSameLength() const {
  17.         return count_if(words.begin()+1, words.end(), [size = words.front().size()] (const string& word) {
  18.             return word.size() == size;
  19.         }) == words.size()-1;
  20.     }
  21.  
  22.     bool AreAnagrams() const {
  23.         return count_if(words.begin()+1, words.end(), [firstWord = words.front()] (const string& word){
  24.             return is_permutation(firstWord.begin(), firstWord.end(), word.begin(), word.end());
  25.         }) == words.size()-1;
  26.     }
  27.  
  28. private:
  29.  
  30.     array<string, 5> words;
  31.  
  32.     friend istream& operator>>(istream& stream, LineOfWords& line) {
  33.  
  34.         copy_n(istream_iterator<string>{stream}, line.words.size(), line.words.begin());
  35.         return stream;
  36.  
  37.     }
  38.  
  39.     friend ostream& operator<<(ostream& stream, const LineOfWords& line) {
  40.  
  41.         copy_n(line.words.begin(), line.words.size(), ostream_iterator<string>{stream, " "});
  42.         return stream;
  43.  
  44.     }
  45.  
  46. };
  47.  
  48. class Program {
  49.  
  50. public:
  51.  
  52.     Program() {
  53.  
  54.         ifstream file("anagram.txt");
  55.         copy(istream_iterator<LineOfWords>{file}, istream_iterator<LineOfWords>{}, back_inserter(lines));
  56.  
  57.     }
  58.  
  59.     void Run() {
  60.  
  61.         Task1();
  62.         Task2();
  63.  
  64.     }
  65.  
  66. private:
  67.  
  68.     void Task1() {
  69.  
  70.         cout << "1)\n";
  71.         copy_if(lines.begin(), lines.end(), ostream_iterator<LineOfWords>{cout, "\n"},
  72.                 mem_fn(&LineOfWords::HasWordsOfSameLength));
  73.  
  74.  
  75.     }
  76.  
  77.     void Task2() {
  78.  
  79.         cout << "2)\n";
  80.         copy_if(lines.begin(), lines.end(), ostream_iterator<LineOfWords>{cout, "\n"},
  81.                 mem_fn(&LineOfWords::AreAnagrams));
  82.  
  83.     }
  84.  
  85.     vector<LineOfWords> lines;
  86.  
  87. };
  88.  
  89. int main() {
  90.    
  91.     Program program;
  92.     program.Run();
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement