1)将被乘数a转换为二进制数;
2)将转换后的二进制数中每个数码乘以对应的权值,再乘以乘数b;
3)最后将各乘积相加,得到a与b相乘的结果。
图a显示了13与11相乘的过程:
在文本框Text1中依次输入被乘数与乘数(输入的字符仅限于数字字符、“*”及“/”,每两个乘数间用“/”分隔,并以“/”结尾),单击“计算”按钮Command1后,将计算结果
依次显示在文本框Text2中。程序运行界面如图b所示。
Private Sub Command1_Click()
Dim a As Integer, b As Integer, qz As Integer, sum As Integer
Dim tx As String, c As String, tmp As String
tx = Text1.Text: tmp=""
n = Len(tx):sum = 0
For i = 1 To n
c = Mid(tx, i, 1)
If c >= "0" And c <= "9" Then
ElseIf c = "*" Then
a = Val(tmp): tmp = ""
Else
b = Val(tmp): tmp = ""
End If
If Then
qz = 1
Do While a > 0
x = a Mod 2
qz = qz * 2
a = a \ 2
Loop
s = s & Str(sum)
a = 0: b = 0: sum = 0
End If
Next i
Text2.Text = s
End Sub