gpt4 book ai didi

c++ - CppDepend 中的圈复杂度

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:03 30 4
gpt4 key购买 nike

我想了解圈复杂度的工作原理,有人可以向我解释为什么以下代码的得分为 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 分

enter image description here

最佳答案

这是CppDepend中的一个问题,计划在2018年7月推出的新版本2018.2将解决它。感谢 Kristian,他给了我们一个样本来重现它。

关于c++ - CppDepend 中的圈复杂度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50956017/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com