Advertisement
Petrovi4

Editor

Jul 27th, 2022 (edited)
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <string>
  2. #include <list>
  3. #include <iostream>
  4. #include <iterator>
  5.  
  6. class Editor {
  7. public:
  8.     //Editor();
  9.  
  10.     // сдвинуть курсор влево
  11.     void Left() {
  12.         if (cursore_ != text_.begin() && !text_.empty()) {
  13.             --cursore_;
  14.         }
  15.     }
  16.     // сдвинуть курсор вправо
  17.     void Right() {
  18.         if (cursore_ != text_.end() && !text_.empty()) {
  19.             ++cursore_;
  20.         }
  21.     }
  22.     // вставить символ token
  23.     void Insert(char token) {
  24.         if (text_.empty()){
  25.             text_.insert(cursore_, token);
  26.             Right();
  27.         }else {
  28.             text_.insert(cursore_, token);
  29.         }
  30.     }
  31.  
  32.     // вырезать не более tokens символов, начиная с текущей позиции курсора
  33.     void Cut(size_t tokens = 1) {
  34.         buffer_.clear();
  35.         auto copy_end = cursore_;
  36.         auto copy_begin = cursore_;
  37.         if (static_cast<size_t>(std::distance(cursore_, text_.end())) < tokens) {
  38.             buffer_.splice(buffer_.begin(), text_, cursore_, text_.end());
  39.             cursore_ = text_.end();
  40.         } else {
  41.             std::advance(copy_end, tokens);
  42.             std::advance(cursore_, tokens);
  43.             buffer_.splice(buffer_.begin(), text_, copy_begin, copy_end);
  44.         }
  45.     }
  46.     // cкопировать не более tokens символов, начиная с текущей позиции курсора
  47.     void Copy(size_t tokens = 1) {
  48.         buffer_.clear();
  49.         auto copy_end = cursore_;        
  50.         if (static_cast<size_t>(std::distance(cursore_, text_.end())) < tokens) {
  51.             buffer_.insert(buffer_.begin(), cursore_, text_.end());
  52.         }
  53.         else {
  54.             std::advance(copy_end, tokens);            
  55.             buffer_.insert(buffer_.begin(), cursore_, copy_end);
  56.         }
  57.     }
  58.     // вставить содержимое буфера в текущую позицию курсора
  59.     void Paste() {
  60.         text_.insert(cursore_, buffer_.begin(), buffer_.end());
  61.     }
  62.     // получить текущее содержимое текстового редактора
  63.     std::string GetText() const {
  64.         std::string result = "";
  65.         for (const char c : text_) {
  66.             result += c;
  67.         }
  68.         return result;
  69.     }
  70. private:
  71.     std::list<char> text_ = {};
  72.     std::list<char> buffer_ = {};
  73.     std::list<char>::iterator cursore_ = text_.begin();
  74.  
  75. };
  76.  
  77. int main() {
  78.     Editor e;
  79.    
  80.     e.Insert('1');
  81.     e.Left();
  82.     e.Insert('2');
  83.     e.Left();
  84.     e.Insert('3');
  85.     std::cout << e.GetText();
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement