利用VB编程设计一个类似于Windows附件中的计算器程序,要求可进行简单的算术运算、三角函数和自然对数运算 20
展开全部
Sub op()
T1.Enabled = True
T1.Visible = True
T1.SetFocus
End Sub
Sub oi()
T1.Enabled = False
T1.Visible = False
End Sub
Private Sub Command1_Click()
If O1 Then
T5 = T1 + T3
ElseIf O2 Then
T5 = T1 - T3
ElseIf O3 Then
T5 = T1 * T3
ElseIf O4 Then
If T3 = 0 Then
MsgBox "除数不能为零"
T3 = ""
T3.SetFocus
Else
T5 = T1 / T3
End If
ElseIf O5 Then
T5 = Sin(T3 * 3.141592653 / 180)
ElseIf O6 Then
T5 = Cos(T3 * 3.141592653 / 180)
ElseIf O7 Then
T5 = Tan(T3 * 3.141592653 / 180)
ElseIf O8 Then
T5 = 1 / Tan(T3 * 3.141592653 / 180)
ElseIf O9 Then
If T3 < 0 Then
MsgBox "被开方数不能小于零", 2 + vbExclamation, "错误"
T3 = ""
T3.SetFocus
Exit Sub
End If
T5 = Sqr(T3)
ElseIf O10 Then
T5 = Abs(T3)
ElseIf O11 Then
T5 = Exp(T3)
ElseIf O12 Then
If T3 <= 0 Then
T3 = ""
T3.SetFocus
End If
T5 = Log(T3)
End If
End Sub
Private Sub Form_Load()
Height = Screen.Height
Width = Screen.Width
Move 0, 0
End Sub
Private Sub O1_Click()
Call op
T2 = O1.Caption
End Sub
Private Sub O10_Click()
Call oi
T2 = O10.Caption
T3.SetFocus
End Sub
Private Sub O11_Click()
Call oi
T2 = O11.Caption
T3.SetFocus
End Sub
Private Sub O12_Click()
Call oi
T2 = O12.Caption
T3.SetFocus
End Sub
Private Sub O2_Click()
Call op
T2 = O2.Caption
End Sub
Private Sub O3_Click()
Call op
T2 = O3.Caption
End Sub
Private Sub O4_Click()
Call op
T2 = O4.Caption
End Sub
Private Sub O5_Click()
Call oi
T2 = O5.Caption
T3.SetFocus
End Sub
Private Sub O6_Click()
Call oi
T2 = O6.Caption
T3.SetFocus
End Sub
Private Sub O7_Click()
Call oi
T2 = O7.Caption
T3.SetFocus
End Sub
Private Sub O8_Click()
Call oi
T2 = O8.Caption
T3.SetFocus
End Sub
Private Sub O9_Click()
Call oi
T2 = O9.Caption
T3.SetFocus
End Sub
Private Sub T2_Change()
T1 = ""
T5 = ""
T3 = ""
End Sub
O1,O2,,,,,,,O12都是单选按钮,分别对应不同的计算符号 + - * / sin cos tan cot sqr abs exp log ,T1,,,,T5是文本框
T1.Enabled = True
T1.Visible = True
T1.SetFocus
End Sub
Sub oi()
T1.Enabled = False
T1.Visible = False
End Sub
Private Sub Command1_Click()
If O1 Then
T5 = T1 + T3
ElseIf O2 Then
T5 = T1 - T3
ElseIf O3 Then
T5 = T1 * T3
ElseIf O4 Then
If T3 = 0 Then
MsgBox "除数不能为零"
T3 = ""
T3.SetFocus
Else
T5 = T1 / T3
End If
ElseIf O5 Then
T5 = Sin(T3 * 3.141592653 / 180)
ElseIf O6 Then
T5 = Cos(T3 * 3.141592653 / 180)
ElseIf O7 Then
T5 = Tan(T3 * 3.141592653 / 180)
ElseIf O8 Then
T5 = 1 / Tan(T3 * 3.141592653 / 180)
ElseIf O9 Then
If T3 < 0 Then
MsgBox "被开方数不能小于零", 2 + vbExclamation, "错误"
T3 = ""
T3.SetFocus
Exit Sub
End If
T5 = Sqr(T3)
ElseIf O10 Then
T5 = Abs(T3)
ElseIf O11 Then
T5 = Exp(T3)
ElseIf O12 Then
If T3 <= 0 Then
T3 = ""
T3.SetFocus
End If
T5 = Log(T3)
End If
End Sub
Private Sub Form_Load()
Height = Screen.Height
Width = Screen.Width
Move 0, 0
End Sub
Private Sub O1_Click()
Call op
T2 = O1.Caption
End Sub
Private Sub O10_Click()
Call oi
T2 = O10.Caption
T3.SetFocus
End Sub
Private Sub O11_Click()
Call oi
T2 = O11.Caption
T3.SetFocus
End Sub
Private Sub O12_Click()
Call oi
T2 = O12.Caption
T3.SetFocus
End Sub
Private Sub O2_Click()
Call op
T2 = O2.Caption
End Sub
Private Sub O3_Click()
Call op
T2 = O3.Caption
End Sub
Private Sub O4_Click()
Call op
T2 = O4.Caption
End Sub
Private Sub O5_Click()
Call oi
T2 = O5.Caption
T3.SetFocus
End Sub
Private Sub O6_Click()
Call oi
T2 = O6.Caption
T3.SetFocus
End Sub
Private Sub O7_Click()
Call oi
T2 = O7.Caption
T3.SetFocus
End Sub
Private Sub O8_Click()
Call oi
T2 = O8.Caption
T3.SetFocus
End Sub
Private Sub O9_Click()
Call oi
T2 = O9.Caption
T3.SetFocus
End Sub
Private Sub T2_Change()
T1 = ""
T5 = ""
T3 = ""
End Sub
O1,O2,,,,,,,O12都是单选按钮,分别对应不同的计算符号 + - * / sin cos tan cot sqr abs exp log ,T1,,,,T5是文本框
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-07-01
展开全部
Option Explicit
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 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
'清除显示内容,保存运算符
Private Sub cmdClear_Click()
cmdClearEntry_Click
StoredValue = 0
Operator = opNone
End Sub
'清除显示内容
Private Sub cmdClearEntry_Click()
txtDisplay.Text = ""
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 cmdEquals_Click()
Dim new_value As Double
If txtDisplay.Text = "" Then
new_value = 0
Else
new_value = CDbl(txtDisplay.Text)
End If
Select Case Operator
Case opNone
StoredValue = new_value
Case opAdd
StoredValue = StoredValue + new_value
Case opSubtract
StoredValue = StoredValue - new_value
Case opMultiply
StoredValue = StoredValue * new_value
Case opDivide
StoredValue = StoredValue / new_value
End Select
Operator = opNone
NewEntry = True
txtDisplay.Text = Format$(StoredValue)
End Sub
' 显示数字
Private Sub cmdNumber_Click(Index As Integer)
If NewEntry Then
txtDisplay.Text = Format$(Index)
NewEntry = False
Else
txtDisplay.Text = txtDisplay.Text & Format$(Index)
End If
End Sub
Private Sub cmdOperator_Click(Index As Integer)
cmdEquals_Click
Operator = Index
NewEntry = True
End Sub
'改变符号
Private Sub cmdPlusMinus_Click()
If NewEntry Then
txtDisplay.Text = "-"
ElseIf Left$(txtDisplay.Text, 1) = "-" Then
txtDisplay.Text = Right$(txtDisplay.Text, 2)
Else
txtDisplay.Text = "-" & txtDisplay.Text
End If
End Sub
'处理键盘按键
Private Sub Form_KeyPress(KeyAscii As Integer)
txtDisplay_KeyPress KeyAscii
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
txtDisplay_KeyUp KeyCode, Shift
End Sub
Private Sub txtDisplay_Change()
txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub
Private Sub txtDisplay_GotFocus()
txtDisplay_Change
End Sub
Private Sub txtDisplay_KeyPress(KeyAscii As Integer)
Dim ch As String
ch = Chr$(KeyAscii)
Select Case ch
Case "0"
cmdNumber_Click 0
Case "1"
cmdNumber_Click 1
Case "2"
cmdNumber_Click 2
Case "3"
cmdNumber_Click 3
Case "4"
cmdNumber_Click 4
Case "5"
cmdNumber_Click 5
Case "6"
cmdNumber_Click 6
Case "7"
cmdNumber_Click 7
Case "8"
cmdNumber_Click 8
Case "9"
cmdNumber_Click 9
Case "*", "x", "X"
cmdOperator_Click opMultiply
Case "+"
cmdOperator_Click opAdd
Case vbCrLf, vbCr, "="
cmdEquals_Click
Case "-"
cmdOperator_Click opSubtract
Case "."
cmdDecimal_Click
Case "/"
cmdOperator_Click opDivide
Case "C", "c"
cmdClearEntry_Click
End Select
KeyAscii = 0
End Sub
Private Sub txtDisplay_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyNumpad0
cmdNumber_Click 0
Case vbKeyNumpad1
cmdNumber_Click 1
Case vbKeyNumpad2
cmdNumber_Click 2
Case vbKeyNumpad3
cmdNumber_Click 3
Case vbKeyNumpad4
cmdNumber_Click 4
Case vbKeyNumpad5
cmdNumber_Click 5
Case vbKeyNumpad6
cmdNumber_Click 6
Case vbKeyNumpad7
cmdNumber_Click 7
Case vbKeyNumpad8
cmdNumber_Click 8
Case vbKeyNumpad9
cmdNumber_Click 9
Case vbKeyMultiply
cmdOperator_Click opMultiply
Case vbKeyAdd
cmdOperator_Click opAdd
Case vbKeySeparator
cmdEquals_Click
Case vbKeySubtract
cmdOperator_Click opSubtract
Case vbKeyDecimal
cmdDecimal_Click
Case vbKeyDivide
cmdOperator_Click opDivide
Case vbKeyBack, vbKeyDelete
DeleteCharacter
End Select
KeyCode = 0
End Sub
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 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
'清除显示内容,保存运算符
Private Sub cmdClear_Click()
cmdClearEntry_Click
StoredValue = 0
Operator = opNone
End Sub
'清除显示内容
Private Sub cmdClearEntry_Click()
txtDisplay.Text = ""
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 cmdEquals_Click()
Dim new_value As Double
If txtDisplay.Text = "" Then
new_value = 0
Else
new_value = CDbl(txtDisplay.Text)
End If
Select Case Operator
Case opNone
StoredValue = new_value
Case opAdd
StoredValue = StoredValue + new_value
Case opSubtract
StoredValue = StoredValue - new_value
Case opMultiply
StoredValue = StoredValue * new_value
Case opDivide
StoredValue = StoredValue / new_value
End Select
Operator = opNone
NewEntry = True
txtDisplay.Text = Format$(StoredValue)
End Sub
' 显示数字
Private Sub cmdNumber_Click(Index As Integer)
If NewEntry Then
txtDisplay.Text = Format$(Index)
NewEntry = False
Else
txtDisplay.Text = txtDisplay.Text & Format$(Index)
End If
End Sub
Private Sub cmdOperator_Click(Index As Integer)
cmdEquals_Click
Operator = Index
NewEntry = True
End Sub
'改变符号
Private Sub cmdPlusMinus_Click()
If NewEntry Then
txtDisplay.Text = "-"
ElseIf Left$(txtDisplay.Text, 1) = "-" Then
txtDisplay.Text = Right$(txtDisplay.Text, 2)
Else
txtDisplay.Text = "-" & txtDisplay.Text
End If
End Sub
'处理键盘按键
Private Sub Form_KeyPress(KeyAscii As Integer)
txtDisplay_KeyPress KeyAscii
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
txtDisplay_KeyUp KeyCode, Shift
End Sub
Private Sub txtDisplay_Change()
txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub
Private Sub txtDisplay_GotFocus()
txtDisplay_Change
End Sub
Private Sub txtDisplay_KeyPress(KeyAscii As Integer)
Dim ch As String
ch = Chr$(KeyAscii)
Select Case ch
Case "0"
cmdNumber_Click 0
Case "1"
cmdNumber_Click 1
Case "2"
cmdNumber_Click 2
Case "3"
cmdNumber_Click 3
Case "4"
cmdNumber_Click 4
Case "5"
cmdNumber_Click 5
Case "6"
cmdNumber_Click 6
Case "7"
cmdNumber_Click 7
Case "8"
cmdNumber_Click 8
Case "9"
cmdNumber_Click 9
Case "*", "x", "X"
cmdOperator_Click opMultiply
Case "+"
cmdOperator_Click opAdd
Case vbCrLf, vbCr, "="
cmdEquals_Click
Case "-"
cmdOperator_Click opSubtract
Case "."
cmdDecimal_Click
Case "/"
cmdOperator_Click opDivide
Case "C", "c"
cmdClearEntry_Click
End Select
KeyAscii = 0
End Sub
Private Sub txtDisplay_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyNumpad0
cmdNumber_Click 0
Case vbKeyNumpad1
cmdNumber_Click 1
Case vbKeyNumpad2
cmdNumber_Click 2
Case vbKeyNumpad3
cmdNumber_Click 3
Case vbKeyNumpad4
cmdNumber_Click 4
Case vbKeyNumpad5
cmdNumber_Click 5
Case vbKeyNumpad6
cmdNumber_Click 6
Case vbKeyNumpad7
cmdNumber_Click 7
Case vbKeyNumpad8
cmdNumber_Click 8
Case vbKeyNumpad9
cmdNumber_Click 9
Case vbKeyMultiply
cmdOperator_Click opMultiply
Case vbKeyAdd
cmdOperator_Click opAdd
Case vbKeySeparator
cmdEquals_Click
Case vbKeySubtract
cmdOperator_Click opSubtract
Case vbKeyDecimal
cmdDecimal_Click
Case vbKeyDivide
cmdOperator_Click opDivide
Case vbKeyBack, vbKeyDelete
DeleteCharacter
End Select
KeyCode = 0
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我有现成的,老外编的,想要就留个邮箱,但是不要提问题。
追问
764707055@qq.com 谢谢
追答
已发
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询