16.小明編寫(xiě)了一個(gè)統(tǒng)計(jì)字符串中數(shù)字字符串之和的VB程序(如字符串“ab12.3cd34ef9”,中數(shù)字字符串之和為58:12+3+34+9),運(yùn)行界面如圖所示。在文本框Text1中輸入字符串s,單擊“統(tǒng)計(jì)”按鈕Command1,在Label3中輸出統(tǒng)計(jì)結(jié)果。 實(shí)現(xiàn)上述功能的VB程序代碼如下,但橫線(xiàn)處代碼有錯(cuò),請(qǐng)改正。 Private Sub Command1_Click ( ) Dim s As String,ch As String Dim ch2As String,sum As Double Dim n As Integer,i As Integer s=Text1.Text n=Len(s) i=1:sum=0 Do While ① ch=Mid(s,i,1) If ch>=“0“And ch<=“9“Then j=i ch2=Mid(s,j,1) Do While ch2>=“0“And ch2<=“9“ j=j+1 ch2=Mid(s,j,1) Loop sum=sum+Val()'② i=j End If i=i+1 Loop Label3.Caption=Str(sum) End Sub
。 (2)實(shí)現(xiàn)上述功能的VB程序如下。請(qǐng)?jiān)跈M線(xiàn)處填入合適的代碼。 Private Sub Command1_Click ( ) Dim a As Integer,b As Integer,c As Integer Dim a1As String,b1As String Dim lena1As Integer,lenb1As Integer,i As Integer Dim result As String a=Val(Text1.Text) b=Val(Text2.Text) If a>b Then c=a:a=b:b=c End If result=““ a1=DtoB(a):b1=DtoB(b) lena1=Len(a1):lenb1=Len(b1) i=1 Do While i<=lena1 If Mid(a1,lena1-i+1,1)=Mid(b1,lenb1-i+1,1)Then result=“0“+result Else result=“1“+result End If i=i+1 Loop result=①
Text3.Text=BtoD(result) End Sub Public Function DtoB(x As Integer) As String Dim remainder As String DtoB=““ Do While x>0 remainder=CStr(x Mod 2)'如:CStr(3Mod 2)的值為“1“ DtoB=remainder+DtoB ②
Loop End Function Public Function BtoD(x As String) As Integer Dim i As Integer BtoD=0 For i=1To Len(x) BtoD=③