Microsoft Small Basic

Program Listing: PHW347
' Division by Writing
' Version 0.1b
' Copyright © 2016 Nonki Takahashi. The MIT License.
' Last update 2015-03-12
'
Not = "False=True;True=False;"
GraphicsWindow.Title = "Division 0.1b"
Form()
Controls.SetTextBoxText(tbox1, 81)
Controls.SetTextBoxText(tbox2, 3)
While "True"
If buttonClicked Then
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(0, 0, gw, gh)
num1 = Controls.GetTextBoxText(tbox1) * 1
num2 = Controls.GetTextBoxText(tbox2) * 1
buttonClicked = "False"
Div()
Else
Program.Delay(200)
EndIf
EndWhile
Sub Div
len1 = Text.GetLength(num1)
len2 = Text.GetLength(num2)
fw = fs * 0.6
' Write the dividend
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(fw * (len2 + 2), fs + 70, num1)
Next()
' Draw a line and an arc
GraphicsWindow.PenWidth = fs / 10
GraphicsWindow.DrawLine(fw * (len2 + 1), fs + 70, fw * (len1 + len2 + 2), fs + 70)
GraphicsWindow.DrawText(fw * (len2 + 1), fs * (1 - 0.15) + 70, ")")
Next()
' Write the divisor
GraphicsWindow.DrawText(fw, fs + 70, num2)
Next()
n = len1 - len2 + 1
For i = 1 To n
' Focus the divisor and a part of the dividend
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Cyan"
rect2 = Shapes.AddRectangle(fw * len2, fs)
Shapes.SetOpacity(rect2, 30)
Shapes.Move(rect2, fw, fs + 70)
If 1 < i Then
rect1 = Shapes.AddRectangle(fw * (len2 + 1), fs)
Else
rect1 = Shapes.AddRectangle(fw * len2, fs)
EndIf
Shapes.SetOpacity(rect1, 30)
Shapes.Move(rect1, fw * (len2 + 2), fs * (2 * (i - 1) + 1) + 70)
Next()
' Make a quotient
If i = 1 Then
n1 = Text.GetSubText(num1, 1, len2)
EndIf
If num2 <= n1 Then
q = Math.Floor(n1 / num2)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(fw * (len2 + i + 1), 70, q)
EndIf
Next()
' Multiply the quotient and the divisor
Shapes.Remove(rect1)
GraphicsWindow.BrushColor = "Cyan"
rectQ = Shapes.AddRectangle(fw * len2, fs)
Shapes.SetOpacity(rectQ, 30)
Shapes.Move(rectQ, fw * (len2 + i + 1), 70)
m = q * num2
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(fw * (len2 + 2), fs * (2 * (i - 1) + 2) + 70, m)
Next()
' Draw a line
GraphicsWindow.PenWidth = fs / 10
y = fs * (2 * (i - 1) + 3) + 70
GraphicsWindow.DrawLine(fw * (len2 + 2), y, fw * (2 * len2 + 3), y)
Shapes.Remove(rect2)
Shapes.Remove(rectQ)
Next()
' Subtract the product from the part of the dividend
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Cyan"
rectD = Shapes.AddRectangle(fw * (len2 + i - 1), fs)
Shapes.SetOpacity(rectD, 30)
Shapes.Move(rectD, fw * (len2 + 2), y - fs * 2)
rectP = Shapes.AddRectangle(fw * (len2 + i - 1), fs)
Shapes.SetOpacity(rectP, 30)
Shapes.Move(rectP, fw * (len2 + 2), y - fs)
r = n1 - m
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(fw * (len2 + i + 1), y, r)
Next()
Shapes.Remove(rectD)
Shapes.Remove(rectP)
' Lower the next part of the dividend
If i < n Then
n1 = Text.GetSubText(num1, i + 1, 1)
GraphicsWindow.DrawText(fw * (len2 + i + 2), y, n1)
n1 = Text.Append(r, n1)
Next()
EndIf
EndFor
'
EndSub
Sub Form
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BrushColor = "LightGray"
GraphicsWindow.PenWidth = 0
Shapes.AddRectangle(gw, 70)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontName = "Courier New"
txt1 = Shapes.AddText("Dividend")
Shapes.Move(txt1, 10, 14)
tbox1 = Controls.AddTextBox(80, 10)
txt2 = Shapes.AddText("Divisor")
Shapes.Move(txt2, 10, 44)
tbox2 = Controls.AddTextBox(80, 40)
Controls.AddButton("Next", 270, 10)
fs = 30
GraphicsWindow.FontSize = fs
buttonClicked = "False"
Controls.ButtonClicked = OnButtonClicked
EndSub
Sub OnButtonClicked
buttonClicked = "True"
EndSub
Sub Next
While Not[buttonClicked]
Program.Delay(200)
EndWhile
buttonClicked = "False"
EndSub