永发信息网

【vb计算器代码】在VB中计算器的代码(要求0到9的加减乘除运算)只要0...

答案:2  悬赏:80  手机版
解决时间 2021-03-03 02:22
【vb计算器代码】在VB中计算器的代码(要求0到9的加减乘除运算)只要0...
最佳答案
【答案】 Dim Num1 As Double
  Dim Num2 As Double
  Dim Equal As Double
  Dim M As Double
  Dim Equal2 As Integer
  Dim StrNum1 As String
  Dim StrNum2 As String
  Dim FirstPoint As Boolean
  Dim FirstNum As Boolean
  Private Sub Command1_Click(Index As Integer) '数字输入
  Select Case Index
  Case 0 To 9
  StrNum1 = LTrim(StrNum1) + LTrim(Str(Index))
  Do While Left(LTrim(StrNum1), 1) = 0 '去掉高位数字0
  StrNum1 = Mid(StrNum1, 2)
  Loop
  If Left(LTrim(StrNum1), 1) = . Then StrNum1 = 0 + LTrim(StrNum1)
  Text1.Text = StrNum1 '数字显示
  Case 10
  If FirstPoint Then '小数点的输入
  StrNum1 = LTrim(StrNum1) + LTrim(.)
  Do While Left(LTrim(StrNum1), 1) = 0 '去掉高位数字0
  StrNum1 = Mid(StrNum1, 2)
  Loop
  If Left(LTrim(StrNum1), 1) = . Then StrNum1 = 0 + LTrim(StrNum1)
  Text1.Text = StrNum1 '数字显示
  FirstPoint = False
  Text1.Text = StrNum1
  End If
  Case 11
  StrNum1 = Text1.Text '退格
  If Len(StrNum1) > 0 Then
  StrNum1 = Left(StrNum1, Len(StrNum1) - 1) '每一次退格,从字符串后面去掉一个字符
  Text1.Text = StrNum1
  Else
  Text1.Text = 0.0
  End If
  If InStr(StrNum1, .) = 0 Then FirstPoint = True '若字符串中没有小数点,把小数点开关打开
  Case 12 '清零
  StrNum1 =
  StrNum2 =
  Text1.Text = 0.0
  aa =
  Equal = 0
  Num1 = 0
  Num2 = 0
  FirstPoint = True '小数点开关
  FirstNum = True '第一个数开关
  Case 13 '正负符号
  If Val(StrNum1) > 0 Then
  StrNum1 = - + LTrim(StrNum1)
  Else
  StrNum1 = Abs(Val(StrNum1))
  End If
  Text1.Text = StrNum1
  Case 14 '电源开关
  End
  End Select
  End Sub
  Private Sub Command2_Click(Index As Integer) '+-*/运算
  Equal2 = Index '=号开关
  If FirstNum Then '存第一个数,其中FirstNum为开关
  StrNum2 = StrNum1
  StrNum1 =
  FirstNum = False
  FirstPoint = True '小数点开,以便输入第二个数
  Else '若不是第一个数进入下面的+-*/运算
  Select Case Index
  Case 0 '0为加法
  Num1 = Val(StrNum2) 'Num1存第一个数
  Num2 = Val(StrNum1) 'Num2存第二个数
  Equal = Num1 + Num2 'Equal存结果
  StrNum2 = Str(Equal) 'StrNum2返回结果,以便下次让Num1存结果
  StrNum1 = 'StrNum1清空,以便下次数字输入
  aa = Str(Equal)
  If Left(LTrim(aa), 1) = - And Left(LTrim(aa), 2) = -. Then aa = -0 + LTrim(Abs(Equal))
  If Left(LTrim(aa), 1) = . Then aa = 0 + LTrim(aa)
  Text1.Text = aa '显示结果
  FirstPoint = True '小数点开
  Case 1 '1为减法
  Num1 = Val(StrNum2)
  Num2 = Val(StrNum1)
  Equal = Num1 - Num2
  StrNum2 = Str(Equal)
  StrNum1 =
  aa = Str(Equal)
  If Left(LTrim(aa), 1) = - And Left(LTrim(aa), 2) = -. Then aa = -0 + LTrim(Abs(Equal))
  If Left(LTrim(aa), 1) = . Then aa = 0 + LTrim(aa)
  Text1.Text = aa
  FirstPoint = True
  Case 2 '为乖法
  Num1 = Val(StrNum2)
  If StrNum1 = Then '便于+-*/之间切换
  Num2 = 1
  Else
  Num2 = Val(StrNum1)
  End If
  Equal = Num1 * Num2
  StrNum2 = Str(Equal)
  StrNum1 =
  aa = Str(Equal)
  If Left(LTrim(aa), 1) = - And Left(LTrim(aa), 2) = -. Then aa = -0 + LTrim(Abs(Equal))
  If Left(LTrim(aa), 1) = . Then aa = 0 + LTrim(aa)
  Text1.Text = aa
  FirstPoint = True
  Case 3 '3为除法
  Num1 = Val(StrNum2) 'Num1存第一个数
  If StrNum1 = Then
  Num2 = 1
  Else
  Num2 = Val(StrNum1)
  End If
  Equal = Num1 / Num2
  StrNum2 = Str(Equal) 'Num2存第二个数
  StrNum1 =
  aa = Str(Equal)
  If Left(LTrim(aa), 1) = - And Left(LTrim(aa), 2) = -. Then aa = -0 + LTrim(Abs(Equal))
  If Left(LTrim(aa), 1) = . Then aa = 0 + LTrim(aa)
  Text1.Text = aa
  FirstPoint = True
  End Select
  End If
  End Sub
  Private Sub Command3_Click() '=号运算
  FirstNum = True '第一个数关,关掉连继+-*/
  Select Case Equal2 'Equal2为=号按键判断+-*/哪种运算
  Case 0 '+运算
  If StrNum2 = Then
  Num1 = Equal
  Else
  Num1 = Val(StrNum2)
  End If
  Num2 = Val(Text1.Text)
  Equal = Num1 + Num2
  StrNum1 = '返回结果
  aa = Str(Equal)
  If Left(LTrim(aa), 1) = - And Left(LTrim(aa), 2) = -. Then aa = -0 + LTrim(Abs(Equal))
  If Left(LTrim(aa), 1) = . Then aa = 0 + LTrim(aa)
  Text1.Text = aa '显示结果
  FirstPoint = True '小数点开
  Case 1 '-运算
  If StrNum2 = Then
  Num1 = Equal
  Else
  Num1 = Val(StrNum2)
  End If
  Num2 = Val(Text1.Text)
  Equal = Num1 - Num2
  StrNum1 =
  aa = Str(Equal)
  If Left(LTrim(aa), 1) = - And Left(LTrim(aa), 2) = -. Then aa = -0 + LTrim(Abs(Equal))
  If Left(LTrim(aa), 1) = . Then aa = 0 + LTrim(aa)
  Text1.Text = aa
  FirstPoint = True
  Case 2 '*运算
  If StrNum2 = Then
  Num1 = Equal
  Else
  Num1 = Val(StrNum2)
  End If
  If StrNum1 = Then
  Num2 = 1
  Else
  Num2 = Val(Text1.Text)
  End If
  Equal = Num1 * Num2
  StrNum1 =
  aa = Str(Equal)
  If Left(LTrim(aa), 1) = - And Left(LTrim(aa), 2) = -. Then aa = -0 + LTrim(Abs(Equal))
  If Left(LTrim(aa), 1) = . Then aa = 0 + LTrim(aa)
  Text1.Text = aa
  FirstPoint = True
  Case 3 '/运算
  If StrNum2 = Then
  Num1 = Equal
  Else
  Num1 = Val(StrNum2)
  End If
  If StrNum1 = Then
  Num2 = 1
  Else
  Num2 = Val(Text1.Text)
  End If
  Equal = Num1 / Num2
  StrNum1 =
  aa = Str(Equal)
  If Left(LTrim(aa), 1) = - And Left(LTrim(aa), 2) = -. Then aa = -0 + LTrim(Abs(Equal))
  If Left(LTrim(aa), 1) = . Then aa = 0 + LTrim(aa)
  Text1.Text = aa
  FirstPoint = True
  End Select
  End Sub
  Private Sub Command4_Click() '开方运算
  aa = Val(Text1.Text)
  StrNum1 =
  If aa >= 0 Then
  aa = Str(Sqr(aa))
  If Left(LTrim(aa), 1) = . Then aa = 0 + LTrim(aa)
  Text1.Text = aa
  Else
  MsgBox 被开方数少于零
  End If
  End Sub
  Private Sub Command7_Click() '倒数运算
  aa = Val(Text1.Text)
  StrNum1 =
  aa = Str(1 / aa)
  If Left(LTrim(aa), 1) = - And Left(LTrim(aa), 2) = -. Then aa = -0 + LTrim(Abs(Equal))
  If Left(LTrim(aa), 1) = . Then aa = 0 + LTrim(aa)
  Text1.Text = aa
  End Sub
  Private Sub Command8_Click(Index As Integer) '存贮器操作
  Select Case Index
  Case 0 'MC,存贮器清零
  Text2.Text =
  M = 0
  Case 1 'MR,显示存贮器内容
全部回答
感谢回答,我学习了
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
高一九班体操比赛的口号(16字)
请问浙江爱德消防设备 模块能不能编至200以上
益母草膏饭前还是饭后
以开机一会诊断卡显示8e报警长鸣一声,然后自
米兰·新娘国际摄影机构地址在什么地方,想过
常见的观赏植物40种,观赏植物的作用有哪些,
我想知道东风风神AX7发动机质量怎么样
北京协和医院北门我想知道这个在什么地方
She' s form America
千阳县华通电动工具行我想知道这个在什么地方
如何评价B站UP主碴条以及他的行为
苛刻什么意思
阿伟潮汕饭店地址在哪,我要去那里办事
吉利博越和哈弗H6 Coupe这两个车里选择一个,
(本题10分)如图1, MA 1 ∥ NA 2 ,则∠ A
推荐资讯
有人比徐英豪更帅吗
我的电脑配置:AMD 速龙64 X2 4000+.主板型号
高速能走应急车道嘛?车没机油了?40码上不去
fiddler怎么没有连上手机
【billiejean】billiejean中英文翻译
lol反复回到登录界面
龙一大药房成华区西林路药店地址在哪,我要去
牛小杂地址在哪,我要去那里办事
凌源市杨杖子农村信用合作社杨杖子分社地址在
契约电价是什么意思?超价电价又是什么意思?
水立方门票多少钱
个人信用贷款的特点有()。A.准入条件宽松B.准
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?