50208. Cyclomatic Complexity

I'm a slow walker, but I never walk backwards.

Task Description

We have a string as a C program; please calculate its cyclomatic complexity. The cyclomatic complexity is the number of keywords, including if, case, while, for, and ? in a program. Note that variables and functions can have names like "for1234" and "while456", but they do not count as the cyclomatic complexity. Also, there will be no string constants having these keywords, and there are no comments to process.

Input Format

The input is a C program.

Output Format

The output is the cyclomatic complexity of the C program.

Sample Description

The purpose of the samples is to calculate the cyclomatic complexity. Both of the samples contain variables or functions with the name similar to the keywords. Note that there could be spaces between the keywords and "(".

Sample Input 1

#include <stdio.h>
void while456(){
    for(int j = 0; j < 2; j++)
        for(int k = 0; k < 2; k++)
            printf("Hello World\n");
}
int main(){
    int for1234 = 7;
    char while789;
    while789 = 'c';
    for(int i = 0; i < 10; i++)for(int j = 0; j < 10; j++);
    printf("%d\n", for1234);
    printf("%c\n", while789);
    while456();
    return 0;
}

Sample Output 1

4

Sample Input 2

#include <stdio.h>
void if456if(){
    int case999 = 10;
    if(case999>15)
        printf("Greater than 15\n");
    else if (case999 == 15)
        printf("Equal to 15\n");
    else
        printf("Less than 15\n");
}
void whwhile(){
    int ifcase = 0;
    while(ifcase!=0);
    while (ifcase);
}
int main(){
    int for123ever = 9;
    printf("%d\n", for123ever);
    whwhile();
    int case1 = 5;
    int case2 = 4;
    int A_case=7;
    int case_case = 7;
    int while_while = 7;
    if456if();
    case1>case2?case1--:case2++;
    case1>case2 ? case1--:case2++;
    case_case < while_while? case_case++ : while_while--;
    case_case < while_while ? case_case++ : while_while--;
    printf("%d\n", case1);
    printf("%d\n", A_case);
    printf("%d\n",case2);
    switch(case1) {
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        default:
            break;
    }
    return 0;
}

Sample Output 2

11

Estimated Cyclomatic Number

11.33

Discussion