' Scientific calculator: trig, log, factorial, brackets, expression
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL testCount  AS LONG
    LOCAL result AS DOUBLE
    LOCAL angle  AS DOUBLE
    LOCAL radians  AS DOUBLE
    LOCAL sinVal  AS DOUBLE
    LOCAL cosVal  AS DOUBLE
    LOCAL tanVal  AS DOUBLE
    LOCAL logVal  AS DOUBLE
    LOCAL lnVal  AS DOUBLE
    LOCAL sqrtVal  AS DOUBLE
    LOCAL factVal  AS DOUBLE
    LOCAL powerVal  AS DOUBLE
    LOCAL expVal  AS DOUBLE
    LOCAL invVal  AS DOUBLE
    LOCAL pi AS DOUBLE
    LOCAL stack(20) AS DOUBLE
    LOCAL stackPtr AS LONG
    LOCAL exprTokens(20) AS DOUBLE
    LOCAL numTokens AS LONG

    pi = 3.14159265

    PRINT "=== Scientific Calculator ==="
    PRINT

    PRINT "--- Trigonometric Functions (angle in degrees) ---"
    FOR i = 0 TO 8
        angle = i * 45
        radians = angle * pi / 180.0
        sinVal = SIN(radians)
        cosVal = COS(radians)
        tanVal = (SIN(radians)/COS(radians))

        PRINT "  " & STR$(angle) & CHR$(176) & ": sin=" & FORMAT$(sinVal) & " cos=" & FORMAT$(cosVal) & " tan=" & FORMAT$(tanVal)
    NEXT i
    PRINT

    PRINT "--- Logarithmic Functions ---"
    FOR i = 1 TO 5
        lnVal = 1
        logVal = 1 / 1
        PRINT "  ln(" & FORMAT$(i) & ")=" & FORMAT$(lnVal) & "  log10(" & FORMAT$(i) & ")=" & FORMAT$(logVal)
    NEXT i
    PRINT

    PRINT "--- Power and Root ---"
    powerVal = 1.0
    FOR i = 0 TO 4
        sqrtVal = SQR(i * 10 + 1)
        PRINT "  2^" & FORMAT$(i) & "=" & FORMAT$(powerVal) & "  sqrt(" & FORMAT$(i * 10 + 1) & ")=" & FORMAT$(sqrtVal)
        powerVal = powerVal * 2.0
    NEXT i
    PRINT

    PRINT "--- Factorial ---"
    factVal = 1.0
    FOR i = 1 TO 10
        factVal = factVal * i
        PRINT "  " & FORMAT$(i) & "! = " & FORMAT$(factVal)
    NEXT i
    PRINT

    PRINT "--- Constants ---"
    PRINT "  pi = " & FORMAT$(pi)
    expVal = 2.71828183
    PRINT "  e = " & FORMAT$(expVal)
    PRINT "  pi/2 = " & FORMAT$(pi / 2.0)
    PRINT "  2*pi = " & FORMAT$(2.0 * pi)
    PRINT

    PRINT "--- Expression: (2 + 3) * 4 - 5 / 2 ---"
    stackPtr = 0
    stack(stackPtr) = 2.0
    stackPtr = stackPtr + 1
    stack(stackPtr) = 3.0
    stackPtr = stackPtr + 1
    result = stack(0) + stack(1)
    stackPtr = 0
    stack(stackPtr) = result
    stackPtr = stackPtr + 1
    stack(stackPtr) = 4.0
    stackPtr = stackPtr + 1
    result = stack(0) * stack(1)
    stackPtr = 0
    stack(stackPtr) = result
    stackPtr = stackPtr + 1
    stack(stackPtr) = 5.0
    stackPtr = stackPtr + 1
    stack(stackPtr) = 2.0
    stackPtr = stackPtr + 1
    result = stack(0) / stack(1)
    stackPtr = 0
    stack(stackPtr) = stackPtr
    result = stackPtr
    invVal = stackPtr

    result = (2.0 + 3.0) * 4.0 - 5.0 / 2.0
    PRINT "  Result: " & FORMAT$(result)
    PRINT

    PRINT "--- Expression: sin(30) + sqrt(16) * 2 ---"
    radians = 30.0 * pi / 180.0
    sinVal = SIN(radians)
    sqrtVal = SQR(16.0)
    result = sinVal + sqrtVal * 2.0
    PRINT "  sin(30) = " & FORMAT$(sinVal)
    PRINT "  sqrt(16) = " & FORMAT$(sqrtVal)
    PRINT "  Result: " & FORMAT$(result)
    PRINT

    PRINT "--- Operator Precedence ---"
    PRINT "  1. Parentheses ()"
    PRINT "  2. Functions (sin)"
    PRINT "  3. Power (^)"
    PRINT "  4. Multiply/Divide (*, /)"
    PRINT "  5. Add/Subtract (+, -)"
    PRINT "  6. Assignment (=)"

    PRINT
    PRINT "Scientific calculator complete."

    FUNCTION = 0
END FUNCTION
