_Mazur

C++/2017MPR

Mar 25th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <algorithm>
  2. #include <bitset>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <iterator>
  6. #include <string>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. unsigned long AsULong(const string& word) {
  12.     return stoul(word, nullptr, 2);
  13. }
  14.  
  15. bool IsTooLarge(const string& word) {
  16.  
  17.     if(word.size() <= 16) {
  18.         return false;
  19.     }
  20.  
  21.     return any_of(word.begin(), word.end()-16, [](char symbol) {
  22.         return symbol == '1';
  23.     });
  24.  
  25. }
  26.  
  27. bool IsDuocyclic(const string& word) {
  28.  
  29.     const auto middle = word.begin() + (word.size()/2);
  30.     return equal(word.begin(), middle, middle, word.end());
  31.  
  32. }
  33.  
  34. bool IsIncorrect(const string& word) {
  35.  
  36.     for(auto it = word.begin(); it != word.end(); it += 4) {
  37.  
  38.         if(AsULong(string{it, it+4}) > 9) {
  39.             return true;
  40.         }
  41.  
  42.     }
  43.  
  44.     return false;
  45.  
  46. }
  47.  
  48. class Program {
  49.  
  50. public:
  51.  
  52.     Program() {
  53.  
  54.         ifstream file("binarne.txt");
  55.         copy(istream_iterator<string>{file}, istream_iterator<string>{}, back_inserter(sequences));
  56.  
  57.     }
  58.  
  59.     void Run() {
  60.  
  61.         Task1();
  62.         Task2();
  63.         Task3();
  64.  
  65.     }
  66.  
  67. private:
  68.  
  69.     void Task1() {
  70.  
  71.         cout << "1)\n";
  72.         cout << "ilosc: " << count_if(sequences.begin(), sequences.end(), IsDuocyclic) << '\n';
  73.        
  74.         vector<string>::const_iterator theLongest = find_if(sequences.begin(), sequences.end(), IsDuocyclic);
  75.  
  76.         for(auto it = theLongest+1; it != sequences.end(); ++it) {
  77.  
  78.             if(IsDuocyclic(*it)) {
  79.                 theLongest = max(it, theLongest, [](auto left, auto right) {
  80.                     return left->size() < right->size();
  81.                 });
  82.             }
  83.  
  84.         }
  85.  
  86.         cout << "długosc najdluzszego: " << theLongest->size() << '\n';
  87.         cout << "najdluzszy: " << *theLongest << '\n';
  88.  
  89.     }
  90.  
  91.     void Task2() {
  92.  
  93.         cout << "2)\n";
  94.         cout << "ilosc: " << count_if(sequences.begin(), sequences.end(), IsIncorrect) << '\n';
  95.  
  96.         vector<string>::const_iterator theShortest = find_if(sequences.begin(), sequences.end(), IsIncorrect);
  97.  
  98.         for(auto it = theShortest+1; it != sequences.end(); ++it) {
  99.  
  100.             if(IsIncorrect(*it)) {
  101.                 theShortest = min(it, theShortest, [](auto left, auto right) {
  102.                     return left->size() < right->size();
  103.                 });
  104.             }
  105.  
  106.         }
  107.  
  108.         cout << "najmniejsza dlugosc: " << theShortest->size() << '\n';
  109.  
  110.     }
  111.  
  112.     void Task3() {
  113.  
  114.         unsigned long maximum = AsULong(sequences.front());
  115.  
  116.         for(auto it = sequences.begin()+1; it != sequences.end(); ++it) {
  117.  
  118.             if(!IsTooLarge(*it)) {
  119.                 maximum = max(AsULong(*it), maximum);
  120.             }
  121.  
  122.         }
  123.  
  124.         cout << "3)\n";
  125.         cout << "w systemie 10: " << maximum << '\n';
  126.         cout << "w systemie 2: " << bitset<16>{maximum} << '\n';
  127.  
  128.     }
  129.  
  130.     vector<string> sequences;
  131.  
  132. };
  133.  
  134. int main() {
  135.  
  136.     Program program;
  137.     program.Run();
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment