| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 462 人关注过本帖
标题:请教一个简单计算器的编写方法
只看楼主 加入收藏
qifjk
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-7-27
结帖率:0
收藏
已结贴  问题点数:0 回复次数:2 
请教一个简单计算器的编写方法
我是新手,想知道一个计算器的大概编程思路,还有计算器设计中小数点和退格的处理方法,谢谢大家了
搜索更多相关主题的帖子: 计算器 编程 小数点 
2011-07-27 12:32
msgj
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:4
帖 子:190
专家分:914
注 册:2009-10-3
收藏
得分:20 
给你个代码例子,自己研究吧!
把控件先建好!
0-9 +-*/ . =
text1
代码
Dim Num1, Num2 As Single
Dim StrNum1, StrNum2 As String
Dim FirstNum As Boolean
Dim PointFlag As Boolean
Dim Runsign As Integer
Dim SignFlag As Boolean

Private Sub command1_Click(Index As Integer)
Select Case Index
  Case 0 To 9
    If FirstNum Then
      StrNum1 = str(Index)
      FirstNum = False
    Else
      StrNum1 = LTrim(StrNum1) + LTrim(str(Index))
    End If
    Text1.Text = Val(StrNum1)
  Case 10
    If Not PointFlag Then
      If FirstNum Then
        StrNum1 = "0."
        FirstNum = False
      Else
        StrNum1 = LTrim(StrNum1) + LTrim(".")
      End If
    Else
       Exit Sub
    End If
    PointFlag = True
    Text1.Text = Val(StrNum1)
  Case 12 To 15
    FirstNum = True
    PointFlag = False
    If SignFlag Then
      Call Run
    Else
      SignFlag = True
      StrNum2 = StrNum1
      StrNum1 = ""
    End If
    Runsign = Index - 11
  Case 11
    On Error Resume Next
    If Not SignFlag Then
      Text1.Text = StrNum1
      equal = Val(StrNum1)
      FirstNum = True
      PointFlag = False
    Else
      Call Run
      SignFlag = False
    End If
  Case Else
    Call ClearData
End Select
End Sub
Private Sub Form_Load()
Me.Top = 100
Me.Left = 8000
If App.PrevInstance Then MsgBox "请不要多次运行此程序,谢谢!", vbAbortRetryIgnore: Unload Me
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
End Sub
Sub Run()
  On Error Resume Next
  Dim equal As Single
  Dim equal2 As Double
  Num1 = Val(StrNum2)
  Num2 = Val(StrNum1)
  Select Case Runsign
    Case 1
      equal = Num1 + Num2
      If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
        equal = 0
        equal2 = Num1 + Num2
      End If
    Case 2
      equal = Num1 - Num2
      If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
        equal = 0
        equal2 = Num1 - Num2
      End If
    Case 3
      equal = Num1 * Num2
      If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
        equal = 0
        equal2 = Num1 * Num2
      End If
    Case 4
      equal = Num1 / Num2
      If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
        equal = 0
        equal2 = Num1 / Num2
      End If
  End Select
  If equal = 0 Then a = equal2 Else a = equal
  StrNum2 = str(a)
  StrNum1 = StrNum2
  Text1.Text = Val(StrNum2)
End Sub
Sub ClearData()
  Num1 = 0
  Num2 = 0
  StrNum1 = ""
  StrNum2 = ""
  FirstNum = True
  PointFlag = False
  Runsign = 0
  SignFlag = False
  Text1.Text = 0
End Sub
2011-07-31 08:17
司马飞鱼
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-8-7
收藏
得分:0 
Dim a As Boolean, b As Long, c As Integer '这里定义了a:记录是否按下“+-×÷=”,
                                                     '                 来确定按数字时屏幕是否要清除
                                                     '                 重新输入;
                                                     '         b:记录第一个计算数;
                                                     '            c:记录上一次按下的操作符号。

Private Sub AC_Click() 'AC按钮被按下,屏幕和变量清零。
    a = False
    b = 0
    c = 0
    Text1.Text = "0"
End Sub

Private Sub com_Click(Index As Integer) '计算器的功能减被按下。
    If Text1.Text = "E" Then Exit Sub '如果计算器发生错误(屏幕显示“E”)就退出过程。
   a = True
    Select Case c
        Case 1
           Text1.Text = b + Text1.Text
        Case 2
           Text1.Text = b - Text1.Text
        Case 3
            Text1.Text = b * Text1.Text
        Case 4
            If Text1.Text = 0 Then '对除数是否为零进行检测。
                Text1.Text = "E" '如果除数为零,就在屏幕显示“E”。
                Exit Sub
            Else
                Text1.Text = CInt(b \ Text1.Text) '将除法的结果转为整数。
            End If
    End Select
    b = Text1.Text '保存上一次的结果,以作为下一次计算的操作数。
    c = Index
End Sub

Private Sub Num_Click(Index As Integer)
    If Text1.Text = "E" Then Exit Sub '如果计算器发生错误(屏幕显示“E”)就退出过程。
    If a = False Then
        Text1.Text = Val(Text1.Text & Index)
    Else
        Text1.Text = Index
        a = False
    End If
End Sub
2011-08-07 12:09
快速回复:请教一个简单计算器的编写方法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029755 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved