我试图理解圈复杂度是如何工作的,有人能解释一下为什么下面的代码给出了3的分数:
CommandLineAgruments Parse(int argc, char *argv[])
{
CommandLineAgruments result;
std::string command;
std::vector<std::string> arguments;
for (int i = 0; i < argc; i++)
{
std::string currentArg = std::string(argv[i]);
size_t index = currentArg.find('-');
if (index == 0)
{
// new commandline argument. Parse the old argument if we have one, then reset
if (command.size() != 0)
{
if (m_supportedCommandLines.find(command) != m_supportedCommandLines.end())
m_supportedCommandLines[command](arguments, result);
else
throw std::invalid_argument("Argument not supported : '" + command + "'");
}
command = currentArg;
arguments.clear();
}
else
{
arguments.push_back(currentArg);
}
}
// Parse last command if we have one
if (command.size() != 0)
{
if (m_supportedCommandLines.find(command) != m_supportedCommandLines.end())
m_supportedCommandLines[command](arguments, result);
else
throw std::invalid_argument("Argument not supported : '" + command + "'");
}
return result;
}
这个函数给出了1的分数:
void ParseOutputArgument(const std::vector<std::string> &arguments,
CommandLineAgruments &commandLine)
{
std::string arg = arguments[0];
std::transform(arg.begin(), arg.end(), arg.begin(), ::tolower);
if (arg == "file")
{
if (arguments.size() != 2)
throw std::invalid_argument("Missing output file path");
commandLine.OutputFile = arguments[1];
commandLine.FileOutputType = CommandLineAgruments::OutputEnum::File;
}
else if (arg == "console")
{
if (arguments.size() != 1)
throw std::invalid_argument("Invalid arguments for output");
commandLine.FileOutputType = CommandLineAgruments::OutputEnum::Console;
}
else
{
throw std::invalid_argument("'" + arg + "' is an invalid argument for output");
}
}
我知道函数A比函数B更复杂,但我很难看出函数B是如何得到1分的。
发布于 2018-06-21 16:43:36
这是CppDepend的一个问题,计划2018年7月推出的新版本2018.2将解决这个问题。感谢克里斯蒂安给我们一个样本来复制它。
https://stackoverflow.com/questions/50956017
复制相似问题