题解 | #参数解析#
参数解析
http://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
临时拼凑,看看其它方法。
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
/*
xcopy /s c:\\ d:\\
4
xcopy
/s
c:\\
d:\\
*/
static const int MAX_STR_NUM=1000;
static const int MAX_STR_LEN=1000;
int main(void)
{
int ret =0;
vector<string> vc_str;
int muti_flag=0;
string tmp;
int arg_num = 0;
while (1)
{
char org_str[MAX_STR_LEN]={0};
ret = scanf("%s",org_str);
if(ret == EOF){
break;
}
//cout<<"get str:"<<org_str<<endl;
if(muti_flag==0) {
if(org_str[0]=='"') {
muti_flag = 1;
tmp.append(org_str+1);
if(tmp[tmp.size()-1]=='"'){
tmp[tmp.size()-1] = 0;
muti_flag = 0;
vc_str.push_back(tmp);
//cout<<"push_back:"<<tmp.c_str()<<endl;
arg_num++;
tmp.clear();
}
}else{
tmp.append(org_str);
vc_str.push_back(tmp);
//cout<<"push_back:"<<tmp.c_str()<<endl;
arg_num++;
tmp.clear();
}
}
else{//muti_flag==1
tmp.append(" ");
tmp.append(org_str);
//int strLen= strlen(org_str);
if(tmp[tmp.size()-1]=='"')
{
tmp[tmp.size()-1] = 0;
muti_flag =0;
vc_str.push_back(tmp);
//cout<<"push_back:"<<tmp.c_str()<<endl;
arg_num++;
tmp.clear();
}
}
}
cout<<vc_str.size()<<endl;
for(auto it=vc_str.begin();it!=vc_str.end();it++){
cout<<it->c_str()<<endl;
}
system("pause");
}
