0

I'm writing an edit distance program that print the right sequenze of command (ADD,DEL,SET). this is my code:

int start(char *path,char *path2) {
char *file1=openFile(path),*file2=openFile(path2);
long int dim1=calcLen(path),dim2 =calcLen(path2);
register int i, j;
int *prev = malloc((dim2 + 1) * sizeof(int));
int *curr = malloc((dim2 + 1) * sizeof(int));
int *tmp = 0;
for(i=0;i<=dim2;i++)prev[i]=i;

//_________________________________________________________________//
for (i = 1; i <= dim1; i++) {
    curr[0] = i;
    for (j = 1; j <= dim2; j++) {

        if (file1[i - 1] != file2[j - 1]) {
            int k = minimum(curr[j - 1], prev[j - 1], prev[j]);
            curr[j] = k + 1;
            if(k==prev[j-1]){
                printf("SET\n");
            }else if(k==prev[j]){
                printf("ADD\n");
            }else if(k==curr[j-1]){
                printf("DEL\n");
            }
        } else {
            curr[j] = prev[j - 1];
        }

    }

    tmp = prev;
    prev = curr;
    curr = tmp;

    memset((void *) curr, 0, sizeof(int) * (dim2 + 1));
  }
}

The two strings are:

  • hello
  • cammelloh

And it print me:

SET
SET
SET
SET
SET
SET
SET
SET
SET
SET
SET
SET
DEL
DEL
DEL
DEL
SET
SET
SET
SET
SET
DEL
DEL
SET
SET
SET
SET
SET
DEL
DEL
SET
SET
SET
SET
SET
SET
ADD
DEL

Process finished with exit code 0

But if i a method that calculate the distance between this two strings it prints me:

EDIT DISTANCE: 5
TIME: 0.002000
Process finished with exit code 0

I realy don't understan how it do that, I think it prints a lot of commands because the second for. But I think that if the algorith is wrong all the program might be wrong and not only the method "start".

This is calcLen:

int calcLen(char *path) {
FILE *fp = fopen(path, "r");
if (fp == NULL) {
    printf("File Not Found!\n");
    return -1;
}
fseek(fp, 0L, SEEK_END);
long int res = ftell(fp);
fclose(fp);
return res;
}

0 Answers0