作者fumizuki (小狮子)
看板Visual_Basic
标题Re: [VB6 ] 计算机
时间Thu Jun 9 22:21:00 2005
※ 引述《fumizuki (小狮子)》之铭言:
: ※ 引述《TrueFeeling (昨日之死)》之铭言:
: : 表单架构:
: : 0-9 数字键--Command1(Index)
: : +-*/键 --Command2(Index)
: : Enter --Command3
: 是打算输入运算式後按enter再算出值?
: 还是像一般计算机一样按下了加减乘除等於就会算出值?
哇哈!这种运算式真不好写...好不容易才试了出来...
用递回法写的,写了一长串,看起来颇复杂的...~.~
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
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
Eval = Val(s)
EndProc:
c = c - 1: Exit Function
SplitProc:
op1 = Left(s, p - 1): op2 = Mid(s, p + 1): Return
End Function
1. 事件程序的命名规则及参数列是写死的,一定要完全一样,不然无法通过编译。
2. Or、And 等运算子跟 + - * / 一样,一定会算出一个值。
(语法没错,执行结果不对而已)
3. N 跟 "N" 是不相同的,一样语法没错,但执行结果不对...
...
测试通过才贴上来的,应该是没有问题的吧(汗)
? Eval("-2.3 * -3 / -7 + -5.6 - -6 + -2 + -3 * -2 / -1 + -0.5")
? -2.3 * -3 / -7 + -5.6 - -6 + -2 + -3 * -2 / -1 + -0.5
-9.08571428571429
-9.08571428571429
以上...XD
加、减、乘、除、负号,运算子优先顺序,都有包含在内了...XD
--
VB 程式设计 仓木麻衣 PTT 星爷板 行列输入法
======================================================
Visual_Basic MaiKuraki Stephen Array
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 210.58.156.43