zoukankan      html  css  js  c++  java
  • 【原创】VB利用堆栈实现算术表达式计算

    这个抽象算法早已为人所知,只不过在VB的公开文档中鲜见示例代码。于是,为了提高自己的程序设计水平,锻炼自己的能力,我写了如下代码。

    【VB代码版权所有,允许转载修改用作学习目的,转载必须注明来源

    【求大家看完后留言啊!】

    堆栈类的实现:

     1 Private Stack() As String
     2 Private itemCount As Long
     3 Private Sub Class_Initialize()
     4     ReDim Stack(0)
     5     Stack(0) = "#"
     6 End Sub
     7 Public Sub Push(ByVal inString As String)
     8     ReDim Preserve Stack(itemCount + 1)
     9     Stack(itemCount + 1) = inString
    10     itemCount = itemCount + 1
    11 End Sub
    12 
    13 Public Function Pop() As String
    14     If itemCount >= 1 Then
    15         Pop = Stack(itemCount)
    16         ReDim Preserve Stack(itemCount - 1)
    17         itemCount = itemCount - 1
    18     Else
    19         Pop = ""
    20     End If
    21 End Function
    22 Public Function Peek() As String
    23     If itemCount = 0 Then
    24         Peek = ""
    25         Exit Function
    26     End If
    27     Peek = Stack(itemCount)
    28 End Function
    29 
    30 Sub Clear()
    31     itemCount = 0
    32     ReDim Stack(itemCount)
    33     Stack(itemCount) = "#"
    34 End Sub
    35 
    36 Public Function Count()
    37     Count = itemCount
    38 End Function
    39 Public Function ViewStack() As String
    40     Dim kOut As String
    41     Dim i As Long
    42     If itemCount = 0 Then ViewStack = "": Exit Function
    43     For i = 1 To itemCount
    44         kOut = kOut & Format(i, "00") & " " & Stack(i) & vbCrLf
    45     Next i
    46     ViewStack = kOut
    47 End Function

    计算模块中的代码:

      1 Public opNum As New StackClass
      2 Public opChar As New StackClass
      3 Public Function CalcString(ByVal strIn As String) As String
      4     Dim sTxt As String
      5     Dim strNumFix As String
      6     Dim curChar As String
      7     Dim i As Long
      8     Dim signCount As Long
      9     Dim ops1 As String, ops2 As String, opC As String
     10     '初始化堆栈
     11         opNum.Clear
     12         opChar.Clear
     13     '堆栈初始化结束
     14     sTxt = strIn
     15     For i = 1 To Len(sTxt)
     16         curChar = Mid(sTxt, i, 1)
     17         If IsSymbol(curChar) = True Then
     18             '看看数字预备区有没有
     19             If strNumFix <> "" Then
     20                 opNum.Push strNumFix
     21                 strNumFix = ""
     22             End If
     23 redo:
     24             If IsHigh(curChar, opChar.Peek) = 1 Then 'if new come char is higher then push it to stack
     25                 opChar.Push curChar '如果等级高的控制符,则进入
     26                 signCount = signCount + 1
     27             ElseIf IsHigh(curChar, opChar.Peek) = 0 Then
     28                 If curChar = "#" And opChar.Peek = "#" Then
     29                     opChar.Pop
     30                     CalcString = opNum.Pop
     31                     Exit Function
     32                 End If
     33             ElseIf IsHigh(curChar, opChar.Peek) = -1 Then 'if low then ready to calculate
     34                 '判断是不是第一个符号
     35                 If signCount = 1 Then '这个符号是刚刚输入#后的那个,无论如何入栈
     36                     opChar.Push curChar
     37                     signCount = signCount + 1
     38                     GoTo nextone
     39                 End If
     40                 ops2 = opNum.Pop
     41                 ops1 = opNum.Pop
     42                 opC = opChar.Pop
     43                 opNum.Push CStr(Calc(ops1, ops2, opC))
     44                 If curChar = ")" And opChar.Peek = "(" Then
     45                     opChar.Pop  '如果操作数是),就把(弹出来
     46                     GoTo moveon
     47                 End If
     48                 GoTo redo
     49 moveon:
     50             End If
     51         Else '非符号
     52             strNumFix = strNumFix & curChar
     53         End If
     54 nextone:
     55     Next i
     56 End Function
     57 
     58 Public Function Calc(ByVal op1 As String, ByVal op2 As String, ByVal options As String) As Double
     59 On Error Resume Next
     60 Calc = 0
     61 Select Case options
     62     Case "+"
     63         Calc = CDbl(op1) + CDbl(op2)
     64     Case "-"
     65         Calc = CDbl(op1) - CDbl(op2)
     66     Case "*"
     67         Calc = CDbl(op1) * CDbl(op2)
     68     Case "/"
     69         Calc = CDbl(op1) / CDbl(op2)
     70 End Select
     71 End Function
     72 
     73 Public Function IsHigh(ByVal sNew As String, ByVal sOld As String) As Integer
     74 '1大于,-1小于,0等于
     75 Select Case sNew
     76 Case "+"
     77     Select Case sOld
     78         Case "("
     79             IsHigh = 1
     80             Exit Function
     81         Case "#"
     82             IsHigh = 1
     83             Exit Function
     84         Case Else
     85             IsHigh = -1
     86             Exit Function
     87     End Select
     88 Case "-"
     89     Select Case sOld
     90         Case "("
     91             IsHigh = 1
     92             Exit Function
     93         Case "#"
     94             IsHigh = 1
     95             Exit Function
     96         Case Else
     97             IsHigh = -1
     98             Exit Function
     99     End Select
    100 Case "*"
    101     Select Case sOld
    102         Case "("
    103             IsHigh = 1
    104             Exit Function
    105         Case "#"
    106             IsHigh = 1
    107             Exit Function
    108         Case "+"
    109             IsHigh = 1
    110             Exit Function
    111         Case "-"
    112             IsHigh = 1
    113             Exit Function
    114         Case Else
    115             IsHigh = -1
    116             Exit Function
    117     End Select
    118 Case "/"
    119     Select Case sOld
    120         Case "("
    121             IsHigh = 1
    122             Exit Function
    123         Case "#"
    124             IsHigh = 1
    125             Exit Function
    126         Case "+"
    127             IsHigh = 1
    128             Exit Function
    129         Case "-"
    130             IsHigh = 1
    131             Exit Function
    132         Case Else
    133             IsHigh = -1
    134             Exit Function
    135     End Select
    136 Case "("
    137     Select Case sOld
    138         Case "+"
    139             IsHigh = 1
    140             Exit Function
    141         Case "-"
    142             IsHigh = 1
    143             Exit Function
    144         Case "*"
    145             IsHigh = 1
    146             Exit Function
    147         Case "/"
    148             IsHigh = 1
    149             Exit Function
    150         Case "("
    151             IsHigh = 1
    152             Exit Function
    153         Case Else
    154             IsHigh = -1
    155             Exit Function
    156     End Select
    157 Case ")"
    158     IsHigh = -1
    159     Exit Function
    160 Case ""
    161     IsHigh = -1
    162     Exit Function
    163 Case "#"
    164     Select Case sOld
    165         Case "#"
    166             IsHigh = 0
    167             Exit Function
    168         Case ""
    169             IsHigh = 1
    170             Exit Function
    171         Case "+"
    172             IsHigh = -1
    173             Exit Function
    174         Case "-"
    175             IsHigh = -1
    176             Exit Function
    177         Case "*"
    178             IsHigh = -1
    179             Exit Function
    180         Case "/"
    181             IsHigh = -1
    182             Exit Function
    183         Case ")"
    184             IsHigh = -1
    185             Exit Function
    186     End Select
    187 End Select
    188 End Function
    189 
    190 Public Function IsSymbol(ByVal strS As String) As Boolean
    191     IsSymbol = True
    192     Select Case strS
    193         Case "+"
    194         Case "-"
    195         Case "*"
    196         Case "/"
    197         Case "("
    198         Case ")"
    199         Case "#"
    200         Case Else
    201             IsSymbol = False
    202     End Select
    203 End Function
  • 相关阅读:
    'IDataObject': ambiguous symbol的解决方法
    捕获windows系统的sleep或hibernate状态
    CallingConvention理解
    Exception from HRESULT: 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL))
    .Net Managed C++如何获取当前线程id和当前进程id
    div垂直居中于div中
    父级是relative,子级为absolute的情况下,子级宽度自适应
    background-img高度固定,图片自适应
    如何让两个input紧挨着.
    C# 调用百度短链接api
  • 原文地址:https://www.cnblogs.com/sunsoftresearch/p/3037271.html
Copyright © 2011-2022 走看看