首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

急求vb计算器代码!该如何解决

2012-01-13 
急求vb计算器代码!!!本人没学过VB,别人托我弄个计算器的代码,请大鸟们帮个忙吧!多谢了!!![解决办法]http:/

急求vb计算器代码!!!
本人没学过VB,别人托我弄个计算器的代码,请大鸟们帮个忙吧!
多谢了!!!

[解决办法]
http://tieba.baidu.com/f?kz=80121222
http://www.moon-soft.com/download/soft/291.htm
[解决办法]
Dim a1, a2, result As Single
 Dim operator As Integer
Private Sub Command1_Click(Index As Integer)
If Index >= 12 And Index <= 15 Then
a1 = CDbl(Text1.Text)
Text1.Text = "0"
operator = Index

End If

If Index = 11 Then
a2 = CDbl(Text1.Text)
Select Case operator
Case 12
result = a1 + a2
Case 13
result = a1 - a2
Case 14
result = a1 * a2
Case 15
result = a1 / a2
End Select
If result > 0 And result < 1 Then
Text1.Text = "0" + CStr(result)
Else
Text1.Text = CStr(result)
End If
End If

If Index >= 0 And Index <= 8 Then
If Text1.Text = "0" Then
Text1.Text = CStr(Index + 1)
Else

Text1.Text = Text1.Text + CStr(Index + 1)
End If
End If

If Index = 9 Then
If Text1.Text = "0" Then
Text1.Text = "0"
Else
Text1.Text = Text1.Text + "0"
End If
End If

If Index = 10 Then
If InStr(Text1.Text, ".") > 0 Then
Else
Text1.Text = Text1.Text + "."
 
End If
End If
End Sub

Private Sub Command2_Click()
Text1.Text = "0"
End Sub

Private Sub Command3_Click()
End
End Sub

Private Sub Form_Load()
Text1.Text = "0"
End Sub


[解决办法]
http://topic.csdn.net/t/20010607/14/149437.html
[解决办法]
http://www.codedn.com/view/660.html
[解决办法]
这个包含了基本的计算,还包含了14种工程计算

-----------------------------------------------
Option Explicit
Public i As Integer
Public j As String
Public k As Double
Public l As String
Public Sum As Double
Public n As Integer
Public Ave As Double
Option Base 1
Public Ab_Ave As Double
Public St_Ave As Double
Public X_Sum As Double
Public Y_Sum As Double
Public XY_Sum As Double
Public Xsqr_Sum As Double
Public Xsqr_Sum1 As Double
Public Ysqr_Sum As Double
Public Ysqr_Sum1 As Double
Public X_Y_Sum As Double
Public AbX_Sum As Double
Public AbY_Sum As Double
Public AbXsqr_Sum As Double
Public nAbXsqr_Sum As Double
Public AbYsqr_Sum As Double
Public XYsqr_Sum As Double
Public YXsqr_Sum As Double
Public m As String
Public t As String
Public b As Double
Public r As Double
Public p As Integer
Public q As Integer

Public k1 As Double

Private StoredValue As Double

Private Const opNone = 0
Private Const opAdd = 1
Private Const opSubtract = 2
Private Const opMultiply = 3
Private Const opDivide = 4
Private Operator As Integer

Private NewEntry As Boolean
Private Sub AquaButton1_Click()


Dim a() As Double

Sum = 0
n = Val(Text1.Text)

If n = 0 Then

MsgBox "请先输入数据的总个数!!!", 48, "警告"

Else

ReDim a(n) As Double
For i = 1 To n
l = InputBox("请输入数据:")
k = Val(l)
a(i) = k
Sum = Sum + a(i)
Next
Text2.Text = ""


Ave = Sum / n
End If

End Sub

Private Sub AquaButton10_Click()
Dim j As Integer


AquaButton11.Enabled = False
AquaButton12.Enabled = False


  
Text8.Text = ""
Text9.Text = ""
Text10.Text = ""
Text11.Text = ""
  
Dim Ax() As Double
Dim Ay() As Double
n = Val(Text7.Text)
If n <= 1 Then
MsgBox "请输入相应的数据个数2个或2个以上", 64, "信息提示"
Text7.Text = ""

Else
ReDim Ax(n) As Double
ReDim Ay(n) As Double

X_Sum = 0
Xsqr_Sum = 0
Xsqr_Sum1 = 0
MsgBox "现在,请输入X的值。", 64, "信息提示"
For j = 1 To n

m = InputBox("请输入X的值:")
Ax(j) = Val(m)
X_Sum = X_Sum + Ax(j)
Xsqr_Sum1 = Xsqr_Sum1 + (Ax(j)) ^ 2

Next

Xsqr_Sum = (X_Sum) ^ 2

MsgBox "现在,请输入Y的值。", 64, "信息提示"

For p = 1 To n

t = InputBox("请输入Y的值:")
Ay(p) = Val(t)
Y_Sum = Y_Sum + Ay(p)
Ysqr_Sum1 = Ysqr_Sum1 + (Ay(p)) ^ 2

Next

Ysqr_Sum = (Y_Sum) ^ 2

X_Y_Sum = 0

For q = 1 To n
X_Y_Sum = X_Y_Sum + Ax(q) * Ay(q)
Next

XY_Sum = (X_Sum) * (Y_Sum)

YXsqr_Sum = (Y_Sum) * (Xsqr_Sum1)

AbXsqr_Sum = (Xsqr_Sum) - n * (Xsqr_Sum1)

nAbXsqr_Sum = n * (Xsqr_Sum1) - (Xsqr_Sum)

AbYsqr_Sum = n * (Ysqr_Sum1) - (Ysqr_Sum)

XYsqr_Sum = (nAbXsqr_Sum) * (AbYsqr_Sum)


If XY_Sum = 0 And AbXsqr_Sum = 0 And nAbXsqr_Sum = 0 And AbYsqr_Sum = 0 Then

MsgBox "数据计算过程中出现错误!请检查数据输入是否正确!", 64, "信息提示"
Text1.Text = ""
Else

k1 = (XY_Sum - n * X_Y_Sum) / (AbXsqr_Sum)

b = ((X_Y_Sum) * (X_Sum) - YXsqr_Sum) / (AbXsqr_Sum)

r = (n * X_Y_Sum - XY_Sum) / Sqr(XYsqr_Sum)

End If
End If
AquaButton11.Enabled = True
End Sub

Private Sub AquaButton11_Click()

 AquaButton10.Enabled = False
Text7.Text = ""
Text7.Enabled = False
  
Text8.Text = ""
Text9.Text = ""
Text10.Text = ""
Text11.Text = ""
  
Text8.Text = Str$(k1)
Text9.Text = Str$(b)
Text10.Text = Str$(r)
Text11.Text = "y = " + Str$(k1) + "*" + "X" + "+" + "(" + Str$(b) + ")"
AquaButton12.Enabled = True
  
End Sub

Private Sub AquaButton12_Click()
Text7.Enabled = True
Text8.Enabled = True
Text9.Enabled = True
Text10.Enabled = True
Text11.Enabled = True
AquaButton10.Enabled = True
AquaButton11.Enabled = False

Text7.Text = ""
Text8.Text = ""
Text9.Text = ""
Text10.Text = ""
Text11.Text = ""
End Sub

Private Sub AquaButton2_Click()

AquaButton1.Enabled = False
AquaButton3.Enabled = False


Text1.Text = ""
Text2.Text = ""
Text2.Text = Str$(Ave)

End Sub

Private Sub AquaButton3_Click()
Call ActiveButton
End Sub

Private Sub AquaButton4_Click()
Dim a() As Double
Dim b() As Double
Dim j As Integer
Sum = 0
n = Val(Text3.Text)

If n = 0 Then


MsgBox "请先输入数据的总个数!!!", 48, "警告"
Else

ReDim a(n) As Double
For i = 1 To n
l = InputBox("请输入数据:")
k = Val(l)
a(i) = k
Sum = Sum + a(i)
Next
Text2.Text = ""
Ave = Sum / n

ReDim b(n) As Double
Ab_Ave = 0
For j = 1 To n
b(j) = Abs(Ave - a(j))
Ab_Ave = Ab_Ave + b(j)
Next
Ab_Ave = Ab_Ave / n
  
End If
End Sub

Private Sub AquaButton5_Click()

Text3.Text = ""
Text4.Text = ""
Text4.Text = Str$(Ab_Ave)
AquaButton4.Enabled = False
Text3.Enabled = False
End Sub

Private Sub AquaButton6_Click()
Text3.Text = ""
Text4.Text = ""
AquaButton4.Enabled = True
Text3.Enabled = True
End Sub

Private Sub AquaButton7_Click()
Dim a() As Double
Dim b() As Double
Dim j As Integer
Sum = 0
n = Val(Text5.Text)

If n = 0 Then
MsgBox "请先输入数据的总个数!!!", 48, "警告"
Else

ReDim a(n) As Double
For i = 1 To n
l = InputBox("请输入数据:")
k = Val(l)
a(i) = k
Sum = Sum + a(i)
Next
Text6.Text = ""
Ave = Sum / n


[解决办法]
接上面

ReDim b(n) As Double
Ab_Ave = 0
For j = 1 To n
b(j) = Abs(Ave - a(j))
Ab_Ave = Ab_Ave + (b(j)) ^ 2
Next
If n = 1 Then
MsgBox "输入数据个数应为2个或2个以上!!", 64, "信息提示"
Text5.Text = ""
Else
 St_Ave = Sqr(Ab_Ave / (n * (n - 1)))
 End If
 End If
End Sub
Private Sub AquaButton8_Click()
AquaButton7.Enabled = False
Text5.Text = ""
Text6.Text = ""
Text6.Text = Str$(St_Ave)
Text5.Enabled = False
end Sub
Private Sub AquaButton9_Click()
Text5.Text = ""
Text6.Text = ""
AquaButton7.Enabled = True
Text5.Enabled = True
End Sub
Private Sub CmdACos_Click()
Dim Ang As Double
Dim Res As Double
Ang = Val(txtDisplay.Text)
If (-Ang * Ang + 1 = 0) Then
MsgBox "你所输入的数据不在该公式的定义域内,请重新输入。", 64, "信息提示"
Else
Res = Round(Atn(-Ang / (-Ang * Ang + 1)), 10) + Round((2 * Atn(1)), 10)
txtDisplay.Text = ""
txtDisplay.Text = Str$(Res)
End If
End Sub
Private Sub CmdACsc_Click()
Dim Ang As Double
Dim Res As Double
Ang = Val(txtDisplay.Text)
If (Ang * Ang - 1) <= 0 Then
MsgBox "你所输入的数据不在该公式的定义域内,请重新输入。", 64, "信息提示"
Else
Res = Round((Atn(Ang / Sqr(Ang * Ang - 1)) + Sgn((Ang) - 1) * (2 * Atn(1))), 10)
txtDisplay.Text = ""
txtDisplay.Text = Str$(Res)
End If
End Sub
Private Sub CmdACTan_Click()
Dim Ang As Double
Dim Res As Double
Ang = Val(txtDisplay.Text)
Res = Atn(Ang) + 2 * Atn(1)
txtDisplay.Text = Str$(Res)
End Sub
Private Sub CmdASec_Click()
Dim Ang As Double
Dim Res As Double
Ang = Val(txtDisplay.Text)
If (Ang * Ang - 1) <= 0 Then
MsgBox "你所输入的数据不在该公式的定义域内,请重新输入。", 64, "信息提示"
Else
Res = Round((Atn(Ang / Sqr(Ang * Ang - 1)) + Sgn((Ang) - 1) * (2 * Atn(1))), 10)
txtDisplay.Text = ""
txtDisplay.Text = Str$(Res)
End If
End Sub
Private Sub CmdASin_Click()
Dim Ang As Double
Dim Res As Double
Ang = Val(txtDisplay.Text)
If (-Ang * Ang + 1 = 0) Then
MsgBox "你所输入的数据不在该公式的定义域内,请重新输入。", 64, "信息提示"
Else
Res = Round(Atn(Ang / (-Ang * Ang + 1)), 10)
txtDisplay.Text = ""
txtDisplay.Text = Str$(Res)
End If
End Sub
private Sub CmdATan_Click()
Dim St As String
Dim Ang As Double


Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
Res = Atn(Ang)
txtDisplay.Text = ""
txtDisplay.Text = Str$(Res)
End Sub
private Sub cmdCos_Click()
Dim Ang As Double
Dim Res As Double
Ang = Val(txtDisplay.Text)
Res = Round(Cos(Ang * 3.14159265358979 / 180), 10)
txtDisplay.Text = ""
txtDisplay.Text = Str$(Res)
End Sub
Private Sub CmdCsc_Click()
Dim Ang As Double
Dim Res As Double
Ang = Val(txtDisplay.Text)
Res = Round(Sin(Ang * 3.14159265358979 / 180), 10)
If Res = 0 Then
 MsgBox "你所输入的数据不在该公式的定义域内,请重新输入。", 64, "信息提示"
Else
Res = 1 / Res
txtDisplay.Text = ""
txtDisplay.Text = Str$(Res)
 End If
End Sub
Private Sub CmdCTan_Click()
Dim Ang As Double
Dim Res As Double
Ang = Val(txtDisplay.Text)
Res = Round(Tan(Ang * 3.14159265358979 / 180), 10)
If Res = 0 Then
MsgBox "你所输入的数据不在该公式的定义域内,请重新输入。", 64, "信息提示"
Else
Res = 1 / Res
txtDisplay.Text = ""
txtDisplay.Text = Str$(Res)
End If
End Sub
Private Sub cmdDecimal_Click()
If InStr(txtDisplay.Text, ".") Then
Beep
Else
If NewEntry Then
txtDisplay.Text = "."
NewEntry = False
Else
txtDisplay.Text = txtDisplay.Text & "."
End If
End If
End Sub

[解决办法]
Private Sub CmdExp_Click()
Dim Ang As Double
Dim Res As Double
Ang = Val(txtDisplay.Text)
Res = Exp(Ang)
txtDisplay.Text = Str(Res)
End Sub
Private Sub CmdHACos_Click()
Dim St As String
Dim Ang As Double
Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
if Ang = 0 Then
MsgBox "所输入的数据不是一个有效值,请重新输入。", 64, "信息提示"
 
ElseIf (Ang + Sqr(Ang * Ang - 1)) <= 0 Or (Ang * Ang - 1) < 0 Then
MsgBox "所输入的数据还不是一个有效值,请重新输入。", 64, "信息提示"
Else
Res = Log(Ang + Sqr(Ang * Ang - 1))
txtDisplay.Text = Str$(Res)
End If
End Sub
Private Sub CmdHACsc_Click()
Dim St As String
Dim Ang As Double
Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
If Ang = 0 Then
MsgBox "所输入的数据不是一个有效值,请重新输入。", 64, "信息提示"

ElseIf ((Sgn(Ang) * Sqr(Ang * Ang + 1) + 1) / Ang) <= 0 Or Ang = 0 Then
MsgBox "所输入的数据不是一个有效值,请重新输入。", 64, "信息提示"

Else
 Res = Log((Sgn(Ang) * Sqr(Ang * Ang + 1) + 1) / Ang)
 txtDisplay.Text = Str$(Res)
End If
End Sub
Private Sub CmdHACTan_Click()
Dim St As String
Dim Ang As Double
Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
If (Ang + 1) * (Ang - 1) <= 0 Then
MsgBox "所输入的数据不是一个有效值,请重新输入。", 64, "信息提示"
 Else
 Res = Log((Ang + 1) * (Ang - 1)) / 2
 txtDisplay.Text = Str$(Res)
 End If
End Sub
Private Sub CmdHASec_Click()
Dim St As String
Dim Ang As Double
Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
if (-Ang * Ang + 1) < 0 Or Ang = 0 Then
 MsgBox "所输入的数据不是一个有效值,请重新输入。", 64, "信息提示"
Else
 Res = Log((Sqr(-Ang * Ang + 1) + 1) / Ang)
 txtDisplay.Text = Str$(Res)
End If
End Sub
Private Sub CmdHASin_Click()
Dim St As String
Dim Ang As Double
Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
If (Ang + Sqr(Ang * Ang + 1)) <= 0 Then
 MsgBox "所输入的数据不是一个有效值,请重新输入。", 64, "信息提示"
Else
 Res = Log(Ang + Sqr(Ang * Ang + 1))
txtDisplay.Text = Str$(Res) 
End If
End Sub
Private Sub CmdHATan_Click()


Dim St As String
Dim Ang As Double
Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
If Ang = 1 Or Ang = -1 Or ((Ang + 1) * (Ang - 1)) < 0 Then
 MsgBox "所输入的数据不是一个有效值,请重新输入。", 64, "信息提示"
Else
 Res = Log((Ang + 1) * (Ang - 1)) / 2
 txtDisplay.Text = Str$(Res)
End If
End Sub
Private Sub CmdHCos_Click()
Dim St As String
Dim Ang As Double
Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
Res = (Exp(Ang) + Exp(-Ang)) / 2
txtDisplay.Text = Str$(Res)
End Sub
Private Sub CmdHCsc_Click()
Dim St As String
Dim Ang As Double
Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
If (Exp(Ang) - Exp(-Ang)) = 0 Then
 MsgBox "所输入的数据不是一个有效值,请重新输入。", 64, "信息提示"
Else
 Res = 2 / (Exp(Ang) - Exp(-Ang))
 txtDisplay.Text = Str$(Res)
End If
End Sub
Private Sub CmdHCTan_Click()
Dim St As String
Dim Ang As Double
Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
If (Exp(Ang) - Exp(-Ang)) = 0 Then
 MsgBox "所输入的数据不是一个有效值,请重新输入。", 64, "信息提示"
Else
Res = (Exp(Ang) + Exp(-Ang)) / (Exp(Ang) - Exp(-Ang))
txtDisplay.Text = Str$(Res)
End If
End Sub
Private Sub CmdHSec_Click()
Dim St As String
Dim Ang As Double
Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
Res = 2 / (Exp(Ang) + Exp(-Ang))
txtDisplay.Text = Str$(Res)
End Sub
Private Sub cmdHSin_Click()
Dim St As String
Dim Ang As Double
Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
Res = (Exp(Ang) - Exp(-Ang)) / 2
txtDisplay.Text = Str$(Res)
End Sub
Private Sub CmdHTan_Click()
Dim St As String
Dim Ang As Double
Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
Res = (Exp(Ang) - Exp(-Ang)) / (Exp(Ang) + Exp(-Ang))
txtDisplay.Text = Str$(Res)
End Sub
Private Sub CmdLog_Click()
Dim St As String
Dim Ang As Double
Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
If Ang <= 0 Or Ang = 1 Then
 MsgBox "所输入的值不是有效值", 64, "信息提示"
  
Else
 Res = Log(Ang)
 txtDisplay.Text = Str$(Res) 
End If
End Sub
Private Sub CmdLogN_Click()
Dim St1 As String
Dim St2 As String
Dim Ang1 As Double
Dim Ang2 As Double
Dim Res As Double
If txtDisplay.Text = "" Then
MsgBox "请先输入真数:", 64, "信息提示"
Else
 St1 = txtDisplay.Text
Ang1 = Val(St1)
MsgBox "现在请输入底数N", 64, "信息提示"
txtDisplay.Text = ""
St2 = InputBox("请输入底数N的值:")
Ang2 = Val(St2)
If Ang1 <= 0 Or Ang2 <= 0 Or Ang1 = 1 Or Ang2 = 1 Then
MsgBox "所输入的值不是有效值", 64, "信息提示"
Else
 Res = Log(Ang1) / Log(Ang2)
 txtDisplay.Text = Str$(Res)
 
End If
End If
End Sub

Private Sub CmdSec_Click()
Dim Ang As Double
Dim Res As Double
Ang = Val(txtDisplay.Text)
Res = Round(Cos(Ang * 3.14159265358979 / 180), 10)
If Res = 0 Then
MsgBox "你所输入的数据不在该公式的定义域内,请重新输入。", 64, "信息提示"
Else
Res = 1 / Res
txtDisplay.Text = ""
txtDisplay.Text = Str$(Res)
End If
End Sub

Private Sub cmdSin_Click()
Dim Ang As Double
Dim Res As Double
Ang = Val(txtDisplay.Text)
Res = Round(Sin(Ang * 3.14159265358979 / 180), 10)
txtDisplay.Text = ""
txtDisplay.Text = Str$(Res)
End Sub

Private Sub CmdSqr_Click()
Dim Str1 As String
Dim Valu As Double
Dim Res As Double
Str1 = txtDisplay.Text
Valu = Val(Str1)
If Valu < 0 Then
MsgBox "被开方数不能小于O!!!", 48, "警告"
Else


Res = Sqr(Valu)
End If
txtDisplay.Text = ""
txtDisplay.Text = Str$(Res)

End Sub

Private Sub CmdTan_Click()
Dim Ang As Double
Dim Res As Double
Ang = Val(txtDisplay.Text)

Res = Round(Tan(Ang * 3.14159265358979 / 180), 10)
txtDisplay.Text = ""
txtDisplay.Text = Str$(Res)
End Sub

Private Sub Command1_Click()
Dim St As String
Dim Ang As Double
Dim Res As Double
St = txtDisplay.Text
Ang = Val(St)
Res = Atn(Ang) + 2 * Atn(1)

End Sub

Private Sub Timer1_Timer()
StatusBar1.Panels(4).Text = "当前时间:" & Now
End Sub
Function ActiveButton()
Text1.Text = ""
Text2.Text = ""
AquaButton1.Enabled = True
Text1.Enabled = True
End Function



' Remove the last character.
Private Sub DeleteCharacter()
Dim txt As String
Dim min_len As Integer

txt = txtDisplay.Text
If Left$(txt, 1) = "-" Then
min_len = 2
Else
min_len = 1
End If

If Len(txt) > min_len Then
txtDisplay.Text = Left$(txt, Len(txt) - 1)
Else
txtDisplay.Text = "0"
End If
End Sub

' Clear the current entry, saved value, and operator.
Private Sub cmdClear_Click()
cmdClearEntry_Click
StoredValue = 0
Operator = opNone
End Sub

' Clear the current entry.
Private Sub cmdClearEntry_Click()
txtDisplay.Text = ""
End Sub

' Add a decimal point to the display.


热点排行