12-17-2022, 06:37 AM
So since I wrote about _STRCMP, which came along after I made the abve function, I figured, hey, might as well make use of it to optimize the string math compare routine a bit...
I'm not 100% sure if it's bullet proof yet, so if anyone can find a flaw, please let me know.
Pete
Code: (Select All)
DO
LINE INPUT "S1: "; string1$
LINE INPUT "S2: "; string2$
IF string1$ = "" AND string2$ = "" THEN SYSTEM
PRINT
SELECT CASE gl%(string1$, string2$)
CASE -1
PRINT string1$; " is smaller than " + string2$
CASE 0
PRINT string1$; " is equal to " + string2$
CASE 1
PRINT string1$; " is larger than " + string2$
END SELECT
PRINT
LOOP
FUNCTION gl% (string1$, string2$)
s1$ = string1$: s2$ = string2$: neg = 1: d1$ = "": d2$ = ""
DO ' Falx loop.
IF LEFT$(string1$, 1) = "-" THEN ' strip off - sign(s). If both are negative, continue.
IF LEFT$(string2$, 1) <> "-" THEN gl% = -1: EXIT DO
s1$ = MID$(s1$, 2)
neg = -1
END IF
IF LEFT$(string2$, 1) = "-" THEN
IF LEFT$(string1$, 1) <> "-" THEN gl% = 1: EXIT DO
s2$ = MID$(s2$, 2)
neg = -1
END IF
s1 = INSTR(s1$, "."): s2 = INSTR(s2$, ".") ' ID if decimal(s).
IF s1 THEN x1$ = MID$(s1$, 1, s1 - 1): d1$ = MID$(s1$, s1 + 1) ELSE x1$ = s1$ ' strip off decimal(s).
IF s2 THEN x2$ = MID$(s2$, 1, s2 - 1): d2$ = MID$(s2$, s2 + 1) ELSE x2$ = s2$
IF LEN(x1$) > LEN(x2$) THEN x2$ = STRING$(LEN(x1$) - LEN(x2$), "0") + x2$
IF LEN(x2$) > LEN(x1$) THEN x1$ = STRING$(LEN(x2$) - LEN(x1$), "0") + x1$
IF LEN(d1$) > LEN(d2$) THEN d2$ = d2$ + STRING$(LEN(d1$) - LEN(d2$), "0")
IF LEN(d2$) > LEN(d1$) THEN d1$ = d1$ + STRING$(LEN(d2$) - LEN(d1$), "0")
x1$ = x1$ + "." + d1$: x2$ = x2$ + "." + d2$
gl% = _STRCMP(x1$, x2$) * neg
EXIT DO
LOOP
END FUNCTION
I'm not 100% sure if it's bullet proof yet, so if anyone can find a flaw, please let me know.
Pete