①把条形码下方数字从右往左依次编序号为“1,2,3,4……”;
②从序号2开始把所有偶数序号位上的数相加求和,并乘以3;
③从序号3开始把所有奇数序号上的数相加求和;
④把第②和第③步结果相加求和;
⑤10减去第④步结果的个位数,得出校验码(如果差值为10,则校验码为0);
如条形码的数字为:977167121601X(X为校验码),则校验码主要产生步骤如下:
1) (1+6+2+7+1+7)×3=72
2) 0+1+1+6+7+9=24
3) 72+24=96
4) 10-6=4
所以最后校验码X=4,此条形码数字为9771671216014。
小何为此编写了VB程序,用以验证条形码输入正确与否。程序运行时,从文本框Text1获取条形码数字部分,单击按钮Command1后,在标签Label1中输出结果。实现上述功能的VB代码如下,请补全代码。
Private Sub Command1_Click()
Dim i As Integer, b As String, Lb As Integer
Dim s1 As Integer, s2 As Integer, tmp As Integer
Dim c As String
Dim d(1 To 13) As Integer '存储数据
Dim res As String '存储结果
b = Text1.Text '输入条形码
Lb = Len(b)
s1 = 0 : s2 = 0
flag = True
res = "输入错误!"
listt1.Clear
If Lb = 13 Then
For i = 1 To 13
c = Mid(b, Lb - i + 1, 1)
If c >= "0" And c <= "9" Then
'输入转换
Else
flag = False : Exit For
End If
Next i
If flag Then
For i = 2 To Lb Step 2
s1 = s1 + d(i) '参数计算1
s2 = '参数计算2
Next i
tmp = s1 * 3 + s2
If Then res = "输入正确!"
End If
End If
Label1.Caption = res
End Sub