VB编写简单计算器程序

VB编写简单计算器程序
VB编写简单计算器程序

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 Command2.Value = True

If UCase(CHAR) = "C" Then Command3.Value = True

If keynum = 27 Then Command4.Value = True

If keynum = 61 Then Command6.Value = True

keynum = 0

End Sub

Function angle(ByVal j1 As Integer) As Single

angle = j1

If Option1.Value 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 Option1.Value 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 Option1.Value 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(Text1.Text)

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

'Option3.Value = 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

'Option4.Value = True

Exit Function

Err:

Text1.Text = "数据太大,溢出!"

End Function

Private Sub Command1_Click(Index As Integer)

'当按下数字键(0-9)时,向文本框尾部追加数据

'并通过变量LastInput记录上次按键为数字键

If Len(Text1.Text) > 16 Then Exit Sub

If Text1.Text = "0" Or LastInput = "Eqv" Then Text1.Text = ""

Text1.Text = Text1.Text & 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(Text1.Text) > 16 Or InStr(1, Text1.Text, ".") > 0 _

And LastInput <> "Eqv" Then Exit Sub

'如果以"."开始输入新数据,在"."前加"0";

'如果在数据输入过程中按下".",直接将"."追加在数据尾部

If LastInput = "Opt" Or LastInput = "Eqv" Or LastInput = "Neg" Then Text1.Text = Text1.Text + "0."

Else

Text1.Text = Text1.Text + "."

End If

LastInput = "Num"

Command1(0).SetFocus

End Sub

Private Sub Command3_Click()

'按下"C"(取消) 按钮的Click 事件过程

'重新设置并初始化变量。

Num1 = 0

Num2 = 0

Text1.Text = "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 Text1.Text = "-"

LastInput = "Neg"

Exit Sub

End If

'按下"C"(取消)键后,单击"-","-"作为负号使用

If LastInput = "Nul" And Command5(Index).Caption = "-" Then Text1.Text = "-"

LastInput = "Neg"

OptType = -1

Exit Sub

End If

'按下运算符键后,获取第一个操作数Num1,并记录按下的运算符Num1 = Val(Text1.Text)

If shuzhi = 2 Then Num1 = n2to10(Num1)

Text1.Text = ""

LastInput = "Opt"

OptType = Index

Command1(0).SetFocus

FirstNum = False

End Sub

Private Sub Command6_Click()

'单击"="按钮时,计算并显示结果

Num2 = Val(Text1.Text)

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

Text1.Text = "EEEE.EEEE"

Else

Result = Num1 / Num2

End If

End Select

If shuzhi = 2 Then Result = n10to2(Result)

If Text1.Text <> "EEEE.EEEE" Then Text1.Text = 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(Text1.Text)

If shuzhi = 2 Then Num1 = n2to10(Num1)

Select Case Index

Case 0 'Sin(x)

Text1.Text = Sin(angle(Num1))

Case 1 'Cos(x)

Text1.Text = Cos(angle(Num1))

Case 2 'Tan(x)

Text1.Text = Tan(angle(Num1))

Case 3 'ASin(x)

If Abs(Num1) <= 1 Then

Text1.Text = ArcSin(Num1)

Else

Text1.Text = "EEEE.EEEE"

End If

Case 4

If Abs(Num1) <= 1 Then

Text1.Text = ArcCos(Num1)

Else

Text1.Text = "EEEE.EEEE"

End If

Case 5

Text1.Text = Atn(Num1)

If Option1.Value Then Text1.Text = Atn(Num1) * 180 / 3.1415926 End Select

If Option3.Value Then Text1.Text = n10to2(Text1.Text)

Num1 = 0

Num2 = 0

LastInput = "Eqv"

OptType = -1

Command1(0).SetFocus

FirstNum = True

End Sub

Private Sub Command8_Click(Index As Integer)

Num1 = Val(Text1.Text)

If shuzhi = 2 Then Num1 = n2to10(Num1)

Select Case Index

Case 0 'E^x

Text1.Text = 2.7182 ^ Num1

Case 1 'LN(X)

If Num1 <= 0 Then

Text1.Text = "EEEE.EEEE"

Else

Text1.Text = Log(Num1)

End If

Case 2 'LOG(X)

If Num1 <= 0 Then

Text1.Text = "EEEE.EEEE"

Else

Text1.Text = Log(Num1) / Log(10)

End If

Case 3 'Sqr(x)

If Num1 < 0 Then

Text1.Text = "EEEE.EEEE"

Else

Text1.Text = Sqr(Num1)

End If

Case 4 '1/X

If Num1 = 0 Then

Text1.Text = "EEEE.EEEE"

Else

Text1.Text = 1 / Num1

End If

Case 5 'N!

If Num1 < 0 Then

Text1.Text = "EEEE.EEEE"

Else

Text1.Text = jiecheng(Int(Num1))

End If

End Select

If shuzhi = 2 Then Text1.Text = n10to2(Val(Text1.Text))

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

Text1.Text = "0"

Text1.Locked = True

shuzhi = 10

Option1.Value = True

Option4.Value = True

Text1.Locked = 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(Text1.Text)

Text1.Text = n10to2(Num1)

Else

Num2 = Val(Text1.Text)

Text1.Text = 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(Text1.Text)

Text1.Text = n2to10(Num1)

Else

Num2 = Val(Text1.Text)

Text1.Text = n2to10(Num2)

End If

FirstNum = True

If j > 0 Then Command1(0).SetFocus j = j + 1

End Sub

相关主题
相关文档
最新文档