Jump to content

Where is wrong??


Andreik
 Share

Recommended Posts

I try to write this _Distance() function to C and work but result is different, what problem could be?

My C code:

extern "C" __declspec(dllexport) float _Distance(char *str1, char *str2, int maxoffset){
    float result;
    int len1 = strlen(str1);
    int len2 = strlen(str2);
    if ((len1 * len2) == 0)
    {
        return (float)len1 + (float)len2;
    }
    int c = 0;
    int offset1 = 0;
    int offset2 = 0;
    int dist = 0;
    while (((c + offset1) < len1) && ((c + offset2) < len2)){
        if (str1[c + offset1 + 1] != str2[c + offset2 +1]){
            offset1 = 0;
            offset2 = 0;
            for (int index = 0; index < maxoffset; ++index){
                if ((c + index < len1) && (str1[c + index + 1] == str2[c + 1])){
                    if (index > 0){
                        dist += 1;
                        offset1 = index;
                    }
                    dist -= 1;
                    break;
                }
                if ((c + index < len2) && (str1[c + 1] == str2[c + index + 1])){
                    if (index > 0){
                        dist += 1;
                        offset2 = index;
                    }
                    dist -= 1;
                    break;
                }
            }
            dist += 1;
        }
        c += 1;
    }
    result = (float)dist + ((float)len1 - (float)offset1 + (float)len2 - (float)offset2) / 2 - (float)c;
    return result;
}

When the words fail... music speaks.

Link to comment
Share on other sites

I try to write this _Distance() function to C and work but result is different, what problem could be?

My C code:

extern "C" __declspec(dllexport) float _Distance(char *str1, char *str2, int maxoffset){
    float result;
    int len1 = strlen(str1);
    int len2 = strlen(str2);
    if ((len1 * len2) == 0)
    {
        return (float)len1 + (float)len2;
    }
    int c = 0;
    int offset1 = 0;
    int offset2 = 0;
    int dist = 0;
    while (((c + offset1) < len1) && ((c + offset2) < len2)){
        if (str1[c + offset1 + 1] != str2[c + offset2 +1]){
            offset1 = 0;
            offset2 = 0;
            for (int index = 0; index < maxoffset; ++index){
                if ((c + index < len1) && (str1[c + index + 1] == str2[c + 1])){
                    if (index > 0){
                        dist += 1;
                        offset1 = index;
                    }
                    dist -= 1;
                    break;
                }
                if ((c + index < len2) && (str1[c + 1] == str2[c + index + 1])){
                    if (index > 0){
                        dist += 1;
                        offset2 = index;
                    }
                    dist -= 1;
                    break;
                }
            }
            dist += 1;
        }
        c += 1;
    }
    result = (float)dist + ((float)len1 - (float)offset1 + (float)len2 - (float)offset2) / 2 - (float)c;
    return result;
}
One error is

for (int index = 0; index < maxoffset; ++index){

which should be

for (int index = 0; index <= maxoffset; ++index){

There could be others.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

One error is

for (int index = 0; index < maxoffset; ++index){

which should be

for (int index = 0; index <= maxoffset; ++index){

There could be others.

This shouldn't be an error. AutoIt code is:

For $i = 0 to $maxOffset - 1
For maxoffset equal with 5 the range is 0-4.

In my C code:

for (int index = 0; index < maxoffset; ++index){
Variable index should be < then maxoffset (equal with 5) so the range is 0-4.

Am I missing something?

When the words fail... music speaks.

Link to comment
Share on other sites

This shouldn't be an error. AutoIt code is:

For $i = 0 to $maxOffset - 1
For maxoffset equal with 5 the range is 0-4.

In my C code:

for (int index = 0; index < maxoffset; ++index){
Variable index should be < then maxoffset (equal with 5) so the range is 0-4.

Am I missing something?

Sorry Adreik, somehow I couldn't see that -1.

Can you give an example of two strings that give different answers in each version?

EDIT: Maybe this is it. In the autoit version the strings are parsed from the first character which is element 1, but in C they start at 0 so you are 1 position out in various places.

For Example

in AutoIt there is

If (($c + $i < $ls1) And (StringMid($s1, $c + $i + 1, 1) = StringMid($s2, $c + 1, 1))) Then

so in C you should have

if ((c + index < len1 - 1) && (str1[c + index] == str2[c])){

which is not what you wrote.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

EDIT: Maybe this is it. In the autoit version the strings are parsed from the first character which is element 1, but in C they start at 0 so you are 1 position out in various places.

Yes, you are right. I tried and get good results. Thanks for help.

Regards,

Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...