VB。求简单计算器的代码。

VB。求简单计算器的代码。
2024-11-29 18:23:15
推荐回答(2个)
回答(1):

'4个选项按钮,3个文本框和1个命令按钮
Private Sub AddOption_Click()
SymbelLabel.Caption = "+" '改变运算符号为+
OutputText.Text = "" '将结果输出框设为空
End Sub

Private Sub DivideOption_Click()
SymbelLabel.Caption = "/" '改变运算符号为/
OutputText.Text = "" '将结果输出框设为空
End Sub

Private Sub MinusOption_Click()
SymbelLabel.Caption = "-" '改变运算符号为-
OutputText.Text = "" '将结果输出框设为空
End Sub

Private Sub MultiOption_Click()
SymbelLabel.Caption = "*" '改变运算符号为*
OutputText.Text = "" '将结果输出框设为空
End Sub

Private Sub RunCommand_Click()
Dim Number1, Number2, Result As Single '变量声明

Number1 = Val(InputText1.Text)
Number2 = Val(InputText2.Text) '获取计算数据

If AddOption.Value = True Then
Result = Number1 + Number2
End If
If MinusOption.Value = True Then
Result = Number1 - Number2
End If
If MultiOption.Value = True Then
Result = Number1 * Number2
End If
If DivideOption.Value = True Then
Result = Number1 / Number2
End If '判断是哪种运算,并计算

OutputText.Text = Result '结果显示

End Sub

回答(2):

VERSION 5.00
Begin VB.Form Form1
Caption = "模拟计算器"
ClientHeight = 2940
ClientLeft = 60
ClientTop = 450
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 2940
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command2
Caption = "结束"
Height = 375
Left = 2640
TabIndex = 5
Top = 2160
Width = 1215
End
Begin VB.CommandButton Command1
Caption = "清除"
Height = 375
Left = 1080
TabIndex = 4
Top = 2160
Width = 1095
End
Begin VB.TextBox Text2
Height = 495
Left = 2040
TabIndex = 3
Top = 1320
Width = 1935
End
Begin VB.TextBox Text1
Height = 495
Left = 2040
TabIndex = 1
Top = 360
Width = 1935
End
Begin VB.Label Label2
Caption = "结 果:"
Height = 375
Left = 720
TabIndex = 2
Top = 1440
Width = 735
End
Begin VB.Label Label1
Caption = "输入运算式:"
Height = 375
Left = 600
TabIndex = 0
Top = 480
Width = 1335
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
Text1 = ""
Text2 = ""
Text1.SetFocus
End Sub

Private Sub Command2_Click()
End
End Sub

Private Sub Text1_LostFocus()
Dim x As String, p As String
Dim a!, b!, y!, n%
x = Text1
a = Val(x)
n = Len(LTrim(a))
p = Mid(x, n + 1, 1)
b = Val(Mid(x, n + 2))
If p = "+" Then
y = a + b
ElseIf p = "-" Then
y = a - b
ElseIf p = "*" Then
y = a * b
ElseIf p = "/" Then
y = a / b
Else
End If
Text2 = y
End Sub