level 1
//文本编辑类
class LineEditor
{
private:
//文本编辑类的数据成员:
DblLinkList<String>textBuffer; //文本缓存
int curLineNo; //当前行号
DblLinkList<string>textUndoBuffer; //用于恢复的文本缓存
int curUndoLineNo; //用于恢复的当前行号
ifstream inFile; //输入文件
ofstream outFile; //输出文件
//辅助函数
StatusCode NextLine(); //转到下一行
StatusCode PreviousLine(); //转到前一行
StatusCode GotoLine(); //转到指定行
StatusCode InsertLine(); //插入一行
void ChangeLine(); //替换当前行或所有行的指定文本串
void ReadFile(); //读入文本文件
void WriteFile(); //写文本文件
2010年10月02日 13点10分
1
level 1
void FindString(); //查找串
publice:
//方法声明
LineEditor(char infName[],char outName[]); //构造函数
void Run(); //运行文本编译器
};
void LineEditor::ChangeLine()
//操作结果:用户输入指定文本串,在当前行或所有行中用输入的新文本串替换指定文本串
//替换成功返回SUCCESS,否则返回FALL
{
char answer; //用户回答字符
bool initialResponse=true; //初始回答
{ //循环直到用户输入恰当的回答为止
if(initialResponse)
{ //初始回答
cout<<"替换当前行c(urrent)或替换所有行a(ll):";
}
else
{ //非初始回答
cout<<"用c或a回答:";
}
answer=GetChar(); //从输入流跳过空格及制表符获取一字符
while(cin.get()!='\n'); //忽略用户输入的其他字符
answer=tolower(answer); //转换为小写字母
2010年10月02日 13点10分
2
level 1
initialResponse=false;
}while(answer!='c'&&answer!='a');
cout<<"输入要被替换的指定文本串:";
String strOld=Read(cin); //旧串
cout<<"输入新文本串:";
String strNew=Read(cin); //新串
for(int row=1;row<=textBuffer.Length();row++)
{
if(answer=='c'&&row!=curLineNo)
{ //只替换当前行,row不为当前行
continue; //进入下一趟循环
}
String strRow; //行
textBuffer.GetElem(row,strRow); //取出行
int index=Index(strRow,strOld); //在当前行中查找旧文本
if(index!=-1)
{ //模式匹配成功
2010年10月02日 13点10分
3
level 1
String newLine; //新行
Copy(newLine,strRow,index); //复制指定文本前的串
Concat(newLine,strNew); //连接新文本串
const char *oldLine=strRow.CStr();//旧行
Concat(newLine,(String)(oldLine+index+strlen(strOld.CStr())));
//连接指定文本串后的串,oldLine+index+strlen(strOld.CStr())用于计算一个临时指针,指向紧跟在被替换字符串后的字符,然后将C风格长转换为String,并连接到newline的后面
textBuffer.SetElem(row,newLine); //设置当前行新串
}
}
}
void LineEditor::FindString()
//操作结果:从当前行或从第1行开始查找指定文本
{
char answer; //用户回答字符
bool initialResponse=true; //初始回答
do
{ //循环直到用户输入恰当的回答为止
if(initialResponse)
{ //初始回答
cout<<"从第1行开始f(irst)或从当行开始c(urrent):";
}
2010年10月02日 13点10分
4
level 1
else
{ //非初始回答
cout<<"用f或c回答:";
}
answer=GetChar(); //从输入流跳过空格及制表符获取一字符
while(cin.get()!='\n'); //忽略用户输入的其他字符
answer=tolower(answer); //转换为小写字母
initialResponse=false;
}while(answer!='f'&&answer!='c');
if(answer=='f')curLineNo=1; //从第1行开始
int index;
cout<<"输入被查找的文本串:";
String searchString=Read(cin); //输入查找文本串
String curLine; //当前行
textBuffer.GetElem(curLineNo,curLine); //取出当前行
while((index=Index(curLine,searchString))==-1)
{ //查找指定文本串
if(curLineNo<textBuffer.Length())
{ //查找下一行
curLineNo++; //下一行
2010年10月02日 13点10分
5
level 1
textBuffer.GetElem(curLineNo,curLine); //取出下一行
}
else
{ //已查找完所有行
break;
}
}
if(index==-1)
{ //查找失败
cout<<"查找串失败";
}
else
{ //查找成功
cout<<curLine.CStr()<<endl;
for(int i=0;i<index;i++)
{ //在查找串前的位置显行空格
cout<<"";
}
for(int j=0;j<(int)strlen(searchString.CStr());j++)
{ //在查找串前的位置显行
cout<<"^";
}
cout<<endl;
}
void LineEditor::Run()
{
char userCommand; //用户命令
do
{
String tempString;
String curLine;
if(curLineNo!=0)
{
textBuffer.GetElem(curLineNo,curLine);
cout<<curLineNo<<":"
<<curLine.CStr()<<endl<<"?";
2010年10月02日 13点10分
6
level 1
}
else
{
cout<<"文件缓存空"<<endl<<"?";
}
userCommand=GetChar();
userCommand=tolower(userCommand);
while(cin.get()!='\n');
if(userCommand!='u'&&userCommand!='h'&&userCommand!='?'&&userCommand!='v')
{
textUndoBuffer=textBuffer;
curUndoLineNo=curLineNo;
}
switch(userCommand)
{
case 'b':
if(textBuffer.Empty())
{
cout<<"警告:文件缓存空"<<endl;
}
else
{
curLineNo=1;
}
break;
case 'c':
if(textBuffer.Empty())
{
cout<<"警告:文件缓存空"<<endl;
}
2010年10月02日 13点10分
7
level 1
else
{
ChangeLine();
}
break;
case 'd':
if(textBuffer.Delete(curLineNo,tempString)!=SUCCESS)
{
cout<<"错误:删除失败"<<endl;
}
break;
case 'e':
if(textBuffer.Empty())
{
cout<<"警告:文件缓存空"<<endl;
}
else
{
curLineNo=textBufer.Length();
}
break;
case 'f':
if(textBuffer.Empty())
{
cout<<"警告:文本缓存空"<<endl;
}
else
{
FindString();
2010年10月02日 13点10分
8
level 1
}
break;
case 'g':
if(GotoLine()!=SUCCESS)
{
cout<<"警告:没有那样的行"<<endl;
}
break;
case '?':
case 'h':
cout<<"有效命令:b(egin) c(hange) d(el) e(nd)"<<endl
<<"f(ind) g(o) h(elp) i(nsert) n(ext) p(rior)"<<endl
<<"q(uit) r(ead) u(ndo) v(iew) w(rite)"<<endl;
break;
case 'i':
if(InsertLine()!=SUCCESS)
cout<<"错误:插入行出错"<<endl;
break;
case 'n':
if(NextLine()!=SUCCESS)
{
cout<<"警告:当前行已是最后一行"<<endl;
}
break;
case 'p':
if(PreviousLine()!=SUCCESS)
{
cout<<"警告:当前行已是第一行"<<endl;
}
2010年10月02日 13点10分
9
level 1
break;
case 'q':
break;
case 'r':
ReadFile();
break;
case 'u':
Swap(textUndoBuffer,textBuffer);
Swap(curUndoLineNo,curLineNo);
break;
case 'v':
textBuffer.Traverse(Write);
break;
case 'w':
if(textBuffer.Empty())
{
cout<<"警告:文本缓存空"<<endl;
}
else
{
WriteFile();
}
break;
default:
cout<<"输入h或?获得帮助或输入有效命令字符:\n";
}
}
while(userCommand!='q');
}
2010年10月02日 13点10分
10
level 11
- 10
……我的第一章电脑桌面背景……那时候只会用鼠标一同乱点,好多回忆啊
2010年10月02日 14点10分
12