早教吧作业答案频道 -->其他-->
3.编写一个简易计算器,能进行加减乘除运算,并且能清除运算结果,有加减乘除,清除一共5个按钮,三
题目详情
3 .编写一个简易计算器,能进行加减乘除运算,并且能清除运算结果,有加 减 乘除 ,清除一共5个按钮,三
▼优质解答
答案和解析
我写的这段代码,有很多不严谨的地方,大体思路就是这样的
如果用多个文本框来输入的话,很容易实现,配合ComboBox进行运算法则的选择,就更简单了
Dim a As Double, b As Double, x As String
Private Sub Command1_Click(Index As Integer)'控件数组
If Text3.Text <> "" And IsNumeric(Text3.Text) = True Then
'文本框不为空,并且输入的是数字
Select Case Index '判断按下了 控件数组中的哪个键
Case 0
a = Text3.Text
x = "+" '用变量x来存储要进行的计算类型
Label1.Caption = a & x '用标签显示输入结果
Text3.Text = "0"
Text3.SetFocus
Text3.SelStart = 0 '文本框接受焦点,并且文本
Text3.SelLength = Len(Text3.Text) '为选定状态,方便输入
Case 1
a = Text3.Text
x = "-"
Label1.Caption = a & x
Text3.Text = "0"
Text3.SetFocus
Text3.SelStart = 0
Text3.SelLength = Len(Text3.Text)
Case 2
a = Text3.Text
x = "*"
Label1.Caption = a & x
Text3.Text = "0"
Text3.SetFocus
Text3.SelStart = 0
Text3.SelLength = Len(Text3.Text)
Case 3
a = Text3.Text
x = "/"
Label1.Caption = a & x
Text3.Text = "0"
Text3.SetFocus
Text3.SelStart = 0
Text3.SelLength = Len(Text3.Text)
Case 4
Text3.Text = ""
Label1.Caption = ""
Text3.SetFocus
a = b = c = 0
End Select
Else
Label1.Caption = "只允许输入数字"
End If
End Sub
Private Sub Command2_Click() '用按键“=”,输出最后结果
b = Text3.Text
If x = "+" Then Text3.Text = a + b
Label1.Caption = a & x & b & "=" & Text3.Text '标签进行最终显示,防止输入错误
If x = "-" Then Text3.Text = a - b
Label1.Caption = a & x & b & "=" & Text3.Text
If x = "*" Then Text3.Text = a * b
Label1.Caption = a & x & b & "=" & Text3.Text
If x = "/" Then
If b <> 0 Then Text3.Text = a / b Else: MsgBox "除数不能为零"
Label1.Caption = a & x & b & "=" & Text3.Text
End If
End Sub
如果用多个文本框来输入的话,很容易实现,配合ComboBox进行运算法则的选择,就更简单了
Dim a As Double, b As Double, x As String
Private Sub Command1_Click(Index As Integer)'控件数组
If Text3.Text <> "" And IsNumeric(Text3.Text) = True Then
'文本框不为空,并且输入的是数字
Select Case Index '判断按下了 控件数组中的哪个键
Case 0
a = Text3.Text
x = "+" '用变量x来存储要进行的计算类型
Label1.Caption = a & x '用标签显示输入结果
Text3.Text = "0"
Text3.SetFocus
Text3.SelStart = 0 '文本框接受焦点,并且文本
Text3.SelLength = Len(Text3.Text) '为选定状态,方便输入
Case 1
a = Text3.Text
x = "-"
Label1.Caption = a & x
Text3.Text = "0"
Text3.SetFocus
Text3.SelStart = 0
Text3.SelLength = Len(Text3.Text)
Case 2
a = Text3.Text
x = "*"
Label1.Caption = a & x
Text3.Text = "0"
Text3.SetFocus
Text3.SelStart = 0
Text3.SelLength = Len(Text3.Text)
Case 3
a = Text3.Text
x = "/"
Label1.Caption = a & x
Text3.Text = "0"
Text3.SetFocus
Text3.SelStart = 0
Text3.SelLength = Len(Text3.Text)
Case 4
Text3.Text = ""
Label1.Caption = ""
Text3.SetFocus
a = b = c = 0
End Select
Else
Label1.Caption = "只允许输入数字"
End If
End Sub
Private Sub Command2_Click() '用按键“=”,输出最后结果
b = Text3.Text
If x = "+" Then Text3.Text = a + b
Label1.Caption = a & x & b & "=" & Text3.Text '标签进行最终显示,防止输入错误
If x = "-" Then Text3.Text = a - b
Label1.Caption = a & x & b & "=" & Text3.Text
If x = "*" Then Text3.Text = a * b
Label1.Caption = a & x & b & "=" & Text3.Text
If x = "/" Then
If b <> 0 Then Text3.Text = a / b Else: MsgBox "除数不能为零"
Label1.Caption = a & x & b & "=" & Text3.Text
End If
End Sub

看了 3.编写一个简易计算器,能进...的网友还看了以下:
脱式计算。(能简便计算的要简便运算)396x1233.96x12.3568+4脱式计算。(能简便计 2020-04-07 …
下列说法正确的是()A.用计算器进行混合运算时,应先按键进行乘方运算,再按键进行乘除运算,最后按键 2020-05-14 …
下列说法正确的是()A.用计算器进行混合运算时,应先按键进行乘方运算,再按键进行乘除运算,最后按键 2020-05-14 …
简算(要用到运算律或其他简便算法,不可按顺序计算)-2^4÷[3^2-(3/4)×(-4/3)^2 2020-07-31 …
市政工程土方是按实方计算的,但挖运是按天然方计,挖运是否要按实际填方乘以1.15的系数计算挖运工程量 2020-11-07 …
计算0.32-2/7+0.77-5/7的最好方法是()a.按顺序计算b.运用加法结合律c.运用分配律 2020-11-27 …
编程实现一个计算器,按照四则运算优先级n编程实现一个简单计算器。请按照四则运算加、减、乘、除、幂(^ 2020-11-28 …
火车运送货物怎么收费的?有一批货物要从浙江义乌运到山东青岛,产品是弹力球,体积是21立方米,重量是1 2020-12-15 …
先阅读下面文字,然后按要求解题.例:1+2+3+……+100=分析:如果一个一个顺序相加显然太烦,我 2021-01-28 …
计算0.31—2/5十0.69-3/5最简便的方法是A.按顺序计算B.运用加法结合律C.运用加法交换 2021-01-29 …