DAY 036: _STRCMP
#3
Same approach, basically...

Code: (Select All)
stringmatha$ = "99.9"
stringmathb$ = "1024"
compa$ = stringmatha$: compb$ = stringmathb$ ' So original variables do not get changed.
DO
    WHILE -1 ' Falx loop.
        IF gl% = 2 THEN EXIT WHILE ' For bypassing sign and decimal adjustments when only positive non-decimal numbers are being evaluated.
        ' Remove trailing zeros after a decimal point.
        IF INSTR(compa$, ".") THEN
            DO UNTIL RIGHT$(compa$, 1) <> "0" AND RIGHT$(compa$, 1) <> "." AND RIGHT$(compa$, 1) <> "-"
                compa$ = MID$(compa$, 1, LEN(compa$) - 1)
            LOOP
        END IF
        IF INSTR(compb$, ".") THEN
            DO UNTIL RIGHT$(compb$, 1) <> "0" AND RIGHT$(compb$, 1) <> "." AND RIGHT$(compb$, 1) <> "-"
                compb$ = MID$(compb$, 1, LEN(compb$) - 1)
            LOOP
        END IF

        IF MID$(compa$, 1, 2) = "-0" OR compa$ = "" OR compa$ = "-" THEN compa$ = "0"
        IF MID$(compb$, 1, 2) = "-0" OR compb$ = "" OR compb$ = "-" THEN compb$ = "0"

        ' A - and +
        j% = 0: k% = 0
        IF LEFT$(compa$, 1) = "-" THEN j% = -1
        IF LEFT$(compb$, 1) = "-" THEN k% = -1
        IF k% = 0 AND j% THEN gl% = -1: EXIT DO
        IF j% = 0 AND k% THEN gl% = 1: EXIT DO

        j&& = INSTR(compa$, ".")
        k&& = INSTR(compb$, ".")

        ' A starting decimal and non-decimal.
        IF j&& = 0 AND k&& = 1 THEN
            IF compa$ = "0" THEN gl% = -1 ELSE gl% = 1
            EXIT DO
        END IF
        IF k&& = 0 AND j&& = 1 THEN
            IF compb$ = "0" THEN gl% = 1 ELSE gl% = -1
            EXIT DO
        END IF

        ' remove decimals and align.
        j2&& = 0: k2&& = 0
        IF j&& <> 0 OR k&& <> 0 THEN
            IF j&& THEN compa$ = MID$(compa$, 1, INSTR(compa$, ".") - 1) + MID$(compa$, INSTR(compa$, ".") + 1): j2&& = LEN(compa$) - j&& + 1
            IF k&& THEN compb$ = MID$(compb$, 1, INSTR(compb$, ".") - 1) + MID$(compb$, INSTR(compb$, ".") + 1): k2&& = LEN(compb$) - k&& + 1
            compa$ = compa$ + STRING$(k2&& - j2&&, "0")
            compb$ = compb$ + STRING$(j2&& - k2&&, "0")
        END IF
        EXIT WHILE
    WEND

    ' Remove leading zeros if any.
    DO UNTIL LEFT$(compa$, 1) <> "0"
        compa$ = MID$(compa$, 2)
    LOOP
    IF compa$ = "" THEN compa$ = "0"
    DO UNTIL LEFT$(compb$, 1) <> "0"
        compb$ = MID$(compb$, 2)
    LOOP
    IF compb$ = "" THEN compb$ = "0"

    ' Both positive or both negative whole numbers.

    SELECT CASE LEN(compa$)
        CASE IS < LEN(compb$)
            gl% = -1
        CASE IS = LEN(compb$)
            IF compa$ = compb$ THEN
                gl% = 0
            ELSEIF compa$ > compb$ THEN gl% = 1
            ELSEIF compa$ < compb$ THEN gl% = -1
            END IF
        CASE IS > LEN(compb$)
            gl% = 1
    END SELECT
    EXIT DO
LOOP
PRINT compa$, compb$
END
Reply


Messages In This Thread
DAY 036: _STRCMP - by Pete - 12-16-2022, 07:09 PM
RE: DAY 036: _STRCMP - by SMcNeill - 12-16-2022, 08:00 PM
RE: DAY 036: _STRCMP - by Pete - 12-16-2022, 08:28 PM
RE: DAY 036: _STRCMP - by Pete - 12-17-2022, 06:37 AM



Users browsing this thread: 2 Guest(s)