我在创建一个用 C++ 编辑文本的程序时遇到了一些麻烦。请记住,我仍处于编程的开始阶段。这是我必须做的:
一些文本放在文本文件 F 中。我必须编写一个程序编辑器,它基于文件 F、来自键盘的命令和数据创建文件 FF。程序编辑器必须识别并处理以下命令:
AN - 在第 n 行之后插入文本;
IN - 在第 n 行之前插入文本;
CM,N - 从第 m 行到第 n 行的替换;
DM,N——删除第m行到第n行;
E——编辑结束;
其中 m 和 n 是文件 F 中的行数。命令记录在一行中,并作为菜单。
这是程序。我在网上研究了很多关于文本编辑的内容,也有一些文本编辑程序的源代码,但我想我还处于编程的初级阶段,我发现那些源代码真的很难理解。我担心几件事:
我是否必须手动将文本放入文本文件 F 中,并且我是否必须在菜单中添加另一个关于添加文本的选项;
另一件事是关于命令——我如何从文本中查找和使用不同的行,以便我可以插入、替换和删除行;
嗯,就是这样。如果你有时间请帮助我,因为我真的需要知道这个程序必须如何以一种不那么复杂的方式完成,我认为它有一些我可以从中学到的有值(value)的东西。提前致谢!
在伪代码中,您会在文档中找到您需要的每个真实函数。:
你需要自己写parse(),所有的vec.something和input.something都是真正的 vector 或字符串函数,不同的名字,你需要搜索文档。
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;