Re: [VB6 ] 計算機
※ 引述《fumizuki (小獅子)》之銘言:
: Public Function Eval(s As String)
: '解開方程式的值
: Dim p As Integer, f As Integer, op1 As String, op2 As String
: Dim a As String, o As String
: '第一次進入函式,把空白全去除掉。
: Static c As Integer: c = c + 1
: If c = 1 Then s = Replace(s, " ", "")
: '加法運算
: o = "+": GoSub SearchProc
: If p > 0 Then GoSub SplitProc: Eval = Eval(op1) + Eval(op2): GoTo EndProc
: '減法運算
: o = "-": GoSub SearchProc
: If p > 0 Then GoSub SplitProc: Eval = Eval(op1) - Eval(op2): GoTo EndProc
: '乘法運算
: o = "*": GoSub SearchProc
: If p > 0 Then GoSub SplitProc: Eval = Eval(op1) * Eval(op2): GoTo EndProc
: '除法運算
: o = "/": GoSub SearchProc
: If p > 0 Then GoSub SplitProc: Eval = Eval(op1) / Eval(op2): GoTo EndProc
: '指數運算
: o = "^": GoSub SearchProc
: If p > 0 Then
: GoSub SplitProc
: If op1 Like "-*" Then
: Eval = -Eval(Mid(op1, 2)) ^ Eval(op2)
: Else
: Eval = Eval(op1) ^ Eval(op2)
: End If
: GoTo EndProc
: End If
: Eval = Val(s)
: EndProc:
: c = c - 1: Exit Function
: SearchProc:
: p = Len(s) + 1
: Do
: p = InStrRev(s, o, p - 1)
為什麼我把下面的a放到這裡就執行錯誤了?? = =
a = Mid(s, p - 1, 1)
: If o = "^" Then Exit Do
: If p <= 1 Then
: f = 0 '首字忽略;找不到則離開迴圈
: Else
: a = Mid(s, p - 1, 1)
: If a = "+" Or a = "-" Or a = "*" Or a = "/" Or a = "^" Then
: f = 1
: Else
: f = 0
: End If
: End If
: Loop While f = 1
: If p = 1 Then p = 0
: Return
: SplitProc:
: op1 = Left(s, p - 1): op2 = Mid(s, p + 1): Return
: End Function
--
少年讀書,如隙中窺月;
中年讀書,如庭中望月;
老年讀書,如臺上玩月。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.162.72.67
推
61.222.155.162 06/13, , 1F
61.222.155.162 06/13, 1F
推
61.222.155.162 06/13, , 2F
61.222.155.162 06/13, 2F
→
218.162.72.67 06/13, , 3F
218.162.72.67 06/13, 3F
討論串 (同標題文章)
Visual_Basic 近期熱門文章
PTT數位生活區 即時熱門文章