作者fumizuki (小獅子)
看板Visual_Basic
標題Re: [VB6 ] 計算機
時間Sun Jun 12 10:57:22 2005
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)
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
--
VB 程式設計 倉木麻衣 PTT 星爺板 行列輸入法
======================================================
Visual_Basic MaiKuraki Stephen Array
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.58.156.43
※ 編輯: fumizuki 來自: 210.58.156.43 (06/12 15:31)
※ 編輯: fumizuki 來自: 210.58.156.43 (06/12 15:36)