In conjunction to what we did in the previous class with
animated blocks, we created an
application that would prove useful: a calculator!
The calculator uses a standard layout with basic arithmetic
functions such as division, multiplication, subtraction,
addition, exponentiation, and negation.
This article contains Small Basic code snippets. If you
have Small Basic, copy and paste them into your Small Basic
code editor and click Run (F5). If you do not have
Small Basic, you can download it for free
here.
Screenshot
Source Code
Copy and paste the following code into a Small Basic editor
and click Run (F5). If you don't have Small Basic,
you can download it for free
here.
'check buttons Sub CheckButtonClicked
input =Controls.GetButtonCaption(Controls.LastClickedButton)
PerformAction()EndSub
Sub PerformAction
IfText.ConvertToUpperCase(input) ="C"Then'clear contents
Clear()ElseIf input ="←"And workingValue <>0ThenIfText.GetLength(workingValue) =1Then
workingValue =0Else
workingValue =Text.GetSubText(workingValue,1,Text.GetLength(workingValue) -1)'strip last characterEndIfElseIf input ="±"Then
workingValue = -workingValue
ElseIf input ="."ThenIfText.IsSubText(workingValue,".")Then'do nothing as we have a period alreadyElse
workingValue =Text.Append(workingValue, input)EndIfElseIf input ="="Then
PerformCalculation()ElseIf input ="/"Or input ="*"Or input ="-"Or input ="+"Or input ="^"ThenIf workingOperation =""Then
workingOperation = input
memoryValue = workingValue
workingValue =0Else
PerformCalculation()
workingOperation = input
memoryValue = workingValue
workingValue =0EndIfElseIf input >=0And input <=9Then'append number keysIfText.IsSubText(workingValue,".")Then
workingValue =Text.Append(workingValue, input)ElseIf workingValue =0Then
workingValue = input
Else
workingValue =Text.Append(workingValue, input)EndIfEndIf
DisplayResult() If input ="="Then
workingValue =0'when the user clicks on a number, it will show the number in place of resultEndIfEndSub
Sub Clear
memoryValue =0'what we have in memory to work against
workingValue =0'what the user is typing / what to show as result
workingOperation =""'operation to perform
DisplayResult()EndSub
Sub DisplayResult
Controls.SetTextBoxText(resultBox, workingValue)EndSub