Don't make me REPETEND myself...
#24
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
Reply


Messages In This Thread
Don't make me REPETEND myself... - by Pete - 07-31-2022, 08:57 PM
RE: Don't make me REPETEND myself... - by bplus - 07-31-2022, 11:20 PM
RE: Don't make me REPETEND myself... - by Pete - 08-01-2022, 01:00 AM
RE: Don't make me REPETEND myself... - by bplus - 08-01-2022, 03:57 PM
RE: Don't make me REPETEND myself... - by Pete - 08-01-2022, 07:10 PM
RE: Don't make me REPETEND myself... - by Jack - 08-01-2022, 07:47 PM
RE: Don't make me REPETEND myself... - by Pete - 08-03-2022, 12:30 AM
RE: Don't make me REPETEND myself... - by Jack - 08-03-2022, 01:39 AM
RE: Don't make me REPETEND myself... - by Pete - 08-03-2022, 02:49 AM
RE: Don't make me REPETEND myself... - by Stuart - 08-03-2022, 08:21 AM
RE: Don't make me REPETEND myself... - by Dav - 08-03-2022, 10:37 AM
RE: Don't make me REPETEND myself... - by Pete - 08-03-2022, 05:39 PM
RE: Don't make me REPETEND myself... - by Pete - 08-03-2022, 06:48 PM
RE: Don't make me REPETEND myself... - by Pete - 08-03-2022, 10:43 PM
RE: Don't make me REPETEND myself... - by Stuart - 08-04-2022, 07:13 AM
RE: Don't make me REPETEND myself... - by Pete - 08-04-2022, 08:37 AM
RE: Don't make me REPETEND myself... - by Jack - 08-04-2022, 09:23 AM
RE: Don't make me REPETEND myself... - by Stuart - 08-04-2022, 10:30 AM
RE: Don't make me REPETEND myself... - by bplus - 08-04-2022, 01:26 PM
RE: Don't make me REPETEND myself... - by Pete - 08-04-2022, 06:30 PM
RE: Don't make me REPETEND myself... - by Pete - 08-04-2022, 09:17 PM
RE: Don't make me REPETEND myself... - by Jack - 08-04-2022, 10:37 PM
RE: Don't make me REPETEND myself... - by Pete - 08-05-2022, 10:05 AM
RE: Don't make me REPETEND myself... - by Pete - 08-14-2022, 11:48 AM



Users browsing this thread: 7 Guest(s)