在命令行输入如下命令:
xcopy /s c:/ d:/,
各个参数如下:
参数1:命令字xcopy
参数2:字符串/s
参数3:字符串c:/
参数4: 字符串d:/
请编写一个参数解析程序,实现将命令行各个参数解析出来。
解析规则:
1.参数分隔符为空格 2.对于用“”包含起来的参数,如果中间有空格,不能解析为多个参数。比如在命令行输入xcopy /s “C:/PRogram files” “d:/”时,参数仍然是4个,第3个参数应该是字符串C:/program files,而不是C:/program,注意输出参数时,需要将“”去掉,引号不存在嵌套情况。3.参数不定长 4.输入由用例保证,不会出现不符合要求的输入
输入一行字符串,可以有空格
输出描述:
输出参数个数,分解后的参数,每个参数都独占一行
输入例子:
xcopy /s c:// d://输出例子:
4xcopy/sc://d://基本的字符串处理问题,分隔时题目要求引号括起来的为一个参数,可以先分隔再对带引号的字符串组合,最后去掉引号字符即可,比较简单。完整AC的代码:
#include <iostream>#include <sstream>#include <vector>#include <algorithm>using namespace std;vector<string> split(string str,char sep){ stringstream ss(str); vector<string> res; string temp; while(getline(ss,temp,sep)){ res.push_back(temp); } return res;}int main(){ string line; while(getline(cin,line)){ vector<string> res=split(line,' '); vector<string> parameters; for(int i=0;i<res.size();i++){ if(res[i].find('"')!=string::npos){ string temp=res[i]; int j=i+1; for(;j<res.size();j++){ temp+=string(" ")+res[j]; if(res[j].find('"')!=string::npos) break; } //去引号 auto it=remove(temp.begin(),temp.end(),'"'); temp.erase(it,temp.end()); parameters.push_back(temp); i=j; } else{ parameters.push_back(res[i]); } } cout<<parameters.size()<<endl; for(auto e:parameters){ cout<<e<<endl; } } return 0;}
新闻热点
疑难解答