需要帮助编写文本编辑器程序的撤消/重做功能 [英] Need Help writing an undo/redo function for text editor program
问题描述
我在C ++中编写了一个文本编辑器程序,它有简单的命令:LEFT,RIGHT,HOME,END,BACKSPACE,DELETE,INSERT,现在我需要执行UNDO和REDO功能。在我的程序中,用户必须能够撤消不超过最后十个命令。我想使用一个向量实现来完成这一点,但我不知道如何设置。我不知道如何将光标位置和字符存储到向量中。有人可以提供一些帮助吗?
I have written a text editor program in C++ that has simple commands: LEFT, RIGHT, HOME, END, BACKSPACE, DELETE, INSERT, and now I need to perform the UNDO and REDO functions. In my program the user must be able to undo no more than the last ten commands. I would like to use a vector implementation to accomplish this, but I have no idea how to set this up. I'm not sure how to store the cursor position and the character into a vector. Can someone offer some help?
#ifndef CURSOR_H
#define CURSOR_H
#include <stdlib.h>
#include <iostream>
template <class Object>
class Cursor;
// Incomplete Declaration
template <class Object>
class CNode
{
public:
CNode( const Object & theElement = Object( ), CNode * n = NULL ) : element( theElement ), next( n ) { }
Object element;
CNode *next;
friend class Cursor<Object>;
};
template <class Object>
class Cursor
{
public:
Cursor( );
bool isEmpty( ) const;
void makeEmpty( );
void left ( );
void right ( );
void del ( ); //This is the delete operation. I named it del instead of delete as delete conflicts with a C++ keyword.
void back ( );
void insert( const Object & x );
void home ( );
void end ( );
void undo ( );
private:
void printText ( ) ;
CNode<Object> *header;
CNode<Object> *cursorPosition;
};
//#include "Cursor.cpp"
#endif
推荐答案
您要使用 deque
,以便可以从前面或后面添加和删除;当添加命令时,将其添加到后面,当撤消从后面删除它,当你达到11命令从前面删除一个。
You want to use a deque
so that you can add and remove from the front or the back; when adding a command, add it to the back, when undoing remove it from the back, and when you reach 11 commands remove one from the front.
这篇关于需要帮助编写文本编辑器程序的撤消/重做功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!