C ++中的简单文本编辑程序

时间:2014-01-09 11:16:41

标签: c++ text editing

创建一个用C ++编辑文本的程序时遇到了一些麻烦。请记住,我仍处于编程的开始阶段。这是我必须做的:

有些文本放在文本文件F中。我必须编写一个程序编辑器,它基于文件F,来自键盘的命令和数据创建文件FF。程序编辑器必须识别并处理以下命令:

AN - 在第n行后插入文本;

IN - 在第n行之前插入文本;

CM,N - 从第m到第n行的行替换;

DM,N - 删除从第m到第n行的行;

E - 编辑结束;

其中m和n是文件F中的行数。命令记录在一行上,并作为菜单。

这是该计划。我在网上研究了很多关于文本编辑的内容,并且有一些文本编辑程序的源代码,但我想我还在编程的开头,我发现这些源代码真的很难理解。我很担心一些事情:

我必须手动将文字放入文本文件F中,并且必须在菜单中添加另一个关于添加文字的选项;

另一件事是关于命令 - 如何查找和使用文本中的不同行,以便我可以插入,替换和删除行;

那就是全部。如果你有时间请帮助我,因为我真的需要知道如何以一种不那么复杂的方式完成这个程序,我认为它有一些我可以从中学到的有价值的东西。提前谢谢!

1 个答案:

答案 0 :(得分:2)

伪代码中,您将找到documpentation中您需要的每个真实函数。:

你需要自己编写parse(),所有vec.something和input.something都是真正的向量或字符串函数,在不同的名称下,你需要搜索文档。

open,close和writeinfile也是不同名称下的io函数(和不同的参数),再次参见doc

getuserinput也是一个重命名的基本io函数。

我写这篇文章的原因是为了让你知道如何做到这一点,它是解决方案勺子给你,想想它作为算法,如果你能没有它做你的作业,它比使用它好得多。另外,学习搜索文档,这非常有用

vector<string> vec
int n, m
string input, output

//Open the file
open(your_file)
//Store every line in a string in the vector
while(input != EOF)
{
    input = getalinefrom(file)
    vec.add(input)
}

//You don t need the file for now, so close it
close(file)

//Create your 'menu', presuming text based, if graphical, well...
do
{
    //Get user choice
    input = getuserinput()

    //Every command is just a letter, so check it to know what to do
    if(input.firstchar == 'A')
    {
        //Parse the input to get n
        n = parse(input)
        //Get the line to add
        input = getuserinput()
        //Add it before n
        vec.addafter(n, input)
    }
    else if (input.firstchar == 'I')
    {
        //Parse the input to get n
        n = parse(input)
        //Get the line to add
        input = getuserinput()
        //Add it before n
        vec.addbefore(n, input)
    }
    else if (input.firstchar == 'C')
    {
        //Well, I don t see what is substitution so I ll let you try
    }
    else if (input.firstchar == 'D')
    {
        //Get n and m
        n = parse(input)
        m = parse(input)
        //Presuming n < m, you ll need to check for error
        while(n < m)
        {
            vec.deleterow(n)
            n = n + 1
        }
    }
//Go out of the loop at E since it s the end of the app
}while(input != "E");
//Concatene every line
n = 0
do
{
    output = output + vec.contentofrow(n)
}while(n < vec.length)
//Open the file again, with correct flag it will erase it content
open(file)
//Write your new content
writeinfile(file, output)
//Close the file
close(file)
return 0;