C语言 以下过程的控制流图和圈复杂度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2669977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:13:14  来源:igfitidea点击:

Control flow graph & cyclomatic complexity for following procedure

ccontrol-flowcyclomatic-complexityblack-boxwhite-box

提问by AJ.

insertion_procedure (int a[], int p [], int N)
{
    int i,j,k;
    for (i=0; i<=N; i++) p[i] = i;
    for (i=2; i<=N; i++)
    {
        k = p[i];
        j = 1;
        while (a[p[j-1]] > a[k]) {p[j] = p[j-1]; j--}
        p[j] = k;
    }
}

I have to find cyclomatic complexity for this code and then suggest some white box test cases and black box test cases. But I am having trouble making a CFG for the code.

我必须找到这段代码的圈复杂度,然后建议一些白盒测试用例和黑盒测试用例。但是我在为代码制作 CFG 时遇到了麻烦。

Would appreciate some help on test cases as well.

也希望能在测试用例方面得到一些帮助。

回答by Vincent Ramdhanie

Start by numbering the statements:

首先为语句编号:

 insertion_procedure (int a[], int p [], int N)
 {
(1)    Int i,j,k;
(2)    for ((2a)i=0; (2b)i<=N; (2c)i++) 
(3)        p[i] = i;
(4)    for ((4a)i=2; (4b)i<=N; (4c)i++)
       {
(5)       k=p[i];j=1;
(6)       while (a[p[j-1]] > a[k]) {
(7)           p[j] = p[j-1]; 
(8)           j--
          }
(9)          p[j] = k;
       }

Now you can clearly see which statement executes first and which last etc. so drawing the cfg becomes simple.

现在您可以清楚地看到哪个语句先执行,哪个最后执行等等。所以绘制 cfg 变得简单。

CFG

CFG

Now, to calculate cyclomatic complexity you use one of three methods:

现在,要计算圈复杂度,您可以使用以下三种方法之一:

  1. Count the number of regions on the graph: 4
  2. No. of predicates (red on graph) + 1 : 3 + 1 = 4
  3. No of edges - no. of nodes + 2: 14 - 12 + 2 = 4.
  1. 计算图上的区域数:4
  2. 谓词数量(图上的红色)+ 1 : 3 + 1 = 4
  3. 没有边缘 - 没有。节点数 + 2:14 - 12 + 2 = 4。

回答by Darin Dimitrov

The cyclomatic complexity is 4.

圈复杂度为 4。

1 for the procedure +1 for the for loop +1 for the while loop +1 for the if condition of the while loop.

1 为过程 +1 为 for 循环 +1 为 while 循环 +1 为 while 循环的 if 条件。

回答by Lineker

You can also use McCabe formula M = E-N + 2C
E = edges
N = nodes
C = components
M = cyclomatic complexity

您也可以使用 McCabe 公式M = E-N + 2C
E = 边
N = 节点
C = 组件
M = 圈复杂度

E = 14
N = 12
C = 1

M = 14-12 + 2*1 = 4

M = 14-12 + 2*1 = 4