简单编辑器

简单编辑器

Description:

要开发一个字符界面的编辑器,对文本的编辑操作是通过输入一个一个的“行编辑命令”完成的。

系统约定: 行编辑命令由命令关键字 命令参数组成。其中,命令关键字都是单一的字母,命令与参数之间使用一个空格进行分割,当命令参数为多个时采用 / 作为参数的分隔符,如果在命令的参数中间出现符号 / ,则用 \/ 表示。我们将 / 定义为转义符。

行插入

1
i #/串

在指定行号(> 0)插入一个新行,新行的内容就是命令中给出的串。若插入的行超过了已有的行数,则在最后一行的后面插入一个新行。

文本替换

1
c 被替换串/替换串

将整个文本中全部被替换串替换为替换串

行删除

1
d #

其中#为指定的行号(不含#)

行合并:

1
f 1/2/3...

将行号为 #2 和 #3… 的行连接到行号为 #1 的行的后面,取消原来的 #2 行和 #3 等,合并后 #2 行和 #3 等原来的内容不存在了,被后面的行抵补替代。若指定行号超出正文的范围,则放弃操作(不含#)。

查找串

1
q #x/串

查询行号为#x是否出现串,如果出现请输出Yes,否则输出No,保证行号合法,且查询操作在最后出现(含有#) 。

Input & Output

输入首先是用[Text]……[/Text]的形式描述的编辑器初始内容,接下来会有若干插入、修改、删除查询操作。请根据描述处理所有操作,并输出最终编辑器内容。然后对于每个查询操作,请按时间轴顺序输出查询结果,注意格式和换行。

输入:

1
2
3
4
5
6
7
8
9
[Text]
ab cdefghij klmn20abcedf
ab
iloveyou
a a bb ccd e
[/Text]
c ab/12345
f 1/2
q #1/def

输出:

1
2
3
4
12345 cdefghij klmn2012345cedf12345
iloveyou
a a bb ccd e
Yes

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<cstdio>
#include<string>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
typedef struct NODE
{
string line;
struct NODE *next;
} node;

node *head;
node *p,*q,*HEAD,*x;

int replace_all(string& str,const string& old_value, const string& new_value)
{ for(string::size_type pos(0);pos!=string::npos; pos+=new_value.length())
{
if((pos=str.find(old_value,pos))!=string::npos )
str.replace(pos,old_value.length(),new_value);
else break;
}
return 0;
}

int main()
{ string a,str;



char op;
int n=0,num,uu=0,fin[10000],JIAA;;
getline(cin,a);
//cout<<a<<endl;
p=new NODE;
//p=(node)malloc(sizeof(node));
p->next=NULL;
HEAD=p;
head=p;
while(1)
{
getline(cin,a);
//cout<<a<<endl;
if(a==”[/Text]”) break;
//p=(node
)malloc(sizeof(node));
p=new NODE;
p->line=a;
head->next=p;
p->next=NULL;
head=p;
n++;
}
//while(1){
while(~scanf(“%c”,&op)) {

if(op==’i’) //行插入
{ n++;
getchar();
cin>>num;//cout<<num<<endl;
getchar();
getline(cin,str);//cout<<str<<endl;
for (int i = 1; i < str.length(); i++) //去掉’ / ‘字符
{
if (str[i]==’/‘&&str[i-1]==’\‘)
str.erase(i-1,1);
}

    p=new NODE;
    p->line=str;

    if(num>=n) 
        {//cout<<1<<endl;
            head->next=p; 
            p->next=NULL;

        }
    else {//cout<<2<<endl;
            q=HEAD;
            for(int i=1;i<=num;i++) 
                {    
                    if(i!=num)
                        {    q=q->next;

                        }    
                    else{
                        p->next=q->next;
                        q->next=p;

                    }

                }
        }
}

if(op==’d’)
{

    getchar();
    cin>>num;
    if(num<=n) 
    { 
    q=HEAD;
    if(num==n)
        {
        for(int i=1;i<=num;i++) 
                {    
                    if(i!=num)
                        q=q->next;                                                                        
                    else
                        q->next=NULL;                                                                                                                        
                } 
        }     

    else for(int i=1;i<=num;i++) 
                {    
                    if(i!=num)
                        q=q->next;                                                                        
                    else
                        q->next=(q->next)->next;                                                                                                                        
                } } 
    n--;
}

if(op==’f’)
{ int jia[20000],jiaa=0,k=0,jj=0; char ch;
getchar();//空格
cin>>num;
getchar();
while(1)
{ cin>>jia[jiaa];

           ch=getchar();
           if(jia[jiaa]>n) 
           {
               if(ch=='\n') break; 
               if(ch=='/') continue;
           }
           jiaa++;    
           if(ch=='\n') {                 
           break; } 
           //if(ch=='/') continue;                                                     
       }
   JIAA=jiaa;
   if(num<=n){             

   q=HEAD;             
   for(int i=1;i<=num;i++) 
       {
           q=q->next;//第 i 个                     
       }

   while(jiaa--)
       {    x=HEAD;            
           for(int i=1;i<=jia[k];i++)     //选定被合并的 
               x=x->next;
           a=x->line;   //合并 
           q->line+=a;    
           k++;
       }    
    k=0;
    sort(jia,jia+JIAA);
   while(JIAA--) 
       {  
           x=HEAD;        
           for(int i=1;i<=jia[k]-jj;i++) //删除 合并完的 
               {    
                   if(i!=jia[k]-jj)
                       x=x->next;                                                                        
                   else
                       x->next=(x->next)->next;                                                                                                                        
               } 
           n--;
           jj++;//每次删除行后  行的号码都会 -1 ; 
           k++;        
       }                                         
}
} 

if(op==’c’)
{
int ss=0;
char betihuan[10000],tihuan[10000];
string one,two;
getchar();
while(1)
{
scanf(“%c”,&betihuan[ss]);
if(betihuan[ss]==’/‘&&ss>0&&betihuan[ss-1]!=’\‘) { ss=ss-1; break; }
if(betihuan[ss]==’/‘&&ss>0&&betihuan[ss-1]==’\‘)
{
ss-=1;
betihuan[ss]=’/‘;
}
ss++;
}
for(int i=0;i<=ss;i++) // 转换格式
one += betihuan[i]; //cout<<one<<endl;
ss=0;
while(1)
{
scanf(“%c”,&tihuan[ss]);
if(tihuan[ss]==’\n’) { ss=ss-1; break; }
if(tihuan[ss]==’/‘&&ss>0&&tihuan[ss-1]==’\‘)
{
ss-=1;
tihuan[ss]=’/‘;
}
ss++;
}
for(int i=0;i<=ss;i++) // 转换格式
two += tihuan[i]; //cout<<two<<endl;
q=HEAD;
for(int i=1;i<=n;i++)
{
q=q->next;
replace_all(q->line,one,two);
}
}
if(op==’q’) //查询
{
getchar();
getchar();
cin>>num;
getchar();
getline(cin,str);
for (int i = 1; i < str.length(); i++) //去掉’ / ‘字符
{
if (str[i]==’/‘&&str[i-1]==’\‘)
str.erase(i-1,1);
}
q=HEAD;
for(int i=1;i<=num;i++)
{
q=q->next;//第 i 个
}
a=q->line;
string::size_type idx = a.find(str); //判断字符串是否包含子字符串
if (idx == string::npos)
fin[uu]=0;// cout<<”NO”<<”\n”;
else
fin[uu]=1; //cout<<”YES”<<”\n”;
uu++;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

}
q=HEAD->next;
//cout<< q->line<<endl;
for(int i=1;i<=n;i++)
{
cout<<q->line<<endl;
q=q->next;
}
for(int i=0;i<uu;i++)
{
if(fin[i]==1) cout<<"Yes"<<"\n";
else cout<<"No"<<"\n";
}
}