Andreik Posted October 17, 2009 Share Posted October 17, 2009 I try to write this _Distance() function to C and work but result is different, what problem could be?My C code:expandcollapse popupextern "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 More sharing options...
martin Posted October 17, 2009 Share Posted October 17, 2009 I try to write this _Distance() function to C and work but result is different, what problem could be? My C code: expandcollapse popupextern "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 More sharing options...
Andreik Posted October 17, 2009 Author Share Posted October 17, 2009 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 - 1For 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 More sharing options...
martin Posted October 17, 2009 Share Posted October 17, 2009 (edited) This shouldn't be an error. AutoIt code is: For $i = 0 to $maxOffset - 1For 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 October 17, 2009 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 More sharing options...
Andreik Posted October 17, 2009 Author Share Posted October 17, 2009 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now