作者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