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