當前位置:編程學習大全網 - 源碼下載 - 用vb做算術平方根 很急很急!!

用vb做算術平方根 很急很急!!

'添加兩個TextBox 壹個CommandButton 示例代碼如下:

Dim a() As Integer

Private Sub Command1_Click()

If Text1.Text = "" Then

Text1.SetFocus

Else

Sqrt

End If

End Sub

Sub Sqrt()

Dim x As Integer

Dim i As Integer, j As Integer

Dim b As Double, c As Double, d As String

x = Len(Text1.Text)

If x Mod 2 = 0 Then

ReDim a(x / 2)

For i = 1 To UBound(a)

a(i) = Mid(Text1.Text, 2 * i - 1, 2)

Next i

Else

ReDim a(Fix(x / 2) + 1)

For i = 1 To UBound(a)

If i = 1 Then

a(i) = Mid(Text1.Text, i, 1)

Else

a(i) = Mid(Text1.Text, 2 * (i - 1), 2)

End If

Next i

End If

For i = 9 To 1 Step -1

If a(1) >= i ^ 2 Then

c = a(1) - i ^ 2

d = CStr(i)

Exit For

End If

Next i

If UBound(a) > 1 Then

For i = 2 To UBound(a)

c = c * 100 + a(i)

b = Val(d) * 20

For j = 9 To 0 Step -1

If (b + j) * j <= c Then

c = c - (b + j) * j

d = d & CStr(j)

Exit For

End If

Next j

Next i

End If

If c = 0 Then

Text2.Text = d

Else

For i = 1 To 100 '控制精確度的現在為精確到小數點100

c = c * 100

b = Val(d) * 20

For j = 9 To 0 Step -1

If (b + j) * j <= c Then

c = c - (b + j) * j

d = d & CStr(j)

Exit For

End If

Next j

Next i

Text2.Text = Mid(d, 1, UBound(a)) & "." & Mid(d, UBound(a) + 1)

End If

End Sub

Private Sub Form_Click()

MsgBox Sqr(Val(Text1.Text)) & vbCrLf & Len(Text2.Text)

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii < 48 Or KeyAscii > 57 Then

MsgBox "請輸入數字", vbInformation, App.EXEName

KeyAscii = 0

End If

End Sub

  • 上一篇:雲服務器ecs和雲虛擬主機的區別
  • 下一篇:看視頻為什麽會分屏
  • copyright 2024編程學習大全網