Ver Mensaje Individual
  #16 (permalink)  
Antiguo 23/10/2006, 09:55
MaxExtreme
 
Fecha de Ingreso: abril-2005
Mensajes: 3.083
Antigüedad: 19 años, 11 meses
Puntos: 17
Sólo te falta compilarlo, usa algún compilador como el que te trae cygwin haciendo: "gcc -o programa.exe codigo.c"

Si vas a usar palabras de más de 200 caracteres (lo dudo), cambia el número de la línea "#define LENGHT 200".

Gasta memoria sin parar. Para que te hagas una idea, el programa necesitaría 38 MB de RAM para comparar dos ficheros de 100.000 palabras.

Además, para colmo, es lento. El algoritmo es "bruto", y no optimiza absolutamente nada.

De nada.

Código:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LENGHT  200

int main(int argc, char * argv[])
{
    struct {
        FILE * fd;
        int lines;
        struct word {
            char word[LENGHT];
            unsigned char repeated;
        } * words;
    } files[2];
    char buffer[LENGHT];
    unsigned int i, j;
    int ret = 0;

    if (argc != 2 + 1) {
        printf("%s: Invalid sintax: program firstlist.txt secondlist.txt\n", argv[0]);
        return -1;
    }

    for (i = 0; i < 2; i++) {
        files[i].lines = 0;
        files[i].words = NULL;
        files[i].fd = fopen(argv[i+1], "r");
        if (files[i].fd == NULL) {
            printf("%s: Unable to open file %s\n", argv[0], argv[i+1]);
            ret = -1;
            goto out;
        }
    }

    for (i = 0; i < 2; i++) {
        while (fgets(buffer, LENGHT, files[i].fd) != NULL)
            files[i].lines++;
        fseek(files[i].fd, 0, SEEK_SET);
        files[i].words = calloc(files[i].lines, sizeof(struct word));
        if (files[i].words == NULL) {
            printf("%s: Unable to malloc memory: %i bytes\n", argv[0], files[i].lines * sizeof(struct word));
            ret = -2;
            goto out;
        }
        j = 0;
        while (fgets(files[i].words[j].word, LENGHT, files[i].fd) != NULL)
            j++;
    }

    for (i = 0; i < files[0].lines; i++) {
        for (j = 0; j < files[1].lines; j++) {
            if (strcmp(files[0].words[i].word, files[1].words[j].word) == 0)
                files[0].words[i].repeated = 1;
        }
    }

    for (i = 0; i < files[0].lines; i++) {
        if (files[0].words[i].repeated != 1)
            printf(files[0].words[i].word);
    }

out:
    for (i = 0; i < 2; i++) {
        free(files[i].words);
        fclose(files[i].fd);
    }

    return ret;
}

Última edición por MaxExtreme; 24/10/2006 a las 07:35