Re: [VB6 ] 計算機
※ 引述《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
討論串 (同標題文章)
Visual_Basic 近期熱門文章
PTT數位生活區 即時熱門文章