1+2*3+1=???的问题

daibing3000 2004-04-08 12:12:06
我想做一个text,在里面输入一些简单的计算,如何实现,先算乘除后算加减啊?
...全文
91 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lionking1027 2004-04-09
  • 打赏
  • 举报
回复
up
mmcgzs 2004-04-09
  • 打赏
  • 举报
回复
抢点分
daibing3000 2004-04-09
  • 打赏
  • 举报
回复
太感谢大家了,我都不知道怎么给分了,我都使用一下,我用的给50分,其他参与的分别给分,大家没有意见吧?

感激大家的帮助!
jilate 2004-04-08
  • 打赏
  • 举报
回复
试过了,楼上的方法确实可行
weaest 2004-04-08
  • 打赏
  • 举报
回复
用ScriptControl来做
Set s = CreateObject("ScriptControl")
s.Language = "VBScript"
text2.Text = s.Eval(text1.Text)
Set s = Nothing
射天狼 2004-04-08
  • 打赏
  • 举报
回复
工程->引用->Microsoft Script Control 1.0

Private Sub Command1_Click()
Dim cal As New ScriptControl

cal.Language = "VBScript"
MsgBox cal.Eval("1+2*3")
End Sub
liyan010 2004-04-08
  • 打赏
  • 举报
回复
用我这个,一个text1用来输入表达式,一个command1用来确定计算,在text2中显示结果
★★可以计算任意复杂的表达式带括号★★
代码如下:


Private Sub GetMatchExp(ByVal Exp As String, LF As String, RF As String, Lp As Integer, Rp As Integer)
'从表达式中,查找匹配起始位置
Dim strT As String
Rp = InStr(Exp, RF)
If Rp = 0 Then
Lp = 0
Else
strT = Left(Exp, Rp - 1)
strT = StrReverse(strT)
Lp = Rp - InStr(strT, LF)
End If
End Sub

Function CalCulNext(ByVal Exp As String) As String
'本函数可以计算一个完整的表达式
'计算表达式中最优先的一步
Dim Lp As Integer, Rp As Integer, Mp As Integer, FirstNum As String, SecondNum As String
Call GetMatchExp(Exp, "(", ")", Lp, Rp)
If Lp * Rp = 0 Then '没有括弧
Mp = FirstOperater(Exp) '可用的操作符位置
If Mp = 0 Then '没有运算符
CalCulNext = Exp
Else
FirstNum = GetOpNum(Exp, Mp, True)
SecondNum = GetOpNum(Exp, Mp, False)
CalCulNext = CalCulNext(Mid(Exp, 1, Mp - Len(FirstNum) - 1) _
& CStr(DirectCalValue(FirstNum, SecondNum, Mid(Exp, Mp, 1))) _
& Right(Exp, Len(Exp) - (Mp + Len(SecondNum))))
End If
Else '有括弧
FirstNum = Left(Exp, Lp - 1)
SecondNum = Right(Exp, Len(Exp) - Rp)
CalCulNext = CalCulNext(FirstNum & CalCulNext(Mid(Exp, Lp + 1, Rp - Lp - 1)) & SecondNum)
End If
End Function
Private Function GetOpNum(ByVal Exp As String, ByVal OperPosition As Integer, _
ByVal AtLeft As Boolean) As String
'从表达式中取出指定操作符左或右侧的操作数
Dim I As Integer, pStep As Integer, J As Integer, Ch As String
J = OperPosition
If AtLeft Then
I = 1
pStep = -1
Else
I = Len(Exp)
pStep = 1
J = J + 1
End If
For J = J + pStep To I Step pStep
Ch = Mid(Exp, J, 1)
If Not (Ch >= "0" And Ch <= "9" Or Ch = ".") Then '
I = J - pStep
Exit For
End If
Next J
If AtLeft Then
J = OperPosition
If I > 1 Then
If Mid(Exp, I - 1, 1) = "-" Then
If I = 2 Then '不是第一个字符,检查数字前有无负号
I = 1
ElseIf I > 2 Then
Ch = Mid(Exp, I - 2, 1)
If Not (Ch >= "0" And Ch <= "9" Or Ch = ".") Then
I = I - 1
End If
End If
End If
End If
Else
J = I + 1
I = OperPosition + 1
End If
GetOpNum = Mid(Exp, I, J - I)
End Function
Private Function DirectCalValue(FirstV As String, SecondV As String, Op As String)
'用操作符计算两个操作数
Dim t1 As Double, t2 As Double
t1 = Val(FirstV): t2 = Val(SecondV)
Select Case Op
Case "+"
t1 = t1 + t2
Case "-"
t1 = t1 - t2
Case "*"
t1 = t1 * t2
Case "/"
t1 = t1 / t2
Case Else
Exit Function
End Select
DirectCalValue = CStr(t1)
End Function
Private Function FirstOperater(ByVal Exp As String) As Integer
'返回指定表达式字符串中下一步可最先计算的操作符位置
Dim P1 As Integer, P2 As Integer
P1 = InStr(Exp, "*")
P2 = InStr(Exp, "/")
GoSub SubFun
'从子程序返回,则没有 * / 符号
P1 = InStr(Exp, "+")
P2 = InStr(2, Exp, "-")
'从子程序返回,则没有 + - 符号
GoSub SubFun
FirstOperater = 0
Exit Function
SubFun:
If P1 * P2 = 0 Then
If P1 + P2 <> 0 Then
FirstOperater = P1 + P2
Exit Function
End If
Else
If P1 > P2 Then P1 = P2
FirstOperater = P1
Exit Function
End If
Return
End Function
'------------------------------------------------------------------
'******************计算字符串形式表达式结束************************
'------------------------------------------------------------------


Private Sub Command1_Click()

Text2.Text = CalCulNext(Replace(Text1.Text, " ", ""))

End Sub
thelostland 2004-04-08
  • 打赏
  • 举报
回复
up
yinweihong 2004-04-08
  • 打赏
  • 举报
回复
2楼///
注意引用microsoft script control 1.0
northwolves 2004-04-08
  • 打赏
  • 举报
回复
1 USE API:

' add a textbox and a commandbutton to form1
Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long
Dim i As Double
Public Function ExecuteLine(sCode As String, Optional fCheckOnly As Boolean) As Boolean
ExecuteLine = EbExecuteLine(StrPtr(sCode), 0&, 0&, Abs(fCheckOnly)) = 0
End Function
Private Sub Command1_Click()

ExecuteLine "form1.i=" + Text1.Text

MsgBox Text1.Text & "=" & i

End Sub

Private Sub Form_Load()
Text1.Text = "1+2*3-4/5"
End Sub

2 USE EXCEL:

'引用microsoft excel 10.0 object library( OR OTHER VERSION)
' add a textbox and a commandbutton to form1
Function result(ByVal x As String)
Dim myobj As Object
Set myobj = CreateObject("excel.sheet")
Set myobj = myobj.Application.ActiveWorkbook.ActiveSheet
myobj.Range("a1").Formula = "= " & x '
result = myobj.Range("a1").Value
If Err.Number > 0 Then MsgBox Err.Description
Set myobj = Nothing
End Function

Private Sub Command1_Click()
MsgBox Text1.Text & "=" & result(Text1.Text)
End Sub

Private Sub Form_Load()
Text1.Text = "1+2*3-4/5"
End Sub
qiqunet 2004-04-08
  • 打赏
  • 举报
回复

这个论坛里关于字符串算式的问题太多了,原来还值100分呀!

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧