vb题目!高分求解!

实验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.单击“清除”按钮,清除输入文本框的内容。

Private Sub cmdClear_Click() '#清空内容 

txtNumber.Text = "" 

txtNumberCheck.Text = "" 

End Sub 

Private Sub cmdGenerate_Click() 

strNumber = txtNumber.Text 

'********************检测长度是否为7**************** 

If Len(Trim(strNumber)) <> 7 Then 

MsgBox "准考证号输入错误,请重新输入" 

txtNumber.Text = "" 

Exit Sub 

End If 

'********************检测是否全部为数字**************** 

Dim oRegExp 

Set oRegExp = CreateObject("VBscript.RegExp") 

oRegExp.IgnoreCase = True 

oRegExp.Global = True 

oRegExp.Pattern = "[0-9]{7}" 

If Not oRegExp.Test(strNumber) Then 

MsgBox "准考证号输入错误,请重新输入" 

txtNumber.Text = "" 

Exit Sub 

End If 

'********************生成校验码**************** 

CheckSum = 0 

For i = 1 To 7 

CheckSum = CheckSum + CInt(Mid(strNumber, 8 - i, 1)) * i 

Next 

CheckNum = CheckSum Mod 10 

txtNumberCheck.Text = CStr(CheckNum) & strNumber 

End Sub 

Private Sub cmdCheck_Click() 

strNumberCheck = txtNumberCheck.Text 

'********************检测长度是否为8**************** 

If Len(Trim(strNumberCheck)) <> 8 Then 

MsgBox "准考证号输入错误,请重新输入" 

txtNumberCheck.Text = "" 

Exit Sub 

End If 

'********************检测是否全部为数字**************** 

Dim oRegExp 

Set oRegExp = CreateObject("VBscript.RegExp") 

oRegExp.IgnoreCase = True 

oRegExp.Global = True 

oRegExp.Pattern = "[0-9]{8}" 

If Not oRegExp.Test(strNumberCheck) Then 

MsgBox "准考证号输入错误,请重新输入" 

txtNumberCheck.Text = "" 

Exit Sub 

End If 

MsgBox "校验成功" 

End Sub

温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-05-29
假设文本框从左到右依次为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

If Right(TextBox2.Value, 1) = a Then
MsgBox "有效8位准考证号!"
Else
MsgBox "无效8位准考证号!"
End If

End If

End Sub

Private Sub CommandButton3_Click()
TextBox1.Value = ""
TextBox2.Value = ""
End Sub

excel中测试通过。本回答被提问者采纳
第2个回答  2009-05-29
楼主 我想问你
例如,某准考证号为:2573548,则检验位为:
a=(8*1+5*2+4*3+3*4+7*5+5*6+2*7)mod 10=1
第一个8*1 表示最后一位是8
那么第二个 5*2代表什么呢?
第3个回答  2009-05-29
Function report(x,t)
if(t=1)then
n=1
do wille(n<=8)
y=y+mid(x,n,1)*n
n=n+1
exit do
report=(mid(y,n,1))mod 10
相似回答