Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <list>
- #include <iostream>
- #include <iterator>
- class Editor {
- public:
- //Editor();
- // сдвинуть курсор влево
- void Left() {
- if (cursore_ != text_.begin() && !text_.empty()) {
- --cursore_;
- }
- }
- // сдвинуть курсор вправо
- void Right() {
- if (cursore_ != text_.end() && !text_.empty()) {
- ++cursore_;
- }
- }
- // вставить символ token
- void Insert(char token) {
- if (text_.empty()){
- text_.insert(cursore_, token);
- Right();
- }else {
- text_.insert(cursore_, token);
- }
- }
- // вырезать не более tokens символов, начиная с текущей позиции курсора
- void Cut(size_t tokens = 1) {
- buffer_.clear();
- auto copy_end = cursore_;
- auto copy_begin = cursore_;
- if (static_cast<size_t>(std::distance(cursore_, text_.end())) < tokens) {
- buffer_.splice(buffer_.begin(), text_, cursore_, text_.end());
- cursore_ = text_.end();
- } else {
- std::advance(copy_end, tokens);
- std::advance(cursore_, tokens);
- buffer_.splice(buffer_.begin(), text_, copy_begin, copy_end);
- }
- }
- // cкопировать не более tokens символов, начиная с текущей позиции курсора
- void Copy(size_t tokens = 1) {
- buffer_.clear();
- auto copy_end = cursore_;
- if (static_cast<size_t>(std::distance(cursore_, text_.end())) < tokens) {
- buffer_.insert(buffer_.begin(), cursore_, text_.end());
- }
- else {
- std::advance(copy_end, tokens);
- buffer_.insert(buffer_.begin(), cursore_, copy_end);
- }
- }
- // вставить содержимое буфера в текущую позицию курсора
- void Paste() {
- text_.insert(cursore_, buffer_.begin(), buffer_.end());
- }
- // получить текущее содержимое текстового редактора
- std::string GetText() const {
- std::string result = "";
- for (const char c : text_) {
- result += c;
- }
- return result;
- }
- private:
- std::list<char> text_ = {};
- std::list<char> buffer_ = {};
- std::list<char>::iterator cursore_ = text_.begin();
- };
- int main() {
- Editor e;
- e.Insert('1');
- e.Left();
- e.Insert('2');
- e.Left();
- e.Insert('3');
- std::cout << e.GetText();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement