Public Class Form1
Private Sub Button1_Click(sender As Button, e As EventArgs) Handles Button1.Click
Dim date1, date2 As DateTime, dif As TimeSpan
RichTextBox1.ReadOnly = True : RichTextBox2.ReadOnly = True
sender.Enabled = False
RichTextBox3.Text = String.Empty
sender.Text = "Calculating"
Label4.Text = "Inspecting First Number..." : MakeNumber(RichTextBox1.Text, RichTextBox1)
Label4.Text = "Inspecting Second Number..." : MakeNumber(RichTextBox2.Text, RichTextBox2)
date1 = Now
If RichTextBox1.Text.Length >= RichTextBox2.Text.Length Then _
HumanAddition(RichTextBox1.Text.ToCharArray, RichTextBox2.Text.ToCharArray) Else _
HumanAddition(RichTextBox2.Text.ToCharArray, RichTextBox1.Text.ToCharArray)
date2 = Now
dif = date2 - date1
Label3.Text = "Answer: " & RichTextBox3.TextLength & " / 2147483647"
Label4.Text = "Calculation finished in: " & CInt(dif.TotalMilliseconds) & "ms"
sender.Text = "Calculate"
RichTextBox1.ReadOnly = False : RichTextBox2.ReadOnly = False
sender.Enabled = True
End Sub
Private Sub TextBox_TextChanged(sender As RichTextBox, e As EventArgs) Handles RichTextBox1.TextChanged, RichTextBox2.TextChanged
If sender.Name = "RichTextBox1" Then _
Nums1Count.Text = "1st number: " & RichTextBox1.TextLength & " / 2147483646" Else _
Nums2Count.Text = "2nd number: " & RichTextBox2.TextLength & " / 2147483646"
End Sub
Private Sub MakeNumber(ByVal input As String, ByRef output As RichTextBox)
Dim l As Integer = 0
While l < input.Length
Select Case input(l)
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" : l += 1 : Application.DoEvents()
Case Else : input = input.Replace(input(l), String.Empty) : Application.DoEvents()
End Select
End While
output.Text = input
End Sub
Private Sub HumanAddition(ByVal nums1() As Char, ByVal nums2() As Char)
Dim carry As Integer = 0
Dim crntcalc As Integer = 0
Dim result() As Char = " " & nums1
Dim nums1marker As Integer = nums1.Length
Dim numsdif As Integer = nums1marker - (nums1marker - nums2.Length)
Dim num2index = numsdif - (nums1marker - nums1marker)
For i = nums1marker - 1 To 0 Step -1
num2index -= 1
If num2index >= 0 Then crntcalc = result(i + 1).ToString * 1 + nums2(num2index).ToString Else crntcalc = nums1(i).ToString
If carry > 0 Then
crntcalc += carry
If i > 0 Then carry = 0
End If
If crntcalc > 9 Then
carry = crntcalc.ToString.First.ToString
result(i + 1) = crntcalc.ToString.Last
Else : result(i + 1) = crntcalc.ToString
End If
If i = 0 And crntcalc > 9 Then
result(0) = carry.ToString
carry = 0
ElseIf num2index < 0 And carry = 0 Then : Exit For
End If
ProgressBarPro.Width = ((nums1marker - i) * Panel1.Width) / nums1marker
Label4.Text = "Calculating: %" & CInt(((nums1marker - i) * 100) / nums1marker)
Application.DoEvents()
Next
ProgressBarPro.Width = Panel1.Width
Label4.Text = "Calculating: %100"
RichTextBox3.Text = New String(result).TrimStart(New Char() {" ", "0"})
Label4.Text = 100
End Sub
End Class