题解 | #参数解析#
参数解析
http://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
用一个布尔变量来判断当前字符串是否要与上一个连接。
循环输入字符串,
(1)若字符串第一个字符是引号,则需要判断该字符串是否结束:
a.最后不是引号,未结束,设置下一个需要连接的标志为真。
b.最后是引号,结束。
(2)字符串第一个字符不是引号,且连接标志为真。连接前需要添加空格:
a.最后一个是引号,说明是需要连接的最后一部分,标志重新设为假。
b.最后不是引号,说明是需要连接的中间部分。
(3)字符串第一个字符不是引号,且连接标志为假。
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
int n=-1;
string str;
vector<string> v;
bool ifcombine=false;
while(cin>>str)
{
string s;
if(str[0]=='"')//第一个是引号,则需要判断该字符串是否结束
{
if(str[str.length()-1]!='"')
{//最后不是引号,未结束,设置下一个需要连接的标志为真
ifcombine=true;
for(int i=1;i<str.length();++i)
s+=str[i];
}
else for(int i=1;i<str.length()-1;++i)
s+=str[i];
v.emplace_back(s);
++n;
}
else if(ifcombine)
{
s+=" ";//连接前加入空格
if(str[str.length()-1]=='"')
{//最后一个是引号,说明是需要连接的最后一部分,标志重新设为假
for(int i=0;i<str.length()-1;++i)
s+=str[i];
ifcombine=false;
}
else for(int i=0;i<str.length();++i)
s+=str[i];//最后不是引号,说明是需要连接的中间部分
v[n]+=s;//添加在上一个字符串的末尾
}
else
{//普通情况,没有引号
v.emplace_back(str);
++n;
}
}
cout<<v.size()<<endl;
for(auto vi:v)
cout<<vi<<endl;
return 0;
}
