Calculator - SierraKen - 08-15-2024
Lots of goodies on this one.
Code: (Select All)
'Calculator
'By SierraKen on July 30, 2020.
'Updated on July 24, 2022 with Copy and Paste Buttons.
'Updated on July 26, 2022 with Back Space Button and RND (Random) Button
'Update on July 29, 2022 with math fix problem with Pete's help.
'-----------------------------------------------------------------------
'This is my very first regular calculator.
'Thank you to B+ and TempodiBasic for the help and everyone for the inspiration!
'Thanks Pete for the math help!
_Title "Calculator - Esc for help."
Dim num As Double
Screen _NewImage(400, 525, 32)
f& = _LoadFont("Arial.ttf", 18)
begin:
_Font f&
num$ = ""
c = 0
a = 0: s = 0: t = 0: d = 0
'Setup calculator
Line (50, 25)-(265, 50), _RGB32(255, 255, 255), B
Line (50, 75)-(350, 350), _RGB32(255, 255, 255), B
'Buttons
For buttony = 75 To 405 Step 55
For buttonx = 50 To 275 Step 75
For bb = 0 To 10
c = c + 10
Line (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
Next bb
Paint (buttonx + 12, buttony + 12), _RGB32(100 + c, 100 + c, 100 + c)
c = 0
Next buttonx
Next buttony
'Copy Button
buttonx = 50: buttony = 460
For bb = 0 To 10
c = c + 10
Line (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
Next bb
Paint (buttonx + 12, buttony + 12), _RGB32(100 + c, 100 + c, 100 + c)
c = 0
'Paste Button
buttonx = 125: buttony = 460
For bb = 0 To 10
c = c + 10
Line (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
Next bb
Paint (buttonx + 12, buttony + 12), _RGB32(100 + c, 100 + c, 100 + c)
c = 0
'Back Space Button
buttonx = 200: buttony = 460
For bb = 0 To 10
c = c + 10
Line (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
Next bb
Paint (buttonx + 12, buttony + 12), _RGB32(100 + c, 100 + c, 100 + c)
c = 0
'Random Number Button
buttonx = 275: buttony = 460
For bb = 0 To 10
c = c + 10
Line (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
Next bb
Paint (buttonx + 12, buttony + 12), _RGB32(100 + c, 100 + c, 100 + c)
c = 0
'Green C Button
buttonx = 275: buttony = 20
For bb = 0 To 10
c = c + 10
Line (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(50, 100 + c, 50), B
Next bb
Paint (buttonx + 12, buttony + 12), _RGB32(50, 100 + c, 50)
Color _RGB32(0, 0, 0), _RGB32(50, 100 + c, 50)
_PrintString (50, 5), "Rad"
wer:
'Label Buttons
_PrintString (306, 47), "C"
Color _RGB32(0, 0, 0), _RGB32(210, 210, 210)
_PrintString (87, 102), Chr$(251) 'square root
_PrintString (152, 102), "sin"
_PrintString (227, 102), "cos"
_PrintString (302, 102), "tan"
_PrintString (87, 157), "7"
_PrintString (162, 157), "8"
_PrintString (237, 157), "9"
_PrintString (312, 157), "/"
_PrintString (87, 212), "4"
_PrintString (162, 212), "5"
_PrintString (237, 212), "6"
_PrintString (312, 212), "x"
_PrintString (87, 267), "1"
_PrintString (162, 267), "2"
_PrintString (237, 267), "3"
_PrintString (312, 267), "-"
_PrintString (87, 322), "0"
_PrintString (162, 322), "."
_PrintString (237, 322), "="
_PrintString (312, 322), "+"
_PrintString (79, 377), " "
_PrintString (79, 377), "deg"
_PrintString (162, 377), "x" + Chr$(253) 'second power
_PrintString (227, 377), "log"
_PrintString (307, 377), "Pi"
_PrintString (79, 432), "1/x"
_PrintString (152, 432), "x/2"
_PrintString (227, 432), "exp"
_PrintString (312, 432), Chr$(241) 'Postive or Negative
_PrintString (68, 487), "Copy"
_PrintString (141, 487), "Paste"
_PrintString (210, 487), "<Back"
_PrintString (293, 487), "RND"
Color _RGB32(255, 255, 255), _RGB32(0, 0, 0)
Do
_Limit 30
a$ = InKey$
If a$ <> "" Then mousex = 0: mousey = 0: mouseLeftButton = 0
If a$ = Chr$(27) Then GoTo help:
If a$ = Chr$(3) Then a$ = "": GoTo clip: 'Ctrl+C copies to clipboard.
If a$ = "C" Or a$ = "c" Then a$ = "": GoTo delete: 'Back Space deletes output.
If a$ = "p" Or a$ = "P" Or a$ = "n" Or a$ = "N" Then a$ = "": GoTo posneg: 'Positive or Negative
If a$ = "f" Or a$ = "F" Then a$ = "": GoTo fraction:
If a$ = "h" Or a$ = "H" Then a$ = "": GoTo half:
If a$ = "e" Or a$ = "E" Then a$ = "": GoTo expcommand:
If a$ = "r" Or a$ = "R" Or a$ = "d" Or a$ = "D" Then a$ = "": GoTo radanddeg:
If a$ = "u" Or a$ = "U" Then a$ = "": GoTo squared:
If a$ = "l" Or a$ = "L" Then a$ = "": GoTo logarithm:
If a$ = "i" Or a$ = "I" Then a$ = "": GoTo pi:
If a$ = "q" Or a$ = "Q" Then a$ = "": GoTo squareroot:
If a$ = "s" Or a$ = "S" Then a$ = "": GoTo sine:
If a$ = "o" Or a$ = "O" Then a$ = "": GoTo cosine:
If a$ = "t" Or a$ = "T" Then a$ = "": GoTo tangent:
If a$ = "1" Then a$ = "": GoTo one:
If a$ = "2" Then a$ = "": GoTo two:
If a$ = "3" Then a$ = "": GoTo three:
If a$ = "4" Then a$ = "": GoTo four:
If a$ = "5" Then a$ = "": GoTo five:
If a$ = "6" Then a$ = "": GoTo six:
If a$ = "7" Then a$ = "": GoTo seven:
If a$ = "8" Then a$ = "": GoTo eight:
If a$ = "9" Then a$ = "": GoTo nine:
If a$ = "0" Then a$ = "": GoTo zero2:
If a$ = "." Then a$ = "": GoTo decimal:
If a$ = "=" Then a$ = "": GoTo equals:
If a$ = "+" Then a$ = "": GoTo add:
If a$ = "-" Then a$ = "": GoTo subtract:
If a$ = "*" Or a$ = "x" Or a$ = "X" Then a$ = "": GoTo multiply:
If a$ = "/" Then a$ = "": GoTo divide:
If a$ = Chr$(8) Then a$ = "": GoTo backbutton:
While _MouseInput: Wend
mousex = _MouseX
mousey = _MouseY
mouseLeftButton = _MouseButton(1)
If mouseLeftButton Then
Clear_MB 1
'Clipboard
If mousex > 50 And mousex < 265 And mousey > 25 And mousey < 50 Then
clip:
Color _RGB32(0, 255, 0), _RGB32(0, 0, 0)
_PrintString (55, 30), num$
_Delay .5
num2$ = _Trim$(num$)
_Clipboard$ = num2$
Color _RGB32(255, 255, 255), _RGB32(0, 0, 0)
_PrintString (55, 30), num$
End If
'Clipboard Button
If mousex > 50 And mousex < 125 And mousey > 460 And mousey < 515 Then
clip2:
Color _RGB32(0, 255, 0), _RGB32(0, 0, 0)
_PrintString (55, 30), num$
buttonx = 50: buttony = 460
GoSub press:
_Delay .5
num2$ = _Trim$(num$)
_Clipboard$ = num2$
Color _RGB32(255, 255, 255), _RGB32(0, 0, 0)
_PrintString (55, 30), num$
End If
'Paste Button
If mousex > 125 And mousex < 200 And mousey > 460 And mousey < 515 Then
paste:
buttonx = 125: buttony = 460
GoSub press:
num$ = _Clipboard$
If Len(num$) > 19 Then num$ = "0"
_PrintString (55, 30), num$
num = Val(num$)
GoSub number:
End If
'Back Space Button
If mousex > 200 And mousex < 275 And mousey > 460 And mousey < 515 Then
backbutton:
buttonx = 200: buttony = 460
GoSub press:
num$ = Left$(num$, Len(num$) - 1)
GoSub number2
End If
'Random Button
If mousex > 275 And mousex < 350 And mousey > 460 And mousey < 515 Then
timebutton:
buttonx = 275: buttony = 460
GoSub press:
num$ = Str$(Rnd)
_PrintString (55, 30), num$
num = Val(num$)
GoSub number:
End If
'Clear
If mousex > 275 And mousex < 350 And mousey > 20 And mousey < 75 Then
delete:
a = 0: s = 0: t = 0: d = 0: num$ = ""
_PrintString (55, 30), " "
buttonx = 275: buttony = 20
GoSub zero:
End If
'1/x
If mousex > 50 And mousex < 125 And mousey > 405 And mousey < 460 Then
fraction:
num = Val(num$)
If num = 0 Then GoTo skipthis:
num = 1 / num
num$ = Str$(num)
skipthis:
buttonx = 50: buttony = 405
GoSub press:
GoSub number:
End If
'x/2
If mousex > 126 And mousex < 200 And mousey > 405 And mousey < 460 Then
half:
num = Val(num$)
num = num / 2
num$ = Str$(num)
buttonx = 126: buttony = 405
GoSub press:
GoSub number2:
End If
'EXP
If mousex > 200 And mousex < 275 And mousey > 405 And mousey < 460 Then
expcommand:
_PrintString (55, 30), " "
num = Exp(Val(num$))
num$ = Str$(num)
buttonx = 200: buttony = 405
GoSub number2:
GoSub press:
End If
'Postive or Negative
If mousex > 275 And mousex < 350 And mousey > 405 And mousey < 460 Then
posneg:
If Val(num$) = 0 Then GoTo skipplusnegative:
If Val(num$) < 0 Then
num = -Val(num$)
num$ = Str$(num)
GoTo skipplusnegative:
End If
num$ = "-" + num$
skipplusnegative:
buttonx = 275: buttony = 405
GoSub press:
GoSub number:
End If
'Radians and Degrees
If mousex > 50 And mousex < 125 And mousey > 350 And mousey < 405 Then
radanddeg:
deg = deg + 1
If deg = 2 Then GoTo deg2rad:
Color _RGB32(0, 0, 0), _RGB32(210, 210, 210)
_PrintString (79, 377), " "
_PrintString (79, 377), "rad"
Color _RGB32(0, 0, 0), _RGB32(50, 100 + c, 50)
_PrintString (50, 5), "Deg"
num = Val(num$)
num = _R2D(num)
num$ = Str$(num)
GoTo skipdeg2rad:
deg2rad:
deg = 0
Color _RGB32(0, 0, 0), _RGB32(210, 210, 210)
_PrintString (79, 377), " "
_PrintString (79, 377), "deg"
Color _RGB32(0, 0, 0), _RGB32(50, 100 + c, 50)
_PrintString (50, 5), "Rad"
num = Val(num$)
num = _D2R(num)
num$ = Str$(num)
skipdeg2rad:
buttonx = 50: buttony = 350
GoSub press:
GoSub number:
End If
'Second Power
If mousex > 126 And mousex < 200 And mousey > 350 And mousey < 405 Then
squared:
num = Val(num$)
num = num ^ 2
num$ = Str$(num)
buttonx = 126: buttony = 350
GoSub press:
GoSub number2:
End If
'logarithm
If mousex > 200 And mousex < 275 And mousey > 350 And mousey < 405 Then
logarithm:
num = Val(num$)
If num = 0 Then GoTo skiplog:
If num < 0 Then GoTo skiplog:
num = Log(num)
num$ = Str$(num)
skiplog:
buttonx = 200: buttony = 350
GoSub press:
GoSub number:
End If
'Pi
If mousex > 275 And mousex < 350 And mousey > 350 And mousey < 405 Then
pi:
'num = 3.14159265359
num = _Pi
num$ = Str$(num)
buttonx = 275: buttony = 350
GoSub press:
GoSub number:
End If
'Square Root
If mousex > 50 And mousex < 125 And mousey > 75 And mousey < 130 Then
squareroot:
If num < 0 Then GoTo skip1:
num = Val(num$)
num = Sqr(num)
num$ = Str$(num)
skip1:
buttonx = 50: buttony = 75
GoSub press:
GoSub number:
End If
'Sine
If mousex > 126 And mousex < 200 And mousey > 75 And mousey < 130 Then
sine:
If deg = 1 Then num = Sin(_D2R(Val(num$)))
If deg = 0 Then num = Sin(num)
num$ = Str$(num)
buttonx = 126: buttony = 75
GoSub press:
GoSub number:
End If
'Cosine
If mousex > 200 And mousex < 275 And mousey > 75 And mousey < 130 Then
cosine:
If deg = 1 Then num = Cos(_D2R(Val(num$)))
If deg = 0 Then num = Cos(num)
num$ = Str$(num)
buttonx = 200: buttony = 75
GoSub press:
GoSub number:
End If
'Tangent
If mousex > 275 And mousex < 350 And mousey > 75 And mousey < 130 Then
tangent:
If deg = 1 Then num = Tan(_D2R(Val(num$)))
If deg = 0 Then num = Tan(num)
num$ = Str$(num)
buttonx = 275: buttony = 75
GoSub press:
GoSub number:
End If
'Number Buttons
If mousex > 50 And mousex < 125 And mousey > 130 And mousey < 185 Then
seven:
num$ = num$ + "7"
If n = 1 Then num$ = "-" + num$: n = 0
buttonx = 50: buttony = 130
GoSub press:
GoSub number:
End If
If mousex > 126 And mousex < 200 And mousey > 130 And mousey < 185 Then
eight:
num$ = num$ + "8"
If n = 1 Then num$ = "-" + num$: n = 0
buttonx = 126: buttony = 130
GoSub press:
GoSub number:
End If
If mousex > 200 And mousex < 275 And mousey > 130 And mousey < 185 Then
nine:
num$ = num$ + "9"
If n = 1 Then num$ = "-" + num$: n = 0
buttonx = 200: buttony = 130
GoSub press:
GoSub number:
End If
If mousex > 50 And mousex < 125 And mousey > 185 And mousey < 240 Then
four:
num$ = num$ + "4"
If n = 1 Then num$ = "-" + num$: n = 0
buttonx = 50: buttony = 185
GoSub press:
GoSub number:
End If
If mousex > 126 And mousex < 200 And mousey > 185 And mousey < 240 Then
five:
num$ = num$ + "5"
If n = 1 Then num$ = "-" + num$: n = 0
buttonx = 126: buttony = 185
GoSub press:
GoSub number:
End If
If mousex > 200 And mousex < 275 And mousey > 185 And mousey < 240 Then
six:
num$ = num$ + "6"
If n = 1 Then num$ = "-" + num$: n = 0
buttonx = 200: buttony = 185
GoSub press:
GoSub number:
End If
If mousex > 50 And mousex < 125 And mousey > 240 And mousey < 295 Then
one:
num$ = num$ + "1"
If n = 1 Then num$ = "-" + num$: n = 0
buttonx = 50: buttony = 240
GoSub press:
GoSub number:
End If
If mousex > 126 And mousex < 200 And mousey > 240 And mousey < 295 Then
two:
num$ = num$ + "2"
If n = 1 Then num$ = "-" + num$: n = 0
buttonx = 126: buttony = 240
GoSub press:
GoSub number:
End If
If mousex > 200 And mousex < 275 And mousey > 240 And mousey < 295 Then
three:
num$ = num$ + "3"
If n = 1 Then num$ = "-" + num$: n = 0
buttonx = 200: buttony = 240
GoSub press:
GoSub number:
End If
If mousex > 50 And mousex < 125 And mousey > 295 And mousey < 350 Then
zero2:
If num$ <> "0" Then ' Prevents more than one leading zero input.
num$ = num$ + "0"
If n = 1 Then num$ = "-" + num$: n = 0
buttonx = 50: buttony = 295
GoSub press:
GoSub number:
End If
End If
'Decimal
If mousex > 126 And mousex < 200 And mousey > 295 And mousey < 350 Then
decimal:
buttonx = 126: buttony = 295
For check = 1 To Len(num$)
If Mid$(num$, check, 1) = "." Then GoTo skipdec:
Next check
num$ = num$ + "."
_PrintString (55, 30), num$
skipdec:
GoSub press:
End If
'Equals
If mousex > 200 And mousex < 275 And mousey > 295 And mousey < 350 Then
equals:
If a = 1 Then num = Val(oldnum$) + Val(num$): a = 0
If s = 1 Then num = Val(oldnum$) - Val(num$): s = 0
If t = 1 Then num = Val(oldnum$) * Val(num$): t = 0
If d = 1 Then num = Val(oldnum$) / Val(num$): d = 0
'----------------------------------------------------
num$ = LTrim$(Str$(num))
'----------------------------------------------------
buttonx = 200: buttony = 295
GoSub press:
GoSub number2:
num$ = ""
num = 0
oldnum$ = ""
End If
'Add
If mousex > 275 And mousex < 350 And mousey > 295 And mousey < 350 Then
add:
d = 0: a = 1: s = 0: t = 0
GoSub convert:
oldnum$ = num$
_PrintString (55, 30), " "
num$ = ""
buttonx = 275: buttony = 295
GoSub press:
End If
'Subtract
If mousex > 275 And mousex < 350 And mousey > 240 And mousey < 295 Then
subtract:
d = 0: a = 0: s = 1: t = 0
GoSub convert:
oldnum$ = num$
_PrintString (55, 30), " "
num$ = ""
buttonx = 275: buttony = 240
GoSub press:
End If
'Multiply
If mousex > 275 And mousex < 350 And mousey > 185 And mousey < 240 Then
multiply:
If t = 0 Then
d = 0: a = 0: s = 0: t = 1
GoSub convert:
oldnum$ = num$
_PrintString (55, 30), " "
num$ = ""
GoTo nex2:
End If
If t = 1 Then
num = Val(oldnum$) * Val(num$)
t = 0
End If
GoSub number2:
nex2:
buttonx = 275: buttony = 185
GoSub press:
End If
'Divide
If mousex > 275 And mousex < 350 And mousey > 130 And mousey < 185 Then
divide:
If d = 0 Then
d = 1: a = 0: s = 0: t = 0
GoSub convert:
oldnum$ = num$
_PrintString (55, 30), " "
num$ = ""
GoTo nex1:
End If
If d = 1 And num$ <> "" Then
num = Val(oldnum$) / Val(num$)
d = 0
End If
GoSub number2:
nex1:
buttonx = 275: buttony = 130
GoSub press:
End If
End If
Loop
'For Number Buttons.
convert:
If Val(num$) > 9999999999999999 Or Len(num$) > 19 Then
num$ = Left$(num$, 19)
End If
num = Val(num$)
Return
number:
If Val(num$) > 9999999999999999 Or Len(num$) > 19 Then
num$ = Left$(num$, 19)
End If
'For Math
number2:
Color _RGB32(0, 0, 0), _RGB32(0, 0, 0)
_PrintString (55, 30), Space$(40)
If Val(num$) > 9999999999999999 Or Len(num$) > 19 Then
num$ = Left$(num$, 19)
End If
Color _RGB32(255, 255, 255), _RGB32(0, 0, 0)
_PrintString (55, 30), num$
Return
'Pressing Each Button
press:
c = 110
For bb = 0 To 10
c = c - 10
Line (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
Next bb
_Delay .03
For bb = 0 To 10
c = c + 10
Line (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
Next bb
Return
'Pressing the Green C Button.
zero:
c = 110
For bb = 0 To 10
c = c - 10
Line (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(50, 100 + c, 50), B
Next bb
_Delay .25
For bb = 0 To 10
c = c + 10
Line (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(50, 100 + c, 50), B
Next bb
Return
help:
_Font 14
Cls
_Title "Calculator Help"
Print "Esc = Keyboard Help"
Print "CTRL+C = Copies To Clipboard"
Print "Left Mouse Click Copies To Clipboard."
Print "Copy Button Copies To Clipboard"
Print "Paste Button Pastes Clipboard To Screen"
Print "Backspace = Deletes Last Output Digit"
Print "RND = Makes a random number from 0 to 1."
Print "Letters can be upper or lower-case."
Print "P or N = Positve and Negative Switch."
Print "F = 1 / x"
Print "H = x / 2"
Print "E = EXP"
Print "R or D = Radian and Degree Switch."
Print "U = X ^ 2"
Print "L = Logarithm"
Print "I = Pi"
Print "Q = Square Root"
Print "s = Sine"
Print "o = Cosine"
Print "t = Tangent"
Print
Print "Below can be either Number Pad or the others."
Print "1-9 = Number Keys"
Print ". = Decimal"
Print "= = Equals"
Print "+ = Add"
Print "- = Subtract"
Print "* or X = Multiply"
Print "/ = Divide"
Print
Print "Press Esc to go back to calculator."
Do: Loop Until InKey$ = Chr$(27)
Cls
_Title "Calculator - Esc for help."
GoTo begin:
Sub Clear_MB (var As Integer)
Do Until Not _MouseButton(var)
While _MouseInput: Wend
Loop
End Sub 'Clear_MB
|