Calculator
#23
Code: (Select All)
' Incomplete calculator - demonstration of a better way of using clickable buttons.  Let the program
' CALCULATE the button positions.  Code is shorter, simpler, self-documenting, and much easier to modify.

Dim ckey$(50), bx1(50), bx2(50), by1(50), by2(50), gbuffer(8000)
Screen _NewImage(800, 600, 32)
Dim As _Float acc, stack

For r = 1 To 9 '                                 row
    For c = 1 To 4 '                             column
        Read t$ '                                button contents
        If Len(t$) Then '                        not a blank button
            x1 = 20 + (c - 1) * 100 '            x start
            y1 = 99 + (r - 1) * 50 '             y start
            x2 = x1 + 60 '                       x end
            y2 = y1 + 30 '                       y end
            For t = 0 To 5 '                     shaded box
                Line (x1 + t, y1 + t)-(x2 - t, y2 - t), _RGB32(t * 42), B
            Next t
            _PrintString ((x2 + x1) \ 2 - _PrintWidth(t$) \ 2, y1 + 8), t$ '     centered button text
            n = n + 1 '                          button number
            ckey$(n) = t$: bx1(n) = x1: by1(n) = y1: bx2(n) = x2: by2(n) = y2 '  store button
        End If
    Next c
Next r '                                        done generating, displaying, and storing buttons

Do
    _PrintString (20, 30), Str$(acc) + Space$(20) '   show accumulator
    _PrintString (20, 60), Space$(20) '               blank line
    If Len(op$) Then _PrintString (20, 60), Str$(stack) + op$

    Do: _Limit 20 '                                   check keyboard and mouse
        i$ = InKey$
        If Len(i$) Then '                             keyboard key has been pressed
            If i$ = Chr$(27) Then System '            Esc quits
            If i$ = Chr$(13) Then i$ = "=" '          key remapping
            If i$ = "c" Then i$ = "CLR" '             key remapping
            If i$ = "*" Then i$ = "x" '               key remapping
            For i = 1 To n '                          does this key match a button?
                If i$ = ckey$(i) Then hit = i '       yes, save which one
            Next i
        Else '                                        check mouse
            While _MouseInput: Wend
            mx = _MouseX: my = _MouseY
            For i = 1 To n
                Line (bx1(i), by1(i))-(bx2(i), by2(i)), _RGB32(0), B
                If (mx > bx1(i)) And (mx < bx2(i)) And (my > by1(i)) And (my < by2(i)) Then
                    Line (bx1(i), by1(i))-(bx2(i), by2(i)), _RGB32(255), B
                    If _MouseButton(1) Then hit = i: i$ = ckey$(i)
                End If
            Next i
        End If
    Loop Until hit

    i = hit: hit = 0
    Get (bx1(i), by1(i))-(bx2(i), by2(i)), gbuffer(0) '
    Put (bx1(i), by1(i)), gbuffer(), PReset '    highlight key
    _Delay .2 '                                  ensure user has time to see highlight
    Put (bx1(i), by1(i)), gbuffer(), PSet '      restore key

    If InStr("0123456789", i$) Then '            entering a number
        If div = 0 Then acc = 0: div = 1
        If div > 1 Then
            acc = acc + Val(i$) / div
            div = div * 10
        Else
            acc = acc * 10 + Val(i$)
        End If
    ElseIf InStr("+-x/", i$) Then '              operator
        op$ = i$
        stack = acc
        acc = 0
    End If

    Select Case i$ '                             other buttons, many non-functional for this demo
        Case Is = "="
            If op$ = "+" Then acc = stack + acc
            If op$ = "-" Then acc = stack - acc
            If op$ = "x" Then acc = stack * acc
            If op$ = "/" Then acc = stack / acc
            op$ = ""
            stack = 0
            div = 0
        Case Is = ".": div = 10
        Case Is = "CLR": acc = 0: stack = 0: op$ = ""
        Case Is = "ûx": acc = Sqr(acc)
        Case Is = "Sin": acc = Sin(acc)
        Case Is = "Cos": acc = Cos(acc)
        Case Is = "Tan": acc = Tan(acc)
        Case Is = "Deg": angle_mode = angle_mode Xor 1
        Case Is = "xý": acc = acc * acc
        Case Is = "Log": acc = Log(acc)
        Case Is = "pi": acc = _Pi
        Case Is = "1/x": If acc > 0 Then acc = 1 / acc
        Case Is = "x/2": acc = acc / 2
        Case Is = "Exp"
        Case Is = "+/-": acc = -acc
        Case Is = "Copy" '
        Case Is = "Paste"
        Case Is = "<Back" '
        Case Is = "RND": acc = Rnd
    End Select
Loop

Data ,,,CLR
Data ûx,Sin,Cos,Tan
Data 7,8,9,/
Data 4,5,6,x
Data 1,2,3,-
Data 0,.,=,+
Data Deg,xý,Log,pi
Data "1/x",x/2,Exp,+/-
Reply


Messages In This Thread
Calculator - by SierraKen - 07-24-2022, 10:21 PM
RE: Calculator - by SierraKen - 07-24-2022, 10:40 PM
RE: Calculator - by Coolman - 07-25-2022, 09:34 AM
RE: Calculator - by James D Jarvis - 07-25-2022, 01:05 PM
RE: Calculator - by Coolman - 07-25-2022, 01:43 PM
RE: Calculator - by OldMoses - 07-25-2022, 11:54 AM
RE: Calculator - by bplus - 07-25-2022, 01:28 PM
RE: Calculator - by SierraKen - 07-25-2022, 07:08 PM
RE: Calculator - by Kernelpanic - 07-25-2022, 09:49 PM
RE: Calculator - by SierraKen - 07-25-2022, 09:53 PM
RE: Calculator - by Kernelpanic - 07-25-2022, 10:24 PM
RE: Calculator - by bplus - 07-25-2022, 09:53 PM
RE: Calculator - by OldMoses - 07-28-2022, 12:41 AM
RE: Calculator - by Kernelpanic - 07-25-2022, 10:45 PM
RE: Calculator - by mnrvovrfc - 07-25-2022, 10:50 PM
RE: Calculator - by bplus - 07-25-2022, 11:10 PM
RE: Calculator - by SierraKen - 07-26-2022, 12:22 AM
RE: Calculator - by SierraKen - 07-26-2022, 04:09 PM
RE: Calculator - by bplus - 07-26-2022, 07:15 PM
RE: Calculator - by SierraKen - 07-26-2022, 09:05 PM
RE: Calculator - by SierraKen - 07-26-2022, 09:19 PM
RE: Calculator - by SierraKen - 07-27-2022, 03:11 AM
RE: Calculator - by Pete - 07-27-2022, 04:38 AM
RE: Calculator - by ChiaPet - 07-27-2022, 06:08 AM
RE: Calculator - by bplus - 07-27-2022, 11:51 AM
RE: Calculator - by ChiaPet - 07-28-2022, 03:54 PM
RE: Calculator - by SierraKen - 07-27-2022, 03:31 PM
RE: Calculator - by Kernelpanic - 07-27-2022, 05:34 PM
RE: Calculator - by Pete - 07-27-2022, 06:32 PM
RE: Calculator - by SierraKen - 07-27-2022, 08:02 PM
RE: Calculator - by Kernelpanic - 07-27-2022, 08:12 PM
RE: Calculator - by SierraKen - 07-27-2022, 08:12 PM
RE: Calculator - by Kernelpanic - 07-27-2022, 08:46 PM
RE: Calculator - by SierraKen - 07-27-2022, 09:27 PM
RE: Calculator - by Pete - 07-27-2022, 09:54 PM
RE: Calculator - by Jack - 07-27-2022, 10:26 PM
RE: Calculator - by Pete - 07-27-2022, 10:42 PM
RE: Calculator - by Pete - 07-27-2022, 11:21 PM
RE: Calculator - by SierraKen - 07-27-2022, 11:59 PM
RE: Calculator - by SierraKen - 07-28-2022, 10:11 PM
RE: Calculator - by Pete - 07-29-2022, 01:00 AM
RE: Calculator - by SierraKen - 07-29-2022, 08:30 PM
RE: Calculator - by Kernelpanic - 07-29-2022, 08:42 PM
RE: Calculator - by SierraKen - 07-29-2022, 08:45 PM
RE: Calculator - by SierraKen - 07-29-2022, 08:47 PM



Users browsing this thread: 26 Guest(s)