zoukankan      html  css  js  c++  java
  • c++ stl8选6中的字符串操作题

    3342:字符串操作

    总时间限制: 
    1000ms
     
    内存限制: 
    65536kB
    描述

    给定n个字符串(从1开始编号),每个字符串中的字符位置从0开始编号,长度为1-500,现有如下若干操作:

    • copy N X L:取出第N个字符串第X个字符开始的长度为L的字符串。
    • add S1 S2:判断S1,S2是否为0-99999之间的整数,若是则将其转化为整数做加法,若不是,则作字符串加法,返回的值为一字符串。
    • find S N:在第N个字符串中从左开始找寻S字符串,返回其第一次出现的位置,若没有找到,返回字符串的长度。
    • rfind S N:在第N个字符串中从右开始找寻S字符串,返回其第一次出现的位置,若没有找到,返回字符串的长度。
    • insert S N X:在第N个字符串的第X个字符位置中插入S字符串。
    • reset S N:将第N个字符串变为S。
    • print N:打印输出第N个字符串。
    • printall:打印输出所有字符串。
    • over:结束操作。

    其中N,X,L可由find与rfind操作表达式构成,S,S1,S2可由copy与add操作表达式构成。

    输入

    第一行为一个整数n(n在1-20之间)


    接下来n行为n个字符串,字符串不包含空格及操作命令等。


    接下来若干行为一系列操作,直到over结束。

    输出

    根据操作提示输出对应字符串。

    样例输入
    3
    329strjvc
    Opadfk48
    Ifjoqwoqejr
    insert copy 1 find 2 1 2 2 2
    print 2
    reset add copy 1 find 3 1 3 copy 2 find 2 2 2 3
    print 3
    insert a 3 2
    printall
    over
    样例输出
    Op29adfk48
    358
    329strjvc
    Op29adfk48
    35a8
    提示

        推荐使用string类中的相关操作函数。

    这题一开始想了很久,主要是搞不懂怎么一会返回数字一会返回字符串,后来想到干脆无论什么时候都返回字符串,

    只有在需要输入整数时再判断是否是整数and是否满足在0-99999之间。

    恶补了一下将整数和字符串来回转换的方法,

    将字符串转为整数只需要 atoi(str.c_str()) ,简单轻松

    但是!!在转换之前应该先检验一波是否是数字。

    方法是用sstream

    bool isNum(string str)
    {
        stringstream sin(str);
        double d;
        char c;
        if(!(sin >> d))
        {
            return false;
        }
        if (sin >> c)
        {
            return false;
        }
        return true;
    }
    

     很稳的检验方法,前导零,科学计数法都能识别得出来。

    将整数转为字符串可以用to_string() 不需要另外包含头文件,轻松愉快~

    所以整体的思路:

    不断detect()读入字符串,在需要数字的地方用num()转化

    其中在实现find和rfind时踩了一个大坑,

    string中的find函数与rfind函数定义如下:

    int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置

    int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
    int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
    int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
    //查找成功时返回所在位置,失败返回string::npos的值 
    int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
    int rfind(const char *s, int pos = npos) const;
    int rfind(const char *s, int pos, int n = npos) const;

    int rfind(const string &s,int pos = npos) const;

    在实际的程序实现中,rfind的查找截止值并不是pos,而是pos+strlen(c)-1。

    意思就是rfind的第二个参数要写成length()-1才会从字符串末尾开始查找

    rfind与find找不到的时候都会返回string::npos,故可以通过返回值知道是否成功查找。

    最后上代码

      1 // All rights reserved.
      2 
      3 #include <iostream>
      4 #include <string>
      5 #include <sstream>
      6 using namespace std;
      7 string str[20];
      8 int t;
      9 string cmd;
     10 string detect();
     11 int num(int p=0);
     12 
     13 void print_all(string* str, int t){
     14     for(int i=0;i<t;i++){
     15         cout<<str[i]<<endl;
     16     }
     17 }
     18 bool isNum(string str)
     19 {
     20     stringstream sin(str);
     21     double d;
     22     char c;
     23     if(!(sin >> d)){return false;}
     24     if (sin >> c){return false;}
     25     return true;
     26 }
     27 int num(string p){
     28         return atoi(p.c_str());
     29 }
     30 
     31 string detect(){
     32     cin>>cmd;
     33     //cout<<cmd;
     34     if(cmd=="over")
     35         return "";
     36     else if(cmd=="add"){
     37         string a;
     38         string b;
     39         
     40         a=detect();
     41         b=detect();
     42         if(isNum(a)&&isNum(b)&&num(a)>=0&&num(a)<=99999&&num(b)>=0&&num(b)<=99999)
     43             return to_string(num(a)+num(b));
     44         else
     45             return a+b;
     46     }
     47     else if(cmd=="copy"){
     48         int xu,pos,len;
     49         xu=num(detect());
     50         pos=num(detect());
     51         len=num(detect());
     52         
     53         return str[xu-1].substr(pos,len);
     54     }
     55     
     56     else if(cmd=="find"){
     57         string search;
     58         int xu;
     59     
     60         search=detect();
     61         xu=num(detect());
     62         if(str[xu-1].find(search,0)!=string::npos)
     63             return to_string(str[xu-1].find(search,0));
     64         else
     65             return to_string((int)search.length());
     66     }
     67     else if(cmd=="rfind"){
     68         string search;
     69         int xu;
     70         
     71         search=detect();
     72         xu=num(detect());
     73         if(str[xu-1].rfind(search,str[xu-1].length()-1)!=string::npos) //rfind has a return value of npos
     74             return to_string(str[xu-1].rfind(search,str[xu-1].length()-1));
     75         else
     76             return to_string((int)search.length());
     77     }
     78         
     79     else if(cmd=="print"){          //以下是死操作(没有返回值
     80         int xu;
     81         
     82         xu=num(detect());
     83         //cout<<xu<<endl;
     84         cout<<str[xu-1]<<endl;
     85     }
     86     else if(cmd=="printall")
     87         print_all(str,t);
     88     
     89     else if(cmd=="insert"){
     90         string inserterr;
     91         int xu;
     92         int pos;
     93         
     94         inserterr=detect();
     95         xu=num(detect());
     96         pos=num(detect());
     97         str[xu-1].insert(pos,inserterr);
     98     }
     99     else if(cmd=="reset"){
    100         string reseterr;
    101         int xu;
    102         
    103         reseterr=detect();
    104         xu=num(detect());
    105         str[xu-1].assign(reseterr);
    106     }
    107     else
    108         return cmd;
    109     return "";
    110 }
    111 
    112 int main(int argc, const char * argv[]) {
    113     cin>>t;
    114     string tmp;
    115     for(int i=0;i<t;i++){
    116         cin>>tmp;
    117         str[i].assign(tmp);
    118     }
    119     while(cmd!="over"){
    120     detect();
    121     }
    122     /*for(int i=0;i<t;i++){
    123         cout<<str[i]<<endl;;
    124     }*/
    125     return 0;
    126 }
  • 相关阅读:
    ARP 协议
    3GPP 5G UPF
    OpenStack v.s. Kubernetes
    C#- FTP递归下载文件
    C#- WinForm获取 当前执行程序路径的几种方法
    C#- 布署WinForm程序
    Delphi- 连接MySQL数据库BDE
    Delphi- 内置数据库的使用例子BDE
    CSS- 兼容样式记录
    Delphi- DLL操作
  • 原文地址:https://www.cnblogs.com/maskoff/p/8980405.html
Copyright © 2011-2022 走看看