#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL num1_num AS LONG
    LOCAL num1_den AS LONG
    LOCAL num2_num AS LONG
    LOCAL num2_den AS LONG
    LOCAL res_num AS LONG
    LOCAL res_den AS LONG
    LOCAL i AS LONG
    LOCAL a AS LONG
    LOCAL b AS LONG
    LOCAL t AS LONG
    LOCAL temp_str AS STRING
    LOCAL check1 AS LONG
    LOCAL check2 AS LONG
    LOCAL check3 AS LONG
    LOCAL check4 AS LONG

    ' Rational arithmetic: operate on num/den pairs

    ' Test 1: 1/2 + 1/3 = 5/6
    num1_num = 1
    num1_den = 2
    num2_num = 1
    num2_den = 3
    res_num = num1_num * num2_den + num2_num * num1_den
    res_den = num1_den * num2_den

    ' Reduce
    a = res_num
    b = res_den
    WHILE b <> 0
        t = b
        b = a MOD b
        a = t
    WEND
    res_num = res_num / a
    res_den = res_den / a

    PRINT "1/2 + 1/3 = " + STR$(res_num) + "/" + STR$(res_den)
    check1 = res_num
    check1 = res_den

    ' Test 2: 2/3 - 1/6 = 1/2
    num1_num = 2
    num1_den = 3
    num2_num = 1
    num2_den = 6
    res_num = num1_num * num2_den - num2_num * num1_den
    res_den = num1_den * num2_den

    a = res_num
    b = res_den
    WHILE b <> 0
        t = b
        b = a MOD b
        a = t
    WEND
    res_num = res_num / a
    res_den = res_den / a

    PRINT "2/3 - 1/6 = " + STR$(res_num) + "/" + STR$(res_den)
    check2 = res_num

    ' Test 3: 2/5 * 3/4 = 3/10
    num1_num = 2
    num1_den = 5
    num2_num = 3
    num2_den = 4
    res_num = num1_num * num2_num
    res_den = num1_den * num2_den

    a = res_num
    b = res_den
    WHILE b <> 0
        t = b
        b = a MOD b
        a = t
    WEND
    res_num = res_num / a
    res_den = res_den / a

    PRINT "2/5 * 3/4 = " + STR$(res_num) + "/" + STR$(res_den)

    ' Test 4: 3/7 / 2/5 = 15/14
    num1_num = 3
    num1_den = 7
    num2_num = 2
    num2_den = 5
    res_num = num1_num * num2_den
    res_den = num1_den * num2_num

    a = res_num
    IF a < 0 THEN a = -a
    b = res_den
    IF b < 0 THEN b = -b
    WHILE b <> 0
        t = b
        b = a MOD b
        a = t
    WEND
    IF res_den < 0 THEN
        res_num = -res_num
        res_den = -res_den
    END IF
    res_num = res_num / a
    res_den = res_den / a

    PRINT "3/7 / 2/5 = " + STR$(res_num) + "/" + STR$(res_den)

    ' Test 5: Compare 1/2 and 2/3
    num1_num = 1
    num1_den = 2
    num2_num = 2
    num2_den = 3
    a = num1_num * num2_den
    b = num2_num * num1_den
    IF a < b THEN
        PRINT "1/2 < 2/3"
    ELSEIF a > b THEN
        PRINT "1/2 > 2/3"
    ELSE
        PRINT "1/2 = 2/3"
    END IF

    ' Test 6: Reduce 12/36 = 1/3
    res_num = 12
    res_den = 36
    a = res_num
    b = res_den
    WHILE b <> 0
        t = b
        b = a MOD b
        a = t
    WEND
    res_num = res_num / a
    res_den = res_den / a
    PRINT "12/36 = " + STR$(res_num) + "/" + STR$(res_den)

    ' Test 7: Improper fraction 7/4
    res_num = 7
    res_den = 4
    a = res_num
    b = res_den
    WHILE b <> 0
        t = b
        b = a MOD b
        a = t
    WEND
    res_num = res_num / a
    res_den = res_den / a
    PRINT "7/4 = " + STR$(res_num) + "/" + STR$(res_den) + " = " + STR$(res_num / res_den) + " + " + STR$(res_num MOD res_den) + "/" + STR$(res_den)

    ' Test 8: Negative fraction
    res_num = -3
    res_den = 8
    PRINT "-3/8 = " + STR$(res_num) + "/" + STR$(res_den)

    ' Test 9: GCD calculation demo
    PRINT ""
    PRINT "GCD calculations:"
    a = 48
    b = 18
    t = 0
    WHILE b <> 0
        t = b
        b = a MOD b
        a = t
    WEND
    PRINT "GCD(48, 18) = " + STR$(a)

    a = 1071
    b = 462
    WHILE b <> 0
        t = b
        b = a MOD b
        a = t
    WEND
    PRINT "GCD(1071, 462) = " + STR$(a)

    ' Test 10: Add three fractions
    num1_num = 1
    num1_den = 6
    num2_num = 1
    num2_den = 3
    res_num = num1_num * num2_den + num2_num * num1_den
    res_den = num1_den * num2_den
    ' Add 1/2
    num1_num = res_num
    num1_den = res_den
    num2_num = 1
    num2_den = 2
    res_num = num1_num * num2_den + num2_num * num1_den
    res_den = num1_den * num2_den

    a = res_num
    b = res_den
    WHILE b <> 0
        t = b
        b = a MOD b
        a = t
    WEND
    res_num = res_num / a
    res_den = res_den / a
    PRINT "1/6 + 1/3 + 1/2 = " + STR$(res_num) + "/" + STR$(res_den)

    PRINT ""
    PRINT "Rational arithmetic demo complete."

    FUNCTION = 0
END FUNCTION
