如何将数据库中存储的带变量公式(即字符串)转化为VB程序中的数学表达式
如存储公式为 :X+Y (字符串即"X+Y")
给变量X、Y赋值
将字符串公式 “X+Y” 变成 数学表达式 X+Y
用表达式进行计算
[解决办法]
做起来还真有点纠结……
简单的例子:x,y的四则运算:
新建一工程,添加text三个,command一个,粘贴如下代码,输入x,y的值和运算方式,按command:
Private Sub Command1_Click() x = Val(Text1.Text) y = Val(Text2.Text) s = Text3.Text f = "+,-,*,/" ff = Split(f, ",") For i = 0 To UBound(ff) ss = Split(s, ff(i)) If UBound(ss) > 0 Then If ss(0) = "x" Then Select Case ff(i) Case "+" Print Text3.Text & "=" & x + y Case "-" Print Text3.Text & "=" & x - y Case "*" Print Text3.Text & "=" & x * y Case "/" Print Text3.Text & "=" & x / y End Select End If If ss(1) = "x" Then Select Case ff(i) Case "+" Print Text3.Text & "=" & y + x Case "-" Print Text3.Text & "=" & y - x Case "*" Print Text3.Text & "=" & y * x Case "/" Print Text3.Text & "=" & y / x End Select End If End If NextEnd SubPrivate Sub Form_Load() Text1.Text = "x的值" Text2.Text = "y的值" Text3.Text = "x+y"End Sub
[解决办法]
调用scriptcontrol,计算表达式。
[解决办法]
工程中添加scriptcontrol 控件或者引用
提取公式,使用scriptcontrol控件或者对象进行计算