用VB编写报分程序
接下来,我们就可以开始编程了。在VB6中新建一个工程文件,在Form1中建立一个Textbox控件Text1,两个CommandButton控件Command1和Command2。Text1的Text属性为空,Command1的Caption属性为“报分”,Command2的Caption属性为“退出”。
#1 源程序如下:
Option Explicit
Private Declare Function sndPlaySound Lib “winmm.dll” Alias “sndPlaySoundA” (ByVal lpszSoundName As String,ByVal uFlags As Long) As Long
Private Sub bf_zcx(x,y,z)'报分子程序,y为数x的长度,z为数x的整数位数
Dim a,num,num1,i,t
If Val(x) = 0 Then
a = sndPlaySound(App.Path + “\” + “0.wav”,&H0)'报0
GoTo aa 'App.Path返回工程文件或EXE文件的当前路径
End If
t = 1
For i = 1 To z '报整数部分
num = Right(Left(x,i),1)??
If Not (i = 1 And z = 2 And num = 1) Then '当x为十位数,且十位数为1时,不报1
If num = 0 Then
t = 0 '当前数num为0时,不报0
Else
If t = 0 Then '当前数不为0,而其前一数为0时,报0和当前数num
a = sndPlaySound(App.Path + “\” + “0.wav”,&H0)'报0
a = sndPlaySound(App.Path + “\” & num & “.wav”,&H0)'报数num
t= 1
Else
a = sndPlaySound(App.Path + “\” & num & “.wav”,&H0)'报数num
End If
End If
End If
If z - i + 1 = 5 And num <> 0 Then '当num为0时,不报万
a = sndPlaySound(App.Path + “\” + “w.wav”, &H0)'报万
End If
If z - i + 1 = 4 And num <> 0 Then '当num为0时,不报千
a = sndPlaySound(App.Path + “\” + “q.wav”,&H0)'报千
End If
If z - i + 1 = 3 And num <> 0 Then'当num为0时,不报百
a = sndPlaySound(App.Path + “\” + “b.wav”,&H0)'报百
End If
If z - i + 1 = 2 And num <> 0 Then'当num为0时,不报十
a = sndPlaySound(App.Path + “\” + “10.wav”,&H0)'报十
End If
Next i
If y = z Then GoTo aa
a = sndPlaySound(App.Path + “\” + “d.wav”,&H0)'报点
t = 1
For i = z + 2 To y'报小数部分
num = Right(Left(x,i),1)
a = sndPlaySound(App.Path + “\” & num & “.wav”,&H0)'报数num
If t >= 3 Then Exit For
t = t + 1
Next i
aa:
a = sndPlaySound(App.Path + “\” + “f.wav”,&H0)'报分
End Sub
Private Sub Command1_Click()
Dim a,i
If Text1.Text > 99999 Then
i = MsgBox(“分数超过99999,不允许报分!”,vbExclamation,“错误”)
GoTo aa
End If
a = Trim(Text1.Text)
Call bf_zcx(a,Len(a), IIf(Int(Val(a)) = Val(a),Len(a),InStr(a,“.”) - 1))?牐?
aa:
End Sub
Private Sub Command1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then '13为回车键
KeyAscii = 0 '避免计算机发出报警声
Call Command1_Click
End If
End Sub
Private Sub Command2_Click()?煟?
Unload Me'本按钮控制软件的退出
End Sub
Private Sub Form_unLoad(cancel As Integer)
End '退出程序
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then '13为回车键
KeyAscii = 0 '避免计算机发出报警声
Call Command1_Click
Exit Sub
End If
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> 127 And KeyAscii <> 46 And KeyAscii <> 8 Then
KeyAscii = 0
Beep '发出报警声
Exit Sub
End If
End If
End Sub
以上程序在Win98、VB6上,运行通过。