08-14-2022, 11:48 AM
A simpler way of doing the string comparison in the last post...
Code: (Select All)
betatest% = -1
DO
DO
LINE INPUT "a$ = "; a$
LINE INPUT "b$ = "; b$
REM Remove trailing zeros after a decimal point.
IF INSTR(a$, ".") THEN
DO UNTIL RIGHT$(a$, 1) <> "0" AND RIGHT$(a$, 1) <> "." AND RIGHT$(a$, 1) <> "-"
a$ = MID$(a$, 1, LEN(a$) - 1)
LOOP
END IF
IF INSTR(b$, ".") THEN
DO UNTIL RIGHT$(b$, 1) <> "0" AND RIGHT$(b$, 1) <> "." AND RIGHT$(b$, 1) <> "-"
b$ = MID$(b$, 1, LEN(b$) - 1)
LOOP
END IF
IF MID$(a$, 1, 2) = "-0" OR a$ = "" OR a$ = "-" THEN a$ = "0"
IF MID$(b$, 1, 2) = "-0" OR b$ = "" OR b$ = "-" THEN b$ = "0"
' A - and +
IF LEFT$(a$, 1) = "-" THEN j% = -1
IF LEFT$(b$, 1) = "-" THEN k% = -1
IF k% = 0 AND j% THEN gl% = -1: EXIT DO
IF j% = 0 AND k% THEN gl% = 1: EXIT DO
' A decimal and non-decimal.
j% = INSTR(a$, ".")
k% = INSTR(b$, ".")
IF j% = 0 AND k% THEN
IF a$ = "0" THEN gl% = -1 ELSE gl% = 1
EXIT DO
END IF
IF k% = 0 AND j% THEN
IF b$ = "0" THEN gl% = 1 ELSE gl% = -1
EXIT DO
END IF
' Both decimals.
IF j% THEN
IF a$ > b$ THEN
gl% = 1
ELSEIF a$ = b$ THEN gl% = 0
ELSEIF a$ < b$ THEN gl% = -1
END IF
EXIT DO
END IF
' Both positive or both negative whole numbers.
SELECT CASE LEN(a$)
CASE IS < LEN(b$)
gl% = -1
CASE IS = LEN(b$)
IF a$ = b$ THEN
gl% = 0
ELSEIF a$ > b$ THEN gl% = 1
ELSEIF a$ < b$ THEN gl% = -1
END IF
CASE IS > LEN(b$)
gl% = 1
END SELECT
EXIT DO
LOOP
IF betatest% THEN
PRINT
SELECT CASE gl%
CASE -1
PRINT "a$ < b$", a$; " < "; b$; " gl% = "; LTRIM$(STR$(gl%))
CASE 0
PRINT "a$ = b$", a$; " = "; b$; " gl% = "; LTRIM$(STR$(gl%))
CASE 1
PRINT "a$ > b$", a$; " > "; b$; " gl% = "; LTRIM$(STR$(gl%))
END SELECT
PRINT
END IF
LOOP