Hola a todos,
Hace poco os expliqué como calcular la complejidad ciclomática y si recordais no era más que sumar el número de condicionales de nuestra función.
Pues bien, he hecho un programilla que precisamente hace eso, contar condicionales. Es muy sencillo y necesita muchas mejoras para realmente funcionar como debería pero es un buen inicio. Espero que os guste.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
int main(int argc, char *argv[])
{
FILE *fp;
long lSize;
char * buffer;
size_t result;
char *p, *p1, *p2;
int cont = 0;
fp = fopen("funcion.txt","rb");
if (fp == NULL) std::cout << "Falta fichero funcion.txt con el codigo a analizar" << std::endl;
//Obtenemos el tamaño del fichero
fseek (fp , 0 , SEEK_END);
lSize = ftell (fp);
rewind (fp);
// reservamos memoria para cargar el fichero
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) std::cout << "Falta memoria para cargar fichero" << std::endl;
// copy the file into the buffer:
result = fread (buffer,1,lSize,fp);
if (result != lSize) std::cout << "Error en la lectura del fichero. Tamaño esperado : " << lSize << " Tamaño real: " << result <<std::endl;
p = (char *) memchr(buffer,'{',lSize);
while(1)
{
p1 = (char *) memchr(p,'i',lSize-(p-buffer) );
if (p1 == NULL) break;
if( ( *(p1-1) == ' ' ) || ( *(p1-1) == '\t' ) || ( *(p1-1) == '\n' ) )
if( *(p1+1) == 'f' )
{
if( ( *(p1+2) == ' ') || ( *(p1+2) == '(') )
{
cont++;
}
}
p = p1+1;
}
p = (char *) memchr(buffer,'{',lSize);
while(1)
{
p1 = (char *) memchr(p,'c',lSize-(p-buffer) );
if (p1 == NULL) break;
if( ( *(p1-1) == ' ' ) || ( *(p1-1) == '\t' ) || ( *(p1-1) == '\n' ) )
if( *(p1+1) == 'a' )
if( *(p1+2) == 's' )
if( *(p1+3) == 'e' )
if( *(p1+4) == ' ' )
cont++;
p = p1+1;
}
std::cout << "Complejidad ciclomatica : " << cont << std::endl;
fclose(fp);
free (buffer);
system("pause");
return 0;
}
Nos vemos

0 comentarios :
Publicar un comentario