' Desktop calculator simulation with button grid and operation logic
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL numKeys  AS LONG
    LOCAL keyRow(30) AS LONG
    LOCAL keyCol(30) AS LONG
    LOCAL disp AS STRING
    LOCAL accum  AS DOUBLE
    LOCAL operand  AS DOUBLE
    LOCAL result  AS DOUBLE
    LOCAL lastOp  AS LONG
    LOCAL clearNext  AS LONG
    PRINT "=== Desktop Calculator ==="
    PRINT

    disp = "0"
    accum = 0.0
    operand = 0.0
    result = 0.0
    lastOp = 0
    clearNext = 0

    PRINT "--- Layout ---"
    PRINT "  [7] [8] [9] [/]"
    PRINT "  [4] [5] [6] [*]"
    PRINT "  [1] [2] [3] [-]"
    PRINT "  [0] [.] [=] [+]"
    PRINT "  [C] [CE][+/-][%]"
    PRINT

    PRINT "--- Test: 7 + 5 * 3 = ---"
    disp = "7"
    PRINT "  Enter: " & disp
    accum = 7.0
    lastOp = 1

    disp = "5"
    PRINT "  Enter: " & disp
    operand = 5.0
    accum = accum + operand
    lastOp = 2
    PRINT "  Op: +, result: " & FORMAT$(accum)

    disp = "3"
    PRINT "  Enter: " & disp
    operand = 3.0
    accum = accum * operand
    PRINT "  Op: *, result: " & FORMAT$(accum)

    disp = "="
    PRINT "  = Result: " & FORMAT$(accum)
    PRINT

    PRINT "--- Test: 100 / 4 = ---"
    accum = 100.0
    operand = 4.0
    result = accum / operand
    PRINT "  100 / 4 = " & FORMAT$(result)
    PRINT

    PRINT "--- Test: 25% of 80 ---"
    result = 80.0 * 25.0 / 100.0
    PRINT "  25% of 80 = " & FORMAT$(result)
    PRINT

    PRINT "--- Memory Functions ---"
    PRINT "  MC: Memory Clear"
    PRINT "  MR: Memory Recall"
    PRINT "  MS: Memory Store"
    PRINT "  M+: Memory Add"
    PRINT "  M-: Memory Subtract"
    PRINT

    PRINT "--- Keyboard Shortcuts ---"
    PRINT "  ESC: Clear"
    PRINT "  Enter: ="
    PRINT "  Backspace: CE"
    PRINT "  0-9.: Digits"
    PRINT "  +-*/: Operators"

    PRINT
    PRINT "Calculator simulation complete."

    FUNCTION = 0
END FUNCTION
