回 帖 发 新 帖 刷新版面

主题:关于文本(textbuffer)的C语言实现问题

#ifndef TEXTBUFFER_H
#define TEXTBUFFER_H

typedef struct textbuffer *TB;

/* Allocate a new textbuffer whose contents is initialised with the text given
 * in the array.
 */

TB newTB (char text[]);

/* Free the memory occupied by the given textbuffer.  It is an error to access
 * the buffer afterwards.
 */
void releaseTB (TB tb);

/* Allocate and return an array containing the text in the given textbuffer.
 */
char *dumpTB (TB tb);

/* Return the number of lines of the given textbuffer.
 */
int linesTB (TB tb);

/* Swap the two given lines in the textbuffer.
 *
 * - The program is to abort() with an error message if line 'pos1' or line
 *   'pos2' is out of range.  The first line of a textbuffer is at position 0.
 */
void swapTB (TB tb, int pos1, int pos2);

/* Merge 'tb2' into 'tb1' at line 'pos'.
 * 
 * - Afterwards line 0 of 'tb2' will be line 'pos' of 'tb1'.
 * - The old line 'pos' of 'tb1' will follow after the last line of 'tb2'.
 * - After this operation 'tb2' can not be used anymore (as if we had used
 *   releaseTB() on it).
 * - The program is to abort() with an error message if 'pos' is out of range.
 */
void mergeTB (TB tb1, int pos, TB tb2);

/* Copy 'tb2' into 'tb1' at line 'pos'.
 * 
 * - Afterwards line 0 of 'tb2' will be line 'pos' of 'tb1'.
 * - The old line 'pos' of 'tb1' will follow after the last line of 'tb2'.
 * - After this operation 'tb2' is unmodified and remains usable independent
 *   of 'tb1'. 
 * - The program is to abort() with an error message if 'pos' is out of range.
 */
void pasteTB (TB tb1, int pos, TB tb2);

/* Cut the lines between and including 'from' and 'to' out of the textbuffer
 * 'tb'.
 *
 * - The result is a new textbuffer (much as one created with newTB()).
 * - The cut lines will be deleted from 'tb'.
 * - The program is to abort() with an error message if 'from' or 'to' is out
 *   of range. 
 */
TB cutTB (TB tb, int from, int to);

/* Copy the lines between and including 'from' and 'to' of the textbuffer
 * 'tb'.
 *
 * - The result is a new textbuffer (much as one created with newTB()).
 * - The textbuffer 'tb' will remain unmodified.
 * - The program is to abort() with an error message if 'from' or 'to' is out
 *   of range. 
 */
TB copyTB (TB tb, int from, int to);

/* Remove the lines between and including 'from' and 'to' from the textbuffer
 * 'tb'.
 *
 * - The program is to abort() with an error message if 'from' or 'to' is out
 *   of range. 
 */
void deleteTB (TB tb, int from, int to);


#endif
以上是我摘自国外网站的题目,但是看不太懂。。还请教高手来看看

回复列表 (共14个回复)

沙发

各位高手看看怎么实现

TB newTB (char text[]);

/* Free the memory occupied by the given textbuffer.  It is an error to access
 * the buffer afterwards.
 */
这个函数?

板凳

你看错了
/* Allocate a new textbuffer whose contents is initialised with the text given
 * in the array.
 */

TB newTB (char text[]);

3 楼

那怎么实现呢?
typedef struct textbuffer *TB;
struct textbuffer{
    TB next;
    char text[];
};
建立链表,链表的每个结点储存一行字
TB newTB (char text[])

  TB tb=malloc(sizeof *tb);
   tb->text[]=text[];
   tb->next=next;
   return tb

我是这样写的

4 楼

其实就是链表的各种基本操作,建立,插入,删除释放,交换,复制等等,你可以去看下书,或者上网搜一下.
#define LINE_NUM 20
struct textbuffer{
    TB next;
    char text[LINE_NUM];
};
typedef struct textbuffer* TB;
TB newTB (char text[])
{
   TB tb = (TB)malloc(sizeof(tb));
   strcpy(tb->text,text);
   tb->next=next;
   return tb
}
还有你的语言学的不扎实,语法有错误,强烈建议你先把C的基本语法好好练一遍

5 楼


/* Allocate and return an array containing the text in the given textbuffer.
 */
char *dumpTB (TB tb);
如果用strcpy的话,这个函数不是启到同样的作用么?

6 楼

char *dump(TB tb)这个就是要你遍历链表,动态开辟个数组保存链表中所有节点的内容,中间大概要用到strcpy,strcat.如果你用c++的话会简单点,string重载了各种运算符,直接=,+就可以了

7 楼

可惜不能用C++啊

8 楼

[quote]char *dump(TB tb)这个就是要你遍历链表,动态开辟个数组保存链表中所有节点的内容,中间大概要用到strcpy,strcat.如果你用c++的话会简单点,string重载了各种运算符,直接=,+就可以了
[/quote]

不是很理解动态开辟数组保存链表中所有节点的内容,这个东西

还求给个算法指教一下

9 楼

char *dump(TB tb)
{
#define MAX_NUM 10000
    char *p = ( char* )malloc( MAX_NUM * sizeof(char) )
    memset(p,0,sizeof(p));
    char *tmp = tb; //tb应为头结点
    while(tmp!=NULL)
    {
        strcat(p,tmp.text);
        tmp = tmp->next;
    }
    return p;
}

10 楼

/* Free the memory occupied by the given textbuffer.  It is an error to access
 * the buffer afterwards.
 */
void releaseTB (TB tb);

这个是不是直接用free来释放?
void releaseTB(TB tb)
{
 free(tb);
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册