vb题目!高分求解!

来源:百度知道 编辑:UC知道 时间:2024/07/04 00:12:49
实验4.8 编一程序,对给定位数的数据,产生检验位。
计算机进行数据处理中,关键字段值的正确与否对系统的运行起了很大作用。例如,在高校招生中,准考证号是表示考生的关键字。为了正确地判断准考证号输入的正确性,将7位数的准考证号在其前面加一检验位,构成有检验位的8位准考证号。现假定产生检验位的公式为:
a=( )mod 10
其中:a为检验位,di为第i位的数。
例如,某准考证号为:2573548,则检验位为:
a=(8*1+5*2+4*3+3*4+7*5+5*6+2*7)mod 10=1
具有检验位的的准考证号为:12573458
本程序要求:
1.输入7位数的准考证号,单击“产生”按钮,产生对应具有检验位的8位数的准考证号,并在标签显示,界面如图所示。当输入非7位数字,显示出错信息,如图2所示,清除输入的内容,重新输入。
2.输入具有检验位的8位数的准考证号,单击“检验”按钮,检验输入的准考证号正确与否。同样,当输入非8位数字,显示出出错信息,清除输入的内容,重新输入。
3.单击“清除”按钮,清除输入文本框的内容。

假设文本框从左到右依次为TextBox1、TextBox2
按钮从左到右依次为CommandButton1、CommandButton2、CommandButton3
则代码如下:

Private Sub CommandButton1_Click()
Dim a As Integer
Dim s As Integer
Dim i As Integer

If Len(TextBox1.Value) <> 7 Then
MsgBox "输入的不是7位号码!"
Else
s = 0
For i = 1 To 7
s = s + Int(Left(TextBox1.Value, i) / 10 ^ (i - 1)) * i
Next
a = s Mod 10
TextBox2.Value = a & TextBox1.Value
End If
End Sub

Private Sub CommandButton2_Click()
Dim a As Integer
Dim s As Integer
Dim i As Integer

If Len(TextBox2.Value) <> 8 Then
MsgBox "输入的不是8位号码!"
Else
s = 0
For i = 1 To 7
s = s + Int(Left(TextBox2.Value, i) / 10 ^ (i - 1)) * i
Next
a = s Mod 10