【问题标题】:Simple text editing program in C++ [closed]C ++中的简单文本编辑程序[关闭]
【发布时间】:2014-01-09 11:16:41
【问题描述】:

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

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

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

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

CM,N - 将第 m 行替换为第 n 行;

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

E - 编辑结束;

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

这是程序。我在网上研究了很多关于文本编辑的内容,并且有一些文本编辑程序的源代码,但我想我还处于编程的初级阶段,我发现这些源代码真的很难理解。我担心几件事:

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

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

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

【问题讨论】:

  • 小贴士:阅读std::vectorstd::string。另请阅读C++ input/output library。对于一个非常简单的基于行的文本编辑器来说,这应该足够了。
  • 嗯,您的任务可能是微不足道的,这取决于您如何详细地完成任务。非图形文本写作可能很难处理。
  • 读取文件并将其放入字符串向量中,在字符串/行上,然后每一行现在都是向量中的索引,您只需修改向量即可编辑“文件” .最后,只需连接所有内容并将其写入文件。
  • 你知道如何打开文件并将行加载到向量中吗?如果是这样,你知道如何保存它吗?

标签: c++ text editing


【解决方案1】:

伪代码中,您会在文档中找到您需要的每一个真实函数。:

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

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

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多