Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5. #include <vector>
  6.  
  7. void CaesarEncryptPart(std::string* s, size_t first, size_t step) {
  8.   for (size_t i = first; i < first+step; i++) {
  9.     char &c = (*s)[i];
  10.     c = 'a' + (c + 3 - 'a') % 26;
  11.   }
  12. }
  13.  
  14. void CaesarEncrypt(std::string* s) {
  15.   std::vector<thread> threads;
  16.   if ((s->length() / 4 == 0) ||
  17.     ((s->length() / 4 == 1) && (s->length() % 4 == 0))){
  18.       for (size_t i = 0; i < s->length(); i++) {
  19.         size_t step = 1;
  20.         threads.emplace_back(std::thread(CaesarEncryptPart, s, i, step));
  21.     }
  22.   } else {
  23.     size_t step = s->length() / 4;
  24.     size_t residue = s->length() % 4;
  25.  
  26.     for (size_t i = 0; i < s->length(); i++){
  27.       if (0 < residue--) {
  28.         threads.emplace_back(std::thread(CaesarEncryptPart, s, i++, step+1));
  29.       } else {
  30.         threads.emplace_back(std::thread(CaesarEncryptPart, s, i, step));
  31.       }
  32.       i += step-1;
  33.     }
  34.   }
  35.   std::cout << threads.size() << std::endl;
  36.   for (size_t i = 0; i < threads.size(); i++) {
  37.     threads[i].join();
  38.   }
  39.   std::cout << *s << std::endl;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement