Class Session Notes


Index > Small Basic Calculator


Introduction

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.

GraphicsWindow.Title = "Calculator in Small Basic"
rows = 5
cols = 4
bMargin = 10
bWidth = 80
bHeight = 60
GraphicsWindow.FontSize = 40
GraphicsWindow.BrushColor = "Green"
GraphicsWindow.Width = (bMargin + bWidth + bMargin) * cols
GraphicsWindow.Height = (bMargin + bHeight + bMargin) * (rows + 1)
resultBox = Controls.AddTextBox(bMargin, bMargin)
Controls.SetSize(resultBox, GraphicsWindow.Width - 2 * bMargin, bHeight)
Clear()
Controls.ButtonClicked = CheckButtonClicked

'map the buttons
bString = "C/*-789+456^123←0±.="

'draw keys
For r = 1 To rows For c = 1 To cols keys[r][c][1] = bMargin + (bMargin + bWidth + bMargin) * (c - 1) 'x location
keys[r][c][2] = bMargin + (bMargin + bHeight + bMargin) * r 'y location
keys[r][c][3] = Text.GetSubText(bString, (r - 1) * cols + c, 1) 'button caption
keys[r][c][4] = Controls.AddButton(keys[r][c][3], keys[r][c][1], keys[r][c][2]) 'create button
Controls.SetSize(keys[r][c][4], bWidth, bHeight) 'resize button
EndFor
EndFor

'check buttons
Sub CheckButtonClicked input = Controls.GetButtonCaption(Controls.LastClickedButton)
PerformAction()
EndSub

Sub PerformAction If Text.ConvertToUpperCase(input) = "C" Then 'clear contents
Clear() ElseIf input = "←" And workingValue <> 0 Then If Text.GetLength(workingValue) = 1 Then workingValue = 0 Else workingValue = Text.GetSubText(workingValue, 1, Text.GetLength(workingValue) - 1) 'strip last character EndIf ElseIf input = "±" Then workingValue = -workingValue ElseIf input = "." Then If Text.IsSubText(workingValue, ".") Then 'do nothing as we have a period already Else workingValue = Text.Append(workingValue, input) EndIf ElseIf input = "=" Then PerformCalculation() ElseIf input = "/" Or input = "*" Or input = "-" Or input = "+" Or input = "^" Then If workingOperation = "" Then workingOperation = input
memoryValue = workingValue
workingValue = 0
Else PerformCalculation()
workingOperation = input
memoryValue = workingValue
workingValue = 0
EndIf
ElseIf input >= 0 And input <= 9 Then 'append number keys If Text.IsSubText(workingValue, ".") Then workingValue = Text.Append(workingValue, input) ElseIf workingValue = 0 Then workingValue = input Else workingValue = Text.Append(workingValue, input) EndIf EndIf
DisplayResult()
If input = "=" Then workingValue = 0 'when the user clicks on a number, it will show the number in place of result EndIf
EndSub

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

Sub PerformCalculation If workingOperation = "/" Then newValue = memoryValue / workingValue ElseIf workingOperation = "*" Then newValue = memoryValue * workingValue ElseIf workingOperation = "-" Then newValue = memoryValue - workingValue ElseIf workingOperation = "+" Then newValue = memoryValue + workingValue ElseIf workingOperation = "^" Then newValue = Math.Power(memoryValue, workingValue) EndIf

memoryValue = newValue
workingValue = newValue
workingOperation = ""
EndSub