| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1646 人关注过本帖
标题:vb 计算器制作问题
只看楼主 加入收藏
狂晕
该用户已被删除
收藏
已结贴  问题点数:20 回复次数:7 
vb 计算器制作问题
提示: 作者被禁止或删除 内容自动屏蔽
搜索更多相关主题的帖子: 计算器 制作 
2010-05-16 17:03
绺子
Rank: 2
等 级:论坛游民
帖 子:4
专家分:17
注 册:2010-5-18
收藏
得分:14 
不会   看别人
2010-05-18 15:56
gshill
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-5-12
收藏
得分:0 
以前自己写的一个,可能参考一下。
Option Explicit
Dim strtemp As String     
Dim i, j, k As Integer     
Dim blnNM As Boolean      
Dim blnEnd As Boolean      
Dim KeyASCII As Integer


Option Base 1              
Dim strNum(100) As String  
Dim intNz As Integer      
Dim intOpt(100)  As Integer  
Dim intOptt As Integer     
Dim intM(100), intMM As Integer
Dim intMz As Integer      



Private Sub cmdBack_Click()           

If Not blnNM And Len(strtemp) > 0 Then
   strtemp = Left(strtemp, Len(strtemp) - 1)
   TxtDisplay.Text = Left(TxtDisplay.Text, Len(TxtDisplay.Text) - 1)
End If

If blnNM Then                        
  intMz = intMz - 1
  TxtDisplay.Text = Left(TxtDisplay.Text, Len(TxtDisplay.Text) - 1)
End If

End Sub

Private Sub cmdCalc_Click(Index As Integer)

intOptt = Index

  
     Select Case Index               
       Case 0
         intMM = 1
         TxtDisplay.Text = TxtDisplay.Text & "+"
       Case 1
         intMM = 1
         TxtDisplay.Text = TxtDisplay.Text & "-"
        Case 2
          intMM = 2
          TxtDisplay.Text = TxtDisplay.Text & "*"
        Case 3
          intMM = 2
          TxtDisplay.Text = TxtDisplay.Text & "/"
        Case 4
          intMM = 3
          TxtDisplay.Text = TxtDisplay.Text & "("
        Case 5
          intMM = 0
          TxtDisplay.Text = TxtDisplay.Text & ")"
    End Select
   
 If blnEnd Then blnEnd = False

 Call PushM
  
   If Not blnNM And Not blnEnd And Len(strtemp) <> 0 Then      
     Call PushN   '入栈
     blnNM = True
  End If
  
 If intNz > 1 Then Call operationA

End Sub

Private Sub cmdClear_Click()

TxtDisplay.Text = ""
intNz = 0
intMz = 0
strtemp = ""
blnEnd = False
blnNM = False

End Sub

Private Sub cmdEnter_Click()   
blnEnd = True

If Not blnNM Then Call PushN   

 Do While intMz > 0 And intNz > 1
   Call operationA
Loop

intMz = 0

TxtDisplay.Text = TxtDisplay.Text & "=" & strNum(intNz)

End Sub

Private Sub cmdNumber_Click(Index As Integer)   
blnEnd = False
   If blnNM Then
    blnNM = False
   End If
   
  If Index = 11 And Len(strtemp) = 0 Then
       strtemp = "-"
    Else
       If Index = 11 And Len(strtemp) >= 1 Then
         If Index = 11 And Len(strtemp) > 1 Then
           TxtDisplay.Text = Left(TxtDisplay.Text, Len(TxtDisplay.Text) - Len(strtemp))
           strtemp = "-"
         Else
           If Index = 11 And Len(strtemp) = 1 And strtemp = "-" Then
             TxtDisplay.Text = Left(TxtDisplay.Text, Len(TxtDisplay.Text) - Len(strtemp))
             strtemp = ""
             Else
             TxtDisplay.Text = Left(TxtDisplay.Text, Len(TxtDisplay.Text) - Len(strtemp))
             strtemp = "-"
           End If
         End If
         
       End If
  End If
  
    If Index < 10 Then        
       strtemp = strtemp + CStr(Index)  
      Else                     
       If InStr(strtemp, ".") = 0 And Index = 10 Then strtemp = strtemp + "."
    End If
   
  If Len(strtemp) > 1 And Left(strtemp, 1) = "0" And Mid(strtemp, 2, 1) <> "." Then
     strtemp = Right(strtemp, Len(strtemp) - 1)     
  End If
  TxtDisplay.Text = TxtDisplay.Text & Right(strtemp, 1)
End Sub
Public Sub operationA()  

If intMz > 1 Then
  If intM(intMz) = 0 And intM(intMz - 1) = 3 Then intMz = intMz - 2
End If

  If intNz > 1 And intMz > 1 Then
      If intM(intMz - 1) >= intM(intMz) And intM(intMz - 1) <> 3 And intM(intMz - 1) <> 0 And Not blnEnd Then Call operationB  
      If blnEnd And intM(intMz) <> 0 Then Call operationC
      If blnEnd And intM(intMz) = 0 Then Call operationCC
  End If
  If intNz > 1 And intMz > 0 And blnEnd And intM(intMz) <> 0 Then Call operationC
  
  If intMz > 1 Then
    If intM(intMz) = 0 And intM(intMz - 1) = 3 Then intMz = intMz - 2  
  End If
End Sub
Public Sub operationCC()      
Select Case intOpt(intMz - 1)
  Case 0
  strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) + Val(strNum(intNz)))
  Case 1
  strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) - Val(strNum(intNz)))
  Case 2
  strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) * Val(strNum(intNz)))
  Case 3
    If Val(strNum(intNz)) <> 0 Then
        strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) / Val(strNum(intNz)))
    Else
              If Val(strNum(intNz)) = 0 Then MsgBox "除数不能为0!", 16, "计算器"
    End If
  End Select
   Call PopN
   Call PopM
End Sub
Public Sub operationC()      
Select Case intOpt(intMz)
  Case 0
  strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) + Val(strNum(intNz)))
  Case 1
  strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) - Val(strNum(intNz)))
  Case 2
  strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) * Val(strNum(intNz)))
  Case 3
    If Val(strNum(intNz)) <> 0 Then
        strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) / Val(strNum(intNz)))
    Else
              If Val(strNum(intNz)) = 0 Then MsgBox "除数不能为0!", 16, "计算器"
    End If
  End Select
  
   Call PopN
   Call PopMend
End Sub
Public Sub operationB()      
Select Case intOpt(intMz - 1)
  Case 0
  strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) + Val(strNum(intNz)))
  Case 1
  strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) - Val(strNum(intNz)))
  Case 2
  strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) * Val(strNum(intNz)))
  Case 3
    If Val(strNum(intNz)) <> 0 Then
        strNum(intNz - 1) = CStr(Val(strNum(intNz - 1)) / Val(strNum(intNz)))
    Else
              If Val(strNum(intNz)) = 0 Then MsgBox "除数不能为0!", 16, "计算器"
    End If
  End Select
   intOpt(intMz - 1) = intOpt(intMz)
   intM(intMz - 1) = intM(intMz)
   Call PopN
   Call PopM
End Sub

Public Sub InitStack()                    
   intNz = 0
   intMz = 0
End Sub
Public Sub PushN()                                             
   If intNz < 100 Then      
      intNz = intNz + 1
      strNum(intNz) = strtemp
   Else
      TxtDisplay.Text = "栈满"
   End If
 strtemp = ""
End Sub
Public Sub PushM()                                             
   If intMz < 100 Then      
      intMz = intMz + 1
      intOpt(intMz) = intOptt
      intM(intMz) = intMM
   Else
      TxtDisplay.Text = "栈满"
   End If
End Sub
Public Sub PopN()                                               
   If intNz > 0 Then
      intNz = intNz - 1
   Else
      TxtDisplay.Text = "栈空"
   End If
End Sub
Public Sub PopM()                                             
   If intMz > 1 Then
      intOpt(intMz - 1) = intOpt(intMz)
      intM(intMz - 1) = intM(intMz)
      intMz = intMz - 1
   End If
End Sub
Public Sub PopMend()                                            
   If intMz > 0 Then
      intMz = intMz - 1
   End If
End Sub

Private Sub form_Keypress(KeyASCII As Integer)
If KeyASCII < 58 And KeyASCII > 47 Then cmdNumber_Click (Chr$(KeyASCII))
If KeyASCII = 46 Then cmdNumber_Click (10)
If KeyASCII = 95 Then cmdNumber_Click (11)
If KeyASCII = 61 Then cmdCalc_Click (0)
If KeyASCII = 45 Then cmdCalc_Click (1)
If KeyASCII = 42 Then cmdCalc_Click (2)
If KeyASCII = 47 Then cmdCalc_Click (3)
If KeyASCII = 40 Then cmdCalc_Click (4)
If KeyASCII = 41 Then cmdCalc_Click (5)
If KeyASCII = 8 Then cmdBack_Click
If KeyASCII = 0 Then cmdEnter_Click
End Sub
Private Sub Form_Load()
TxtDisplay.Text = ""
intNz = 0
intMz = 0
End Sub
Private Sub MenuAbout_Click()
formAbout.Show
End Sub
Private Sub MenuHelp_Click()
Formhelp.Show
End Sub
Private Sub calculator_Keypress(KeyASCII As Integer)   
If KeyASCII = 13 Then
KeyASCII = 0
Call cmdEnter_Click
End If
End Sub
2010-05-20 17:17
烦死人了
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-4-17
收藏
得分:0 
回复 楼主 狂晕
用标签控件作为显示器,然后输入的时候用+号就可以把连续输入的字符连接起来,要计算时用val()函数把标签控件的caption属性转换为数值型
2010-05-23 12:29
cyy814645654
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-5-15
收藏
得分:0 
回复 3楼 gshill
太复杂了
2010-05-31 11:37
狂晕
该用户已被删除
收藏
得分:0 
回复 3楼 gshill
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-31 13:51
liangge2012
Rank: 2
等 级:论坛游民
帖 子:14
专家分:10
注 册:2011-3-17
收藏
得分:0 
这个会不会太复杂啦?
2012-03-23 14:21
a15515354572
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-11-25
收藏
得分:0 
我试了下 出现的是变量为定义,,求问是怎么回事,,初学者 求写个不是太难的
2012-11-25 13:12
快速回复:vb 计算器制作问题
数据加载中...
 
   



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

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