文本编辑器数据结构 [英] Text editor data structures

查看:104
本文介绍了文本编辑器数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑很快就用C语言编写一个文本编辑器。

我这样做是为了更多地了解C.我想写点什么

像ed.c,一个简单的行编辑器。什么类型的数据结构将适合?b $ b?我正在考虑使用链表,但我也想知道一棵树是否有用。请给我你的

想法...谢谢,tilak

解决方案

SD写道:

我正在考虑很快就用C语言编写一个文本编辑器。
我这样做是为了更多地了解C.我想写一些东西,比如ed.c,a简单的线编辑器。什么类型的数据结构适合?我正在考虑使用链表,但我也想知道树是否有用。请给我你的想法...谢谢,tilak




在这个n.g. ,我们主要是C编程语言,按照标准规定




< OT>

话虽如此,如果您开始编写文本编辑器,请获取设计模式的副本 - Erich Gamma等。这应该可以帮到你一个

很多。

< / OT>




SD写道:

我正在考虑用C语言编写一个文本编辑器,以便在很快的时间内获得
。我这样做是为了更多地了解C.我想写一些像ed.c这样的简单行编辑器。
适合哪种类型的数据结构?我正在考虑使用链表,但我也想知道树是否有用。请给我你的想法...谢谢,tilak




询问使用什么数据结构,无论什么情况下确切的

你想用它是不太可能给你一个很好的答案。你可能需要花一点时间做一些设计并确定

你需要不同的数据结构来做什么类型的事情。你在某个时间内需要排序功能吗?您是否只需要通过值顺序查看

?在决定数据结构之前,需要回答许多其他问题。

。你的

问题的编写方式目前没有任何人知道你真正想要使用这些结构的内容。




" SD" < TI **** @ gmail.com>写了


我正在考虑在不久的将来用C语言编写一个文本编辑器。
我这样做是为了更多地了解C.我想写一些东西



通常情况下,测试编辑器会围绕链表进行测试。


typedef struct line

{

struct line * next;

struct line * prev;

char * text;

} LINE;


定义一个行任何测试块都以\ n结尾。或者文件结尾。


这种结构意味着插入,删除和块移动可以非常有效地实现。现代计算机实际上是如此之快以至于每次用户按下一个键时你可能会将整个文本文件向上移动一个字节,但是没有任何一点点投掷远离CPU周期。


您需要调用malloc()来获取新块,并分配

文本。您可能希望以100左右的单位分配行,以避免对realloc()进行
的连续调用。这将需要容量。

LINE结构的成员。


您可能希望LINE * start和LINE * tail成为全局变量,

方便,也是一个全局插入点。


主要问题是显示文本。你可以使用printf(),但是使用这样的

编辑器是非常尴尬的。真的,你需要把一个像

这样的库作为curses,它允许在屏幕上随意放置文本。


I am thinking about writing a text editor in C for unix sometime soon.
I am just doing this to learn more about C. I want to write something
like ed.c, a simple line editor. What types of data structures would be
appropriate? I am thinking about using a linked list, but I am also
wondering whether a tree would be useful. Please give me your
ideas...thanks, tilak

解决方案

SD wrote:

I am thinking about writing a text editor in C for unix sometime soon.
I am just doing this to learn more about C. I want to write something
like ed.c, a simple line editor. What types of data structures would be
appropriate? I am thinking about using a linked list, but I am also
wondering whether a tree would be useful. Please give me your
ideas...thanks, tilak



In this n.g. , we primarily the C programming language, as prescribed
by the standard.

<OT>
Having said that, if you are starting on writing a text editor, get a
copy of ''Design Patterns'' - Erich Gamma et al. That should help you a
lot.
</OT>



SD wrote:

I am thinking about writing a text editor in C for unix sometime soon. I am just doing this to learn more about C. I want to write something
like ed.c, a simple line editor. What types of data structures would be appropriate? I am thinking about using a linked list, but I am also
wondering whether a tree would be useful. Please give me your
ideas...thanks, tilak



Asking what data structure to use witout any context of what exactly
you want to use it for is not likely to get you a very good answer. You
probably need to spend a little time doing some design and determine
what types of things you need different data structures to do. Do you
need sorting capabilities in a given time? Will you only need to look
through values sequencially? Many other questions will need to be
answered before you can decide on a data structure. The way your
question is currently written no one here has any idea what you really
want to use the structures for.



"SD" <ti****@gmail.com> wrote


I am thinking about writing a text editor in C for unix sometime soon.
I am just doing this to learn more about C. I want to write something
like ed.c, a simple line editor. What types of data structures would be
appropriate? I am thinking about using a linked list, but I am also
wondering whether a tree would be useful. Please give me your
ideas...thanks, tilak


Normally a test editor is buitl around linked lists.

typedef struct line
{
struct line *next;
struct line *prev;
char *text;
} LINE;

define a "line" as any block of test terminated by an "\n" or end of file.

This structure means that insertions, deletions, and block moves can be
implemented quite efficiently. A modern computer is actually so fast that
you can probably move up an entire text file by one byte each time the user
presses a key, but there is no point throwing away CPU cycles.

You will need to call malloc() to grab new blocks, and also to allocate the
text. You might want to allocate lines in units of 100 or so to avoid
contuinal calls to realloc(). This would require a "capacity" member of the
LINE structure.

You probably want LINE *start and LINE *tail to be global variables, for
convenience, and also a global insertion point.

The main problem is displaying the text. You can use printf(), but such an
editor is extremely awkward to use. Really you need to look a a library such
as curses which allows to to place text at will on the screen.


这篇关于文本编辑器数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆