1

有人能帮忙吗?

while (x > level)
    x = x – 1;
x = 0
4

1 回答 1

1

圈复杂度可以使用此处提供的公式计算。

Cyclomatic complexity = E - N + P 
where,
  E = number of edges in the flow graph.
  N = number of nodes in the flow graph.
  P = number of nodes that have exit points

对于您的情况,图表应如下所示:

---------------                ----------
|  x > level  |----- NO ------>| x = x-1|
|-------------|                ----|-----
       |      |---------------------
       |
      Yes
       |
-------|----------
| End while (if) |
-------|----------
       |
       |
   ---------
   |  x = 0 |
   ----------

(不是 ASCII 艺术人士)

所以,cyclomatic complexity应该是:

E = 4, N = 4, P = 2 => Complexity = 4 - 4 + 2 = 2

[edit] Ira Baxter很好地指出了如何为 , 等语言简化这种计算JavaC#但是C++,必须仔细执行识别条件,如下所示

- Start with a count of one for the method.
- Add one for each of the following flow-related elements that are found in the method.
    Returns - Each return that isn't the last statement of a method.
    Selection - if, else, case, default.
    Loops - for, while, do-while, break, and continue.
    Operators - &&, ||, ?, and :
    Exceptions - catch, finally, throw, or throws clause.
于 2016-03-11T22:00:25.780 回答