admin 管理员组文章数量: 887021
2024年1月23日发(作者:integer什么意思c)
Option Explicit
Dim LastInput As String * 3 '记录上次按下的按键
Dim Num1 As Double '第一个操作数
Dim Num2 As Double '第二个操作数
Dim OptType As Integer '按下哪一个操作符
Dim Result As Double '表示运算结果
Dim shuzhi As Integer '表示当前采用的shuzhi
Dim FirstNum As Boolean '是否是第一个操作数
Sub keyp(keynum As Integer)
Dim CHAR As String * 1
CHAR = Chr(keynum)
If CHAR = "+" Or keynum = 43 Then Command5(0).Value = True
If CHAR = "-" Or keynum = 45 Then Command5(1).Value = True
If CHAR = "*" Or keynum = 42 Then Command5(2).Value = True
If CHAR = "/" Or keynum = 47 Then Command5(3).Value = True
If shuzhi = 2 And CHAR >= "2" And CHAR <= "9" Then
keynum = 0
Exit Sub
End If
If keynum >= 48 And keynum <= 57 Then Command1(keynum - 48).Value = True
If keynum = 46 Then = True
If UCase(CHAR) = "C" Then = True
If keynum = 27 Then = True
If keynum = 61 Then = True
keynum = 0
End Sub
Function angle(ByVal j1 As Integer) As Single
angle = j1
If Then angle = j1 * 3.14 / 180
End Function
Function ArcSin(ByVal Num As Single) As Single
If Num = 1 Then
ArcSin = 3.1415926 / 2
ElseIf Num = -1 Then
ArcSin = 3.1415926 * 3 / 2
Else
ArcSin = Atn(Num / Sqr(-Num * Num + 1))
End If
If Then ArcSin = ArcSin * 180 / 3.1415926
End Function
Function ArcCos(ByVal Num As Single) As Single
If Num = 1 Then
ArcCos = 0
ElseIf Num = -1 Then
ArcCos = 3.1415926
Else
ArcCos = Atn(-Num / Sqr(-Num * Num + 1)) + 2 * Atn(1)
End If
If Then ArcCos = ArcCos * 180 / 3.1415926
End Function
Function jiecheng(ByVal n As Integer) As Single
Dim COUNT As Integer
jiecheng = 1
For COUNT = 1 To n
jiecheng = jiecheng * COUNT
Next
End Function
Function n10to2(ByVal Number As Single) As Single
Dim IntN As Long 'Number的整数部分
Dim FracN As Single 'Number的小数部分
Dim ModN As Integer '整数部分换算时,记录余数
Dim RltN As String '换算结果
Dim i As Integer
If InStr(Number, "e") > 0 Or InStr(Number, "E") > 0 Then
MsgBox "不能转换以科学记数法表示的数据!"
Exit Function
End If
ModN = 0
'Number = Val()
IntN = Int(Number)
FracN = Number - IntN
'以下代码用于将十进制的整数部分换算为二进制
Do While IntN > 0
ModN = IntN Mod 2
IntN = IntN 2
RltN = ModN & RltN
Loop
RltN = RltN & "."
i = 1
'以下代码用于将十进制的小数部分换算为二进制
Do While i <= 7 Or FracN <> 0
FracN = FracN * 2
If FracN >= 1 Then
FracN = FracN - 1
RltN = RltN & "1"
Else
RltN = RltN & "0"
End If
i = i + 1
Loop
n10to2 = RltN
' = True
End Function
Function n2to10(ByVal Number As Double) As Single
Dim i As Integer, j As Integer
Dim IntN As Long, FracN As Single
Dim RltN As Single
Dim POS As Integer '记录小数点位置
If InStr(Number, "e") > 0 Or InStr(Number, "E") > 0 Then
MsgBox "不能转换以科学记数法表示的数据!"
Exit Function
End If
On Error GoTo Err
IntN = Int(Number)
FracN = Number - IntN
Do While IntN > 0 '换算整数部分
RltN = RltN + (IntN Mod 10) * 2 ^ j
j = j + 1
IntN = IntN 10
Loop
POS = InStr(1, Str(FracN), ".")
j = -1
For i = POS + 1 To Len(Str(FracN)) '换算小数部分
RltN = RltN + 2 ^ j * Val(Mid(Str(FracN), i, 1))
j = j - 1
Next i
n2to10 = RltN
' = True
Exit Function
Err:
= "数据太大,溢出!"
End Function
Private Sub Command1_Click(Index As Integer)
'当按下数字键(0-9)时,向文本框尾部追加数据
'并通过变量LastInput记录上次按键为数字键
If Len() > 16 Then Exit Sub
If = "0" Or LastInput = "Eqv" Then = ""
= & Index '追加数据
LastInput = "Num"
Command1(0).SetFocus
End Sub
Private Sub Command1_KeyPress(Index As Integer, KeyAscii As Integer)
Call keyp(KeyAscii)
End Sub
Private Sub Command2_Click()
'按下小数点按钮的处理过程
'如果数据位数超出范围,或数据中已包含小数点,退出本过程
If Len() > 16 Or InStr(1, , ".") > 0 _
And LastInput <> "Eqv" Then Exit Sub
'如果以"."开始输入新数据,在"."前加"0";
'如果在数据输入过程中按下".",直接将"."追加在数据尾部
If LastInput = "Opt" Or LastInput = "Eqv" Or LastInput = "Neg" Then
= + "0."
Else
= + "."
End If
LastInput = "Num"
Command1(0).SetFocus
End Sub
Private Sub Command3_Click()
'按下"C"(取消) 按钮的 Click 事件过程
'重新设置并初始化变量。
Num1 = 0
Num2 = 0
= "0"
OptType = -1
LastInput = "Nul"
Command1(0).SetFocus
FirstNum = True
End Sub
Private Sub Command4_Click()
'单击"OFF"键退出程序
End
End Sub
Private Sub Command5_Click(Index As Integer)
'单击运算符(+、-、*、/)按钮的事件过程
'按下运算符后,单击"-","-"作为负号使用
If LastInput = "Opt" And Command5(Index).Caption = "-" Then
= "-"
LastInput = "Neg"
Exit Sub
End If
'按下"C"(取消)键后,单击"-","-"作为负号使用
If LastInput = "Nul" And Command5(Index).Caption = "-" Then
= "-"
LastInput = "Neg"
OptType = -1
Exit Sub
End If
'按下运算符键后,获取第一个操作数Num1,并记录按下的运算符
Num1 = Val()
If shuzhi = 2 Then Num1 = n2to10(Num1)
= ""
LastInput = "Opt"
OptType = Index
Command1(0).SetFocus
FirstNum = False
End Sub
Private Sub Command6_Click()
'单击"="按钮时,计算并显示结果
Num2 = Val()
If shuzhi = 2 Then Num2 = n2to10(Num2)
Select Case OptType
Case 0 '加法
Result = Num1 + Num2
Case 1 '减法
Result = Num1 - Num2
Case 2 '乘法
Result = Num1 * Num2
Case 3 '除法
If Num2 = 0 Then
= ""
Else
Result = Num1 / Num2
End If
End Select
If shuzhi = 2 Then Result = n10to2(Result)
If <> "" Then = Trim(Str(Result)) '显示结果
Num1 = 0
Num2 = 0
LastInput = "Eqv"
OptType = -1
Command1(0).SetFocus
FirstNum = True
End Sub
Private Sub Command7_Click(Index As Integer)
On Error Resume Next
Num1 = Val()
If shuzhi = 2 Then Num1 = n2to10(Num1)
Select Case Index
Case 0 'Sin(x)
= Sin(angle(Num1))
Case 1 'Cos(x)
= Cos(angle(Num1))
Case 2 'Tan(x)
= Tan(angle(Num1))
Case 3 'ASin(x)
If Abs(Num1) <= 1 Then
= ArcSin(Num1)
Else
= ""
End If
Case 4
If Abs(Num1) <= 1 Then
= ArcCos(Num1)
Else
= ""
End If
Case 5
= Atn(Num1)
If Then = Atn(Num1) * 180 / 3.1415926
End Select
If Then = n10to2()
Num1 = 0
Num2 = 0
LastInput = "Eqv"
OptType = -1
Command1(0).SetFocus
FirstNum = True
End Sub
Private Sub Command8_Click(Index As Integer)
Num1 = Val()
If shuzhi = 2 Then Num1 = n2to10(Num1)
Select Case Index
Case 0 'E^x
= 2.7182 ^ Num1
Case 1 'LN(X)
If Num1 <= 0 Then
= ""
Else
= Log(Num1)
End If
Case 2 'LOG(X)
If Num1 <= 0 Then
= ""
Else
= Log(Num1) / Log(10)
End If
Case 3 'Sqr(x)
If Num1 < 0 Then
= ""
Else
= Sqr(Num1)
End If
Case 4 '1/X
If Num1 = 0 Then
= ""
Else
= 1 / Num1
End If
Case 5 'N!
If Num1 < 0 Then
= ""
Else
= jiecheng(Int(Num1))
End If
End Select
If shuzhi = 2 Then = n10to2(Val())
Num1 = 0
Num2 = 0
LastInput = "Eqv"
OptType = -1
Command1(0).SetFocus
FirstNum = True
End Sub
Private Sub Form_Load()
'加载窗体,变量初始化
LastInput = "Nul"
Num1 = 0
Num2 = 0
OptType = -1
= "0"
= True
shuzhi = 10
= True
= True
= True
End Sub
Private Sub Option1_Click()
Static i As Integer
If i > 0 Then Command1(0).SetFocus
i = i + 1
End Sub
Private Sub Option2_Click()
Command1(0).SetFocus
End Sub
Private Sub Option3_Click()
Dim i As Integer
shuzhi = 2
For i = 2 To 9
Command1(i).Enabled = False
Next i
If FirstNum Then
Num1 = Val()
= n10to2(Num1)
Else
Num2 = Val()
= n10to2(Num2)
End If
Command1(0).SetFocus
End Sub
Private Sub Option4_Click()
Dim i As Integer
Static j As Integer
If shuzhi = 2 Then
For i = 2 To 9
Command1(i).Enabled = True
Next i
shuzhi = 10
End If
If FirstNum Then
Num1 = Val()
= n2to10(Num1)
Else
Num2 = Val()
= n2to10(Num2)
End If
FirstNum = True
If j > 0 Then Command1(0).SetFocus
j = j + 1
End Sub
版权声明:本文标题:VB编写简单计算器程序 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1705987683h496826.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论