题解 | #参数解析#

参数解析

http://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677

#include<iostream>
#include<vector>
using namespace std;
//先检查引号,遇到奇数个引号的时候进入引号内,直到下一个引号的长度解析为一个参数
//再对空格进行判断,如果空格是在引号外,则空格分割参数
vector<string> parameter_analysis(string s){
    vector<string> res;
    string str="";
    bool flag=false;
    int n=s.length();
    for(int i=0;i<n;i++){
      if(s[i]=='\"'){
          if(flag==false){
              flag=true;
          }else{
              flag=false;
              res.push_back(str);
              str.clear();
          }
      }else{
          if(flag==true){
              str+=s[i];
          }else{
              if((s[i]==' ')&&(str.empty()==false)){
                res.push_back(str);
                str.clear();
              }
              else if(s[i]!=' '){
                    str+=s[i];
              }
          }
      }
    }
    if(str.empty()==false){
        res.push_back(str);
    }
    return res;
}
int main(){
    string temp;
    while(getline(cin,temp)){
        vector<string> str=parameter_analysis(temp);
        int m=str.size();
        cout<<m<<endl;
        for(int i=0;i<m;i++){
            cout<<str[i]<<endl;
        }
    }
    return 0;
}
全部评论

相关推荐

05-21 15:47
门头沟学院 Java
浪漫主义的虹夏:项目有亮点吗,第一个不是纯玩具项目吗,项目亮点里类似ThreadLocal,Redis储存说难听点是花几十分钟绝大部分人都能学会,第二个轮子项目也没体现出设计和技术,想实习先沉淀,好高骛远的自嗨只会害了自己
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务