ACM模拟题——VIM

 题目描述:本题目只有一组输入数据。第一行包含一个正整数L(L<=100),表示有一个包含L行文字的文本,接下来是L行文本每行不超过100个字符。在文本之后是若干个替换命令:[range]s/{pattern}/{string}/[flag];


  • :表示替换命令的开始;
  • [range]表示被操作的文本的范围;
  • s是substitute的简写,替换的意思;
  • {pattern}和{string}是要匹配的文本和要替换成的文本;
  • /用来作为分隔符;
  • [flag]是一些替换的规则。
  • 如果没有pattern,则使用上一次替换命令的pattern

  • 输入样例:

4

If the Tao is greet,then the operating system is greet.

If the operating system is greeter.then the compiler is greet.

If the compiler is greeter,then the applications is greet.

The user is pleased and there is harmony in the world

:1,3s/greet/great/g

:%s//great/g

      输出样例:

1  If the Tao is great,then the operating system is great.

2  If the operating system is greeter.then the compiler is great.

3  If the compiler is greeter,then the applications is great.

Pattern not found

  • 代码实现

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        //程序所需变量
        int lines=0;//lines行文本
        //pos_s s的位置,pos为','的位置,p1=pos_s+1,p_se2为第一个分隔符的位置,p_se3为第二个
        int pos_s=0,pos=0,p1=0,p_se2=0,p_se3=0;
        char separator; //分隔符
        string text[20]; //文本,最大行数为20
        string command;//命令
        string original,oldoriginal,replace;//original为要被替换文本,replace为替换文本
        int or_len=0,re_len=0;//oe_len为original字符串长度,re_len为replace字符串长度
        int pos_text=0;//每次寻找要替换文本的位置
        int start,end;//替换的开始和结束位置
        bool change=false;//标志位,判断是否有文本要被替换
    
    
        cout<<"请输入文本行数"<<endl;
        cin>>lines;
        cin.get();//保证getline函数能正常读取一行数据
        //输入文本
         for(int i=0;i<lines;i++)
            getline(cin,text[i]);
        //处理替换命令
        while(getline(cin,command))
        {
            //如果命令长度小于1,视为不再有命令输入,结束循环
            if(command.length() < 1)
               break;
            
            //每次标志位都要归为,以备处理下一次命令
            change = false;
    
            //数据预处理,为进行替换做准备
            pos_s = command.find('s');      
            separator = command[pos_s+1];   
            p1 = pos_s+1; 
            p_se2 = command.find(separator, p1+1);
            p_se3 = command.find(separator,p_se2+1);
    
            original = command.substr(p1+1,p_se2-p1-1);
            replace = command.substr(p_se2+1,p_se3-p_se2-1);
    
            or_len = original.length();
            re_len = replace.length();
    
           if(original == "")
                original = oldoriginal;
            else
                oldoriginal = original;
    
            //获取替换的开始和结束位置
            if(command[1] == '%')
            {
                start =0;
                end = lines-1;
            }
            else
            {
                for(int i=1;i<pos;i++)
                {
                    start = start*10 + (command[i]-'0');
                }
                for(int i=pos+1;i<pos_s;i++)
                {
                    end = end * 10 +(command[i]-'0');
                }
            }
            //进行替换
            for(int i=start;i<=end;i++)
            { 
                pos_text= text[i].find(original,0);
                while(pos_text!=-1)
                {
                    text[i].replace(pos_text,or_len,replace);
                    pos_text = text[i].find(original,pos_text+or_len);
                    change = 1;
                }
            }  
            //打印替换结果   
            if(change)       
                for(int i=0;i<lines;i++)
                   cout<<text[i]<<endl;
            else
                cout<<"Pattern not found"<<endl;
        }
    }
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值