2024-07-27 09:57:21
首先,需建立如下控件:
标签:Label1
命令按钮:
名称 Caption值
Cback ←
Cclear CE
Cstart C
Csin Sin
Ccos Cos
Csqrt Sqrt
Csign +/-
Cpoint Cpoint
Cequal =
控件数组:(命令按钮)
Calcu(1) +
Calcu(2) -
Calcu(3) *
Calcu(4) /
Cnum(0) 0
Cnum(1) 1
...
Cnum(9) 9
然后在代码窗口写源码:
Option Explicit
Dim Num1, Num2 As Double
Dim PointIn As Boolean
Dim Inputing As Boolean
Dim Operation1 As Integer
Private Sub Calcu_Click(i As Integer)
If Inputing = False Then '在前次运算提交之后尚未输入新的数据
Operation1 = i '运算符重置
Num1 = Label1 '将显示栏里的数据赋值给第一个操作数
Exit Sub
End If
Inputing = False '将当前状态置为非输入数据阶段
If Num1 <> 0 Then '非首次计算
Num2 = Label1 '将显示栏里的数据赋值给第二个操作数
Calculate (Operation1) '计算前一次运算并显示结果
Else '首次计算
Num1 = Label1 '将显示栏里的数据赋值给第一个操作数
End If
Operation1 = i '提交运算符
End Sub
Private Sub Cback_Click()
Dim TheLen As Integer
TheLen = Len(Label1)
If TheLen > 2 Then
If Right(Label1, 1) = "." Then
Label1 = Left(Label1, TheLen - 2) & "."
Else
Label1 = Left(Label1, TheLen - 1)
End If
Else
Cclear_Click
End If
End Sub
Private Sub Cclear_Click()
Label1 = "0."
Num2 = 0
PointIn = False
End Sub
Private Sub Ccos_Click()
Calculate (6)
Inputing = False
End Sub
Private Sub Cequal_Click()
If Inputing Then '如果刚输入过数据'Or Operation1 > 0 Then
Num2 = Label1 '将显示栏里的数据赋值给第二个操作数
End If
Inputing = False '将当前状态置为非输入数据阶段
Calculate (Operation1) '计算提交的运算并显示结果
Num1 = 0
Operation1 = 0
End Sub
Private Sub Calculate(Oprt As Integer)
Select Case Oprt
Case 1
Num1 = Num1 + Num2
ShowResult (Num1)
Case 2
Num1 = Num1 - Num2
ShowResult (Num1)
Case 3
Num1 = Num1 * Num2
ShowResult (Num1)
Case 4
Num1 = Num1 / Num2
ShowResult (Num1)
Case 5
Num2 = Label1
Num1 = Sin(Num2)
ShowResult (Num1)
Case 6
Num2 = Label1
Num1 = Cos(Num2)
ShowResult (Num1)
Case 7
Num2 = Label1
Num1 = Sqr(Num2)
ShowResult (Num1)
End Select
End Sub
Private Sub ShowResult(Num As Double)
If Num = Fix(Num) Then '整数
Label1 = Num & "."
ElseIf Left(Num, 1) = "." Then '第一个字符为小数点
Label1 = "0" & Num
ElseIf Left(Num, 2) = "-." Then '前两个字符为"-."
Label1 = "-0." & Right(CStr(Num), Len(CStr(Num)) - 2)
Else
Label1 = Num
End If
End Sub
Private Sub Cnum_Click(Index As Integer)
NumInput (Index)
End Sub
Private Sub Cpoint_Click()
If Inputing = False Then
Label1 = "0."
Inputing = True
End If
PointIn = True
End Sub
Private Sub Csign_Click()
If Label1 <> "0." Then
Dim StrTemp As String
StrTemp = Label1
If Left(StrTemp, 1) = "-" Then
Label1 = Right(StrTemp, Len(StrTemp) - 1)
Else
Label1 = "-" & StrTemp
End If
End If
End Sub
Private Sub Csin_Click()
Calculate (5)
Inputing = False
End Sub
Private Sub Csqrt_Click()
Dim x As Long
x = Label1
If x >= 0 Then
Calculate (7)
Else
Label1 = "Error!"
End If
Inputing = False
End Sub
Private Sub Cstart_Click()
Label1 = "0."
Num1 = 0
Num2 = 0
PointIn = False
Inputing = True
Operation1 = 0
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Cequal.SetFocus
If KeyCode = 46 Then '按Del键
Cclear_Click
End If
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
'键入数字:
Case 48 To 57
NumInput (KeyAscii - 48)
Case 46 '小数点
Cpoint_Click
'键入运算符:
Case 43 '加号
Calcu_Click (1)
Case 45 '减号
Calcu_Click (2)
Case 42 '乘号
Calcu_Click (3)
Case 47 '除号
Calcu_Click (4)
Case 27 '重新开始(退出键)
Cstart_Click
Case 8 '退格
Cback_Click
Case 13 '等于(回车键)
Cequal_Click
End Select
End Sub
Private Sub Form_Load()
Me.Top = (Screen.Height - Me.Height) / 2
Me.Left = (Screen.Width - Me.Width) / 2
Me.KeyPreview = True
Cstart_Click
End Sub
Private Sub NumInput(n As Integer)
If Len(Label1) > 15 Then
Exit Sub
End If
If Inputing = False Then
Cclear_Click
Label1 = n & "."
Inputing = True
ElseIf Label1 <> "0." Then
If Right(Label1, 1) = "." Then
If PointIn = False Then
Dim TheLen As Integer
TheLen = Len(Label1)
Label1 = Left(Label1, TheLen - 1)
Label1 = Label1 & n & "."
Else
Label1 = Label1 & n
End If
Else
Label1 = Label1 & n
End If
Else
If PointIn Then
Label1 = Label1 & n
Else
Label1 = n & "."
End If
End If
End Sub
注:如图所示,
我依次给你解释一下吧:最上面那个模拟显示屏的控件是标签Label1,下面的控件全是命令按钮:第一排的三个按钮←、CE、C的名称分别是Cback、Cclear、Cstart;第二排的sin,cos,Sqrt的名称分别是:Csin、Ccos、Csqrt;下面的所有数字0,1,2,...,9这十个数字是一个控件数组,类型也是命令按钮,名称为Cnum,下标Index分别是0,1,2,...,9,与它们代表的数字一一对应;四个运算符+,-,*,/也是一个命令按钮数组,名称为Calcu,下标分别为1,2,3,4;最下面一排的+/1,小数点,=的名称分别是:Csign、Cpoint、Cequal。代码就按我给你写的复制到代码窗口就可以了。
2024-07-27 10:34:37
'val 转换变量为实数
'caption 空间/窗体的标题?
Dim Js(0) As String'定义变量,存储运算符
Private Sub Command1_Click()
Js(0) = "+" '给JS(0)赋值为“+”
End Sub
Private Sub Command2_Click()
Js(0) = "-" '给js(0)赋值为:-
End Sub
Private Sub Command3_Click()
Js(0) = "*" '给js(0)赋值为:×
End Sub
Private Sub Command4_Click()
Js(0) = "/" '给JS(0)赋值为:÷
End Sub
Private Sub Command5_Click()
On Error GoTo Errah '如果出现错误就跳转到Errah
Select Case Js(0) '判断js(0)
Case "+" '如果js(0) = "+"
Text3.Text = Val(Text2.Text) + Val(Text1.Text)
'转换text2和text3的text为实数后相加
Case "-"
Text3.Text = Val(Text1.Text) - Val(Text2.Text)
'转换text2和text3的text为实数后相减
Case "*"
Text3.Text = Val(Text1.Text) * Val(Text2.Text)
'转换text2和text3的text为实数后相乘
Case "/"
Text3.Text = Val(Text1.Text) / Val(Text2.Text)
'转换text2和text3的text为实数后相除
End Select '结束判断
If Text3.Text = "" Then
Exit Sub
End If
MsgBox "计算结果为:" & Text3.Text
Exit Sub
Errah:
MsgBox "错误"
End Sub
Private Sub Form_Load()
Text3.Text = ""
Text2.Text = ""
Command1.Caption = "+"
Command2.Caption = "-"
Command3.Caption = "*"
Command4.Caption = "/"
Command5.Caption = "="
Text1.Text = ""
Me.Caption = "计算器"
End Sub
2024-07-27 08:04:07
' --------------------------------------------------------------------------
Option Explicit
Dim Op1, Op2 ' 前面输入的操作数
Dim DecimalFlag As Integer ' 小数点仍然存在吗?
Dim NumOps As Integer ' 操作数个数
Dim LastInput ' 指示上一次按键事件的类型
Dim OpFlag ' 指示未完成的操作
Dim TempReadout
' C (取消) 按钮的 Click 事件过程
' 重新设置显示并初始化变量
Private Sub Cancel_Click()
Readout = Format(0, "0.")
Op1 = 0
Op2 = 0
Form_Load
End Sub
' CE (取消输入) 按钮的 Click 事件过程
Private Sub CancelEntry_Click()
Readout = Format(0, "0.")
DecimalFlag = False
LastInput = "CE"
End Sub
' 小数点 (.) 按钮的 Click 事件过程
' 如果上一次按键为运算符,初始化 readout 为 "0.";
' 否则显示时追加一个小数点
Private Sub Decimal_Click()
If LastInput = "NEG" Then
Readout = Format(0, "-0.")
ElseIf LastInput <> "NUMS" Then
Readout = Format(0, "0.")
End If
DecimalFlag = True
LastInput = "NUMS"
End Sub
' 窗体的初始化过程
' 设置所有变量为其初始值
Private Sub Form_Load()
DecimalFlag = False
NumOps = 0
LastInput = "NONE"
OpFlag = " "
Readout = Format(0, "0.")
'Decimal.Caption = Format(0, ".")
End Sub
' 数字键 (0-9) 的 Click 事件过程
' 向显示中的数追加新数
Private Sub Number_Click(Index As Integer)
If LastInput <> "NUMS" Then
Readout = Format(0, ".")
DecimalFlag = False
End If
If DecimalFlag Then
Readout = Readout + Number(Index).Caption
Else
Readout = Left(Readout, InStr(Readout, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".")
End If
If LastInput = "NEG" Then Readout = "-" & Readout
LastInput = "NUMS"
End Sub
' 运算符 (+, -, x, /, =) 的 Click 事件过程
' 如果接下来的按键是数字键,增加 NumOps。
' 如果有一个操作数,则设置 Op1。
' 如果有两个操作数,则将 Op1 设置为 Op1 与
' 当前输入字符串的运算结果,并显示结果
Private Sub Operator_Click(Index As Integer)
TempReadout = Readout
If LastInput = "NUMS" Then
NumOps = NumOps + 1
End If
Select Case NumOps
Case 0
If Operator(Index).Caption = "-" And LastInput <> "NEG" Then
Readout = "-" & Readout
LastInput = "NEG"
End If
Case 1
Op1 = Readout
If Operator(Index).Caption = "-" And LastInput <> "NUMS" And OpFlag <> "=" Then
Readout = "-"
LastInput = "NEG"
End If
Case 2
Op2 = TempReadout
Select Case OpFlag
Case "+"
Op1 = CDbl(Op1) + CDbl(Op2)
Case "-"
Op1 = CDbl(Op1) - CDbl(Op2)
Case "X"
Op1 = CDbl(Op1) * CDbl(Op2)
Case "/"
If Op2 = 0 Then
MsgBox "除数不能为 0", 48, "计算器"
Else
Op1 = CDbl(Op1) / CDbl(Op2)
End If
Case "="
Op1 = CDbl(Op2)
Case "%"
Op1 = CDbl(Op1) * CDbl(Op2)
End Select
Readout = Op1
NumOps = 1
End Select
If LastInput <> "NEG" Then
LastInput = "OPS"
OpFlag = Operator(Index).Caption
End If
End Sub
' 百分比键 (%) 的 Click 事件过程
' 计算并显示第一个操作数的百分数
Private Sub Percent_Click()
Readout = Readout / 100
LastInput = "Ops"
OpFlag = "%"
NumOps = NumOps + 1
DecimalFlag = True
End Sub