Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <string>
- #include <thread>
- #include <vector>
- void CaesarEncryptPart(std::string* s, size_t first, size_t step) {
- for (size_t i = first; i < first+step; i++) {
- char &c = (*s)[i];
- c = 'a' + (c + 3 - 'a') % 26;
- }
- }
- void CaesarEncrypt(std::string* s) {
- std::vector<thread> threads;
- if ((s->length() / 4 == 0) ||
- ((s->length() / 4 == 1) && (s->length() % 4 == 0))){
- for (size_t i = 0; i < s->length(); i++) {
- size_t step = 1;
- threads.emplace_back(std::thread(CaesarEncryptPart, s, i, step));
- }
- } else {
- size_t step = s->length() / 4;
- size_t residue = s->length() % 4;
- for (size_t i = 0; i < s->length(); i++){
- if (0 < residue--) {
- threads.emplace_back(std::thread(CaesarEncryptPart, s, i++, step+1));
- } else {
- threads.emplace_back(std::thread(CaesarEncryptPart, s, i, step));
- }
- i += step-1;
- }
- }
- std::cout << threads.size() << std::endl;
- for (size_t i = 0; i < threads.size(); i++) {
- threads[i].join();
- }
- std::cout << *s << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement