作者TrueFeeling (昨日之死)
看板Visual_Basic
標題Re: [VB6 ] 計算機
時間Mon Jun 13 17:24:00 2005
※ 引述《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
1F:推 fumizuki:因為你p可能會小於1,減1變成0,Mid不接受 61.222.155.162 06/13
2F:推 fumizuki:那是不合理的數字 61.222.155.162 06/13
3F:→ TrueFeeling:明白了 218.162.72.67 06/13