Exemple #1
0
int main(int argc, char *argv[]) {

    /*Verifica a quantidade de argumentos passados.*/
    if (argc != 4) {

        printf("Quantidade de argumentos inválidos!\n");

    } else {
        FILE *pt;

        /*Verifica o comando de busca.*/
        if (strcmp(argv[1], "busca_dna") == 0) {

            pt = fopen(argv[3], "r");

            /*Verifica a possibilidade de abrir o arquivo em disco.*/
            if (pt == NULL) {
                printf("Erro ao abrir o arquivo.\n");
                return EXIT_SUCCESS;

            } else {

                /*Realiza todo o processamento necessário e remoção de caracteres inválidos.*/
                int qtdcaracteres = calcularTamanhoArquivo(pt);

                char textoentrada[qtdcaracteres + 1];

                fread(textoentrada, sizeof (char), qtdcaracteres, pt);

                textoentrada[qtdcaracteres] = '\0';

                char textolimpo[qtdcaracteres];

                limpartexto(textoentrada, textolimpo, qtdcaracteres);


                /*Processamento do padrão inserido sobre o texto de entrada.*/
                int result = 0;
                result = KnuthMorrisPratt(textolimpo, strlen(textolimpo), argv[2], strlen(argv[2]));

                if (result == -1) {
                    printf("Not found!\n");
                }

                fclose(pt);
                return EXIT_SUCCESS;
            }
        }else{
            printf("Comando inválido!\n");
        }
    }
}
Exemple #2
0
int main(int argc, char** argv)
{
    char* FilePath;
    FILE* fp;

    char  Text[MAX_BUFFER];
    char* Pattern;
    int   PatternSize = 0;
    int   Line        = 0;
    
    if ( argc < 3 )
    {
        printf("Usage: %s <FilePath> <Pattern>\n", argv[0] );
        return 1;
    }

    FilePath = argv[1];
    Pattern  = argv[2];

    PatternSize = strlen( Pattern );

    if ( (fp = fopen( FilePath, "r"))  == NULL )
    {
        printf("Cannot open file:%s\n", FilePath);
        return 1;
    } 

    while ( fgets( Text, MAX_BUFFER, fp ) != NULL )
    {
        int Position = 
            KnuthMorrisPratt( Text, strlen(Text), 0, Pattern, PatternSize );
        
        Line++;

        if ( Position >= 0 ) 
        {
            printf("line:%d, column:%d : %s", Line, Position+1, Text);
        }
    }

    fclose( fp );

    return 0;
}