作者:小 琛
欢迎转载,请标明出处
在命令行输入如下命令:
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:\
4
xcopy
/s
c:\
d:\
题目分析
以空格和’ ” ‘为判断基础,将记录好的字符串以\n隔开存入新的字符串,再以\n为判断来输出
#include <iostream>
#include <string>
using namespace std;
`void function()
{
string str_;
string res;
size_t num = 0;
getline(cin, str_);
string::iterator it = str_.begin();
while (it != str_.end())
{
if (*it == '"')
{
while (*it != '"')
{
res += *it;
it++;
}
it++;
it++;
num++;
}
if (*it == ' ')
{
res += '\n';
num++;
it++;
continue;
}
res += (*it);
it++;
}
cout << num + 1 << endl;
it = str_.begin();
while (it != str_.end())
{
if (*it == ' ')
{
cout << endl;
it++;
continue;
}
cout << *it;
it++;
}
}
int main()
{
function();
return 0;