Imports System
Imports System.Windows.Forms
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim n, m, o, p, q, r As Integer
Dim r1, s1, t1, t2, t3, t4 As String
Dim random As New Random()
For n = 0 To random.Next(100)
For m = 0 To random.Next(100)
o = n + m
p = n - m
q = n * m
If m <> 0 Then
r = n / m
End If
'用函数将整数转换为基数为 2 的字符串
r1 = IntToBaseString(n, 2)
s1 = IntToBaseString(m, 2)
t1 = IntToBaseString(o, 2)
t2 = IntToBaseString(p, 2)
t3 = IntToBaseString(q, 2)
If m <> 0 Then
t4 = IntToBaseString(r, 2)
End If
'输出结果
TextBox1.Text &= r1 & " + " & s1 & " = " & t1 & vbCrLf
TextBox1.Text &= r1 & " - " & s1 & " = " & t2 & vbCrLf
TextBox1.Text &= r1 & " * " & s1 & " = " & t3 & vbCrLf
If m <> 0 Then
TextBox1.Text &= r1 & " / " & s1 & " = " & t4 & vbCrLf
End If
Next
Next
End Sub
Private Function IntToBaseString(ByVal num As Integer, ByVal base As Integer) As String
Dim buffer As New System.Text.StringBuilder(33)
Dim i As Integer = 0
Dim isNegative As Boolean = False
'处理负数
If num < 0 Then
isNegative = True
num = -num
End If
'转换为字符串
Do
Dim digit As Integer = num Mod base
If digit < 2 Then
buffer.Insert(0, ChrW(48 + digit))
Else
buffer.Insert(0, ChrW(65 + digit - 10))
End If
num = num \ base
Loop While num > 0
'如果是负数,添加负号
If isNegative Then
buffer.Insert(0, "-")
End If
Return buffer.ToString()
End Function
End Class