作者TrueFeeling (昨日之死)
看板Visual_Basic
标题Re: [VB6 ] 计算机
时间Sat Jun 11 15:00:28 2005
※ 引述《fumizuki (小狮子)》之铭言:
: Private Sub Command1_Click(Index As Integer)
: Text1.Text = Text1.Text & Index
: End Sub
: Private Sub Command2_Click(Index As Integer)
: Text1.Text = Text1.Text & Command2(Index).Caption
: End Sub
: Private Sub Command3_Click()
: Label1.Caption = Eval(Text1.Text)
: End Sub
: Function Eval(s As String)
: Dim p As Integer, op1 As String, op2 As String, o As String
: Static c As Integer: c = c + 1
: If c = 1 Then s = Replace(s, " ", "")
: p = InStr(s, "+")
: If p > 0 Then GoSub SplitProc: Eval = Eval(op1) + Eval(op2): GoTo EndProc
: If s Like "-*" Then p = 2 Else p = 1
: p = InStr(p, s, "-")
: If p > 0 Then
: op1 = Mid(s, p - 1, 1)
: If op1 = "+" Then
: p = p - 1: GoSub SplitProc: Eval = Eval(op1) + Eval(op2): GoTo EndProc
: ElseIf op1 = "-" Then
: p = p - 1: GoSub SplitProc: Eval = Eval(op1) - Eval(op2): GoTo EndProc
: ElseIf op1 = "*" Then
: p = p - 1: GoSub SplitProc: Eval = Eval(op1) * Eval(op2): GoTo EndProc
: ElseIf op1 = "/" Then
: p = p - 1: GoSub SplitProc: Eval = Eval(op1) / Eval(op2): GoTo EndProc
我在这边加入一个:
ElseIf op1 = "^" Then
p = p - 1: GoSub SplitProc
: Else
: GoSub SplitProc: Eval = Eval(op1) + Eval("-" & op2): GoTo EndProc
: End If
: End If
: p = InStr(s, "*")
: If p > 0 Then GoSub SplitProc: Eval = Eval(op1) * Eval(op2): GoTo EndProc
: p = InStr(s, "/")
: If p > 0 Then GoSub SplitProc: Eval = Eval(op1) / Eval(op2): GoTo EndProc
这边加入一个:
p = InStr(s, "^")
If p > 0 Then GoSub SplitProc: F = F(op1) ^ F(op2): GoTo EndProc
: Eval = Val(s)
: EndProc:
: c = c - 1: Exit Function
: SplitProc:
: op1 = Left(s, p - 1): op2 = Mid(s, p + 1): Return
: End Function
没想到竟然也可以算次方,可是我想不出原理是什麽
如果说程式执行到^的时候,字串已经里面不会再有加减乘除号了,
这样
p = InStr(s, "^")
If p > 0 Then GoSub SplitProc: F = F(op1) ^ F(op2): GoTo EndProc
这边就可以理解
但是这边
ElseIf op1 = "^" Then
p = p - 1: GoSub SplitProc
如果-前面有^,後面有*,
例如:Eval(5^-3*2)的时候
照理说,我没有设定Eval=Eval(op1) ^ Eval(op2):
(因为设了会出错)
Eval(5^-3*2)应该无法分割,但是答案却出乎我预料的正确@@!!
--
少年读书,如隙中窥月;
中年读书,如庭中望月;
老年读书,如台上玩月。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.141.159.236