vb 做计算器 界面如图所示
发布网友
发布时间:2022-04-22 07:59
我来回答
共3个回答
热心网友
时间:2023-07-22 12:04
http://icese.net/icetool/other/vb_calc.rar追问大侠啊,按我的图写一个好吗
热心网友
时间:2023-07-22 12:04
还可以啊
热心网友
时间:2023-07-22 12:05
命令按钮:
名称 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。代码就按我给你写的复制到代码窗口就可以了。追问大侠啊,按我的图写一个好吗