怎么样算"1+2*3/4"的值

silverblade 2003-09-29 05:34:56
一个变量里面存的是类似"1+2*3/4"这样的可以计算的四则运算题,用什么简单的办法可以得到结果。请注意,要简单。如果是把这个字符串分割开再算接别说了。

引申出一个问题。怎么样再VB中运行类似脚本的东西。
比如一个变量中保存有:
dim a as integer
a=10
print a
这样的VB代码,怎么样在程序中运行?
...全文
90 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
900126 2003-10-08
  • 打赏
  • 举报
回复
是2.5:1
900126 2003-10-08
  • 打赏
  • 举报
回复
虽然不用引用,但计算速度要满诶,好象用script速度要快些(1:0.25)
看来各有优点。
自编程序来计算在特殊需要时,是有优点的(比如简易运算的速度快),但做通用运算时,还是使用上述两种方法为好。我用了多年自编的处理程序,看来也该换换了。
不过,有自己编写的能力还是必要的。一些企业在录用考试时,就用过这个程序。
luolovegui 2003-10-05
  • 打赏
  • 举报
回复
高!UP
goodname008 2003-10-05
  • 打赏
  • 举报
回复
' 这个办法简单易行,什么都不用引用。 :)

Option Explicit
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

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 "msgbox 1+2*3/4"
ExecuteLine "dim a as integer"
ExecuteLine "a = 10"
ExecuteLine "Print a"
End Sub
Mars.CN 2003-10-05
  • 打赏
  • 举报
回复
MsgBox Eval.Eval("1+2*3/4")
pigpag 2003-10-01
  • 打赏
  • 举报
回复
楼上一大段的代码的算法与我的不同。我认为用数组分析,不用函数递归,前途不好(功能上无法扩张,单目运算符不能实现)
yxl3440 2003-10-01
  • 打赏
  • 举报
回复
up
feiqinfeiwhw 2003-09-30
  • 打赏
  • 举报
回复
Dim aa(0 To 3) As String
Dim sngResult As Single
Private Sub Command1_Click()
Dim str As String
str = "1+2*3/4"
aa(0) = "+"
aa(1) = "-"
aa(2) = "*"
aa(3) = "/"
sngResult = 0
Call Un(str)
MsgBox sngResult
End Sub

Private Function Un(ByVal str As String) As Single
Dim str1(0 To 1) As String
Dim lngResult As Single
Dim intPos As Long
Dim intpos3 As Long
Dim intpos4 As Long
Dim lngLength As Long
Dim int1 As Single
Dim int2 As Single
Dim j As Integer
If str = "" Then Exit Function
intPos = InStr(1, str, aa(2))
j = 2
If intPos = 0 Then
j = 3
intPos = InStr(1, str, aa(3))
End If
If intPos = 0 Then
j = 0
intPos = InStr(1, str, aa(0))
End If
If intPos = 0 Then
j = 1
intPos = InStr(1, str, aa(1))
End If
If intPos = 0 Then Exit Function
lngLength = intPos
str1(0) = Left(str, intPos - 1)
str1(1) = Right(str, Len(str) - intPos)
If str1(0) <> "" Then
int1 = getId(str1(0))
Else
If aa(j) = "+" Or aa(j) = "-" Then
int1 = 0
Else
int1 = 1
End If
End If
If str1(1) <> "" Then
int2 = getId1(str1(1))
Else
If aa(j) = "+" Or aa(j) = "-" Then
int2 = 0
Else
int2 = 1
End If
End If
sngResult = Result(aa(j), int1, int2)
str = CStr(Result(aa(j), int1, int2))
If str1(0) <> "" Then str = str1(0) & str
If str1(1) <> "" Then str = str & str1(1)
If str1(1) = "" And str1(0) = "" Then Exit Function
Call Un(str)
End Function

Private Function getId(ByRef strstr As String) As Single
Dim i As Integer
Dim intpos1 As Long
Dim intpos2 As Long
For i = 0 To 3
intpos1 = InStr(1, strstr, aa(i))
If intpos1 > intpos2 Then intpos2 = intpos1
Next i
getId = CSng(Right(strstr, Len(strstr) - intpos2))
strstr = Left(strstr, intpos2)
If Len(strstr) = 1 Then
If strstr = "-" Then getId = -CSng(getId)
End If
End Function

Private Function getId1(ByRef strstr As String) As Single
Dim i As Integer
Dim intpos1 As Long
Dim intpos2 As Long
For i = 0 To 3
intpos1 = InStr(1, strstr, aa(i))
If intpos1 <> 0 And intpos2 = 0 Then intpos2 = intpos1
If intpos1 <> 0 And intpos1 < intpos2 Then intpos2 = intpos1
Next i
If intpos2 = 0 Then
getId1 = CSng(strstr)
strstr = ""
Else
getId1 = CSng(Left(strstr, intpos2 - 1))
strstr = Right(strstr, Len(strstr) - intpos2 + 1)
End If
End Function
Private Function Result(ByVal str As String, ByVal int1 As Single, ByVal int2 As Single) As Single
Select Case str
Case "+"
Result = int1 + int2
Case "-"
Result = int1 - int2
Case "*"
Result = int1 * int2
Case "/"
Result = int1 / int2
Case Else
End Select
End Function
可以用到任何算术式
sngresult存放的就是计算结果
daviddivad 2003-09-30
  • 打赏
  • 举报
回复
感谢您使用微软产品。

您可以利用Microsoft Script Control来实现这个功能。

首先,你需要在工程里引用一下Microsoft Script Control。然后加入一下代码:

Private Sub Form_Load()

Dim scr As New ScriptControl
Dim mycode As String

scr.Language = "vbscript"
mycode = "public function Test()" + vbCrLf
mycode = mycode + "MsgBox ""Hello VB""" + vbCrLf
mycode = mycode + "End function"

scr.AddCode (mycode)
scr.Eval ("Test()")

End Sub

这样,在字符串mycode里定义的函数Test()就会被调用了。

参考:

HOWTO: Use Script Control Modules and Procedures Collections
http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q184745


- 微软全球技术中心 VB技术支持

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查
(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。

xiaohei728 2003-09-30
  • 打赏
  • 举报
回复
具然有这么多的方法真行呢
我第一次见这样的问题呢

值得收为已有
golden24kcn 2003-09-30
  • 打赏
  • 举报
回复
来晚了
txlicenhe 2003-09-30
  • 打赏
  • 举报
回复
菜单"工程/引用/Microsoft Script Control 1.0"

Private Sub Command2_Click()
Dim Eval As New ScriptControl

Eval.Language = "VBScript"
MsgBox Eval.Eval("1+2*3/4")

Set Eval = Nothing
End Sub
rainstormmaster 2003-09-30
  • 打赏
  • 举报
回复
我本人不推荐ScriptControl:1.慢 2.懒

当然,只是引用一处当然推荐使用。要是准备自己开发强大计算功能的,本人不推荐

同意
pigpag 2003-09-30
  • 打赏
  • 举报
回复
我本人不推荐ScriptControl:1.慢 2.懒

当然,只是引用一处当然推荐使用。要是准备自己开发强大计算功能的,本人不推荐。
pigpag 2003-09-30
  • 打赏
  • 举报
回复
我觉得这样的问题要自己考虑解决方法~~~~个人认为。

其实如果你真的想自己编这样的代码,一个下午就能编出PowerToys里面的计算功能了,并不难。
aaa2000 2003-09-30
  • 打赏
  • 举报
回复
做个记号
viena 2003-09-30
  • 打赏
  • 举报
回复
弓虽!
sunmaoyou 2003-09-30
  • 打赏
  • 举报
回复

Public Function jisuan(GS As String) As String
Dim i, n As Integer
Dim TempGs, temp As String
Dim Vl() As String '操作数
Dim Vls As Integer '操作数的数目
Dim Si As Integer '上一操作符的位置
Dim Ads, Sus, Mus, Bys, Lks, Rks As Integer '操作符的数目
Dim Adp(), Mup(), Byp(), Lkp(), Rkp() As Integer '操作符的位置
Dim Adn(), Mun(), Byn() As Integer '操作符的排列次序
Dim Sig() As Integer '每一个操作符的位置

On Error GoTo Err
Do While True
ReDim Adp(Len(GS)), Mup(Len(GS)), Byp(Len(GS)) _
, Lkp(Len(GS)), Rkp(Len(GS)) As Integer
ReDim Adn(Len(GS)), Mun(Len(GS)), Byn(Len(GS)) _
, Lkn(Len(GS)), Rkn(Len(GS)), Sig(Len(GS)) As Integer

ReDim Vl(Len(GS))

If Len(GS) = 0 Then GoTo Err
If Mid(GS, Len(GS), 1) <> "#" Then

TempGs = GS
For i = 1 To Len(GS) '将减化加

If Mid(GS, i, 1) = "-" And i <> 1 Then
If Mid(GS, i - 1, 1) <> "+" And Mid(GS, i - 1, 1) <> "-" _
And Mid(GS, i - 1, 1) <> "*" And Mid(GS, i - 1, 1) <> "/" Then
TempGs = Mid(TempGs, 1, i - 1 + n) + "+" + Mid(GS, i)
n = n + 1
End If

End If
Next i
GS = TempGs

n = 0
For i = 1 To Len(GS) '处理负负得正
If Mid(GS, i, 1) = "-" Then
If Mid(GS, i + 1, 1) = "-" Then
TempGs = Mid(TempGs, 1, i - 1 - n) + Mid(GS, i + 2)
n = n + 2
End If
End If
Next i
GS = TempGs
GS = GS + "#"
End If

Vls = 1
Ads = 0: Sus = 0: Mus = 0: Bys = 0: Lks = 0: Rks = 0

For i = 1 To Len(GS)

Select Case Mid(GS, i, 1)
Case "+"
Ads = Ads + 1
Adp(Ads) = i
Adn(Ads) = Vls
Case "*"
Mus = Mus + 1
Mup(Mus) = i
Mun(Mus) = Vls
Case "/"
Bys = Bys + 1
Byp(Bys) = i
Byn(Bys) = Vls
Case "("
Lks = Lks + 1
Lkp(Lks) = i

Case ")"
Rks = Rks + 1
Rkp(Rks) = i

End Select

If Mid(GS, i, 1) = "+" Or Mid(GS, i, 1) = "*" Or _
Mid(GS, i, 1) = "/" Or Mid(GS, i, 1) = "#" Then

If Si + 1 = i And Mid(GS, i + 1, 1) <> "#" _
Then '操作符非法连续或以操作符开头
GoTo Err
Else
Si = i
End If

If Not IsNumeric(Vl(Vls)) And Mid(GS, i + 1, 1) <> "#" _
Then '操作数不是数字
GoTo Err
End If
Sig(Vls) = i
Vls = Vls + 1

Else
If Mid(GS, i, 1) <> "(" And Mid(GS, i, 1) <> ")" Then
Vl(Vls) = Vl(Vls) + Mid(GS, i, 1) '制作操作数
Else
If i <> 1 Then
If ((Mid(GS, i - 1, 1) = "(" And Mid(GS, i, 1) = ")") Or _
(Mid(GS, i - 1, 1) = ")" And Mid(GS, i, 1) = "(")) _
Then '判定括号前后符号的合法性
GoTo Err
End If
End If
End If
End If

Next i

If Lks <> Rks Then
GoTo Err '左右括号数是否匹配
End If

For i = 1 To Lks
If Lkp(i) > Rkp(i) Then GoTo Err '左右括号出现顺序错误
Next i

If Lks <> 0 Then '括号处理
Do While True
For i = Lks To 1 Step -1
For n = Rks To 1 Step -1
temp = jisuan(Mid(GS, Lkp(i) + 1, Rkp(n) - Lkp(i) - 1))
If temp <> "公式有错误" Then
GS = Mid(GS, 1, Lkp(i) - 1) + temp + Mid(GS, Rkp(n) + 1)
Exit Do
End If
Next n
Next i
If temp = "公式有错误" Then GoTo Err
'括号中有错误退出
Loop
Else
If Mus <> 0 Then '乘法处理
GS = Mid(GS, 1, Sig(Mun(1) - 1)) + Trim(Str(Val(Vl(Mun(1))) _
* Val(Vl(Mun(1) + 1)))) + Mid(GS, Val(Mup(1)) + Len(Vl(Mun(1) _
+ 1)) + 1)
Else
If Bys <> 0 Then '除法处理
GS = Mid(GS, 1, Sig(Byn(1) - 1)) + Trim(Str(Val(Vl(Byn(1))) _
/ Val(Vl(Byn(1) + 1)))) + Mid(GS, Val(Byp(1)) + Len(Vl(Byn(1) _
+ 1)) + 1)
Else
If Ads <> 0 Then '加法处理
GS = Trim(Str(Val(Vl(1)) + Val(Vl(2)))) + Mid(GS, Val(Adp(1)) _
+ Len(Vl(2)) + 1)
Else
jisuan = Mid(GS, 1, Len(GS) - 1)
Exit Function
End If
End If
End If
End If
Loop


Err:
jisuan = "公式有错误"

End Function



sunmaoyou 2003-09-30
  • 打赏
  • 举报
回复
也可以添加一个模块,可以计算任何代数式的值:
#If Win16 Then
Type RECT
Left As Integer
Top As Integer
Right As Integer
Bottom As Integer
End Type
#Else
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
#End If

'User and GDI Functions for Explode/Implode to work

#If Win16 Then
Declare Sub GetWindowRect Lib "User" (ByVal hWnd As Integer, lpRect As RECT)
Declare Function GetDC Lib "User" (ByVal hWnd As Integer) As Integer
Declare Function ReleaseDC Lib "User" (ByVal hWnd As Integer, ByVal hdc As Integer) As Integer
Declare Sub SetBkColor Lib "GDI" (ByVal hdc As Integer, ByVal crColor As Long)
Declare Sub Rectangle Lib "GDI" (ByVal hdc As Integer, ByVal X1 As Integer, ByVal Y1 As Integer, ByVal X2 As Integer, ByVal Y2 As Integer)
Declare Function CreateSolidBrush Lib "GDI" (ByVal crColor As Long) As Integer
Declare Function SelectObject Lib "GDI" (ByVal hdc As Integer, ByVal hObject As Integer) As Integer
Declare Sub DeleteObject Lib "GDI" (ByVal hObject As Integer)
#Else
Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hdc As Long) As Long
Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Declare Function Rectangle Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Declare Function SelectObject Lib "user32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
#End If

'****************************************************************
'*Author: Carl Slutter
'*
'*Description:
'*The higher the "Movement", the slower the window
'*"explosion".
'*
'*Creation Date: Thursday 23 January 1997 2:27 pm
'*Revision Date: Thursday 23 January 1997 2:27 pm
'*
'*Version Number: 1.00
'****************************************************************

Sub ExplodeForm(F As Form, Movement As Integer)
Dim myRect As RECT
Dim formWidth%, formHeight%, i%, X%, Y%, Cx%, Cy%
Dim TheScreen As Long
Dim Brush As Long

GetWindowRect F.hWnd, myRect
formWidth = (myRect.Right - myRect.Left)
formHeight = myRect.Bottom - myRect.Top
TheScreen = GetDC(0)
Brush = CreateSolidBrush(F.BackColor)

For i = 1 To Movement
Cx = formWidth * (i / Movement)
Cy = formHeight * (i / Movement)
X = myRect.Left + (formWidth - Cx) / 2
Y = myRect.Top + (formHeight - Cy) / 2
Rectangle TheScreen, X, Y, X + Cx, Y + Cy
Next i

X = ReleaseDC(0, TheScreen)
DeleteObject (Brush)

End Sub


Public Sub ImplodeForm(F As Form, Direction As Integer, Movement As Integer, ModalState As Integer)
'****************************************************************
'*Author: Carl Slutter
'*
'*Description:
'*The larger the "Movement" value, the slower the "Implosion"
'*
'*Creation Date: Thursday 23 January 1997 2:42 pm
'*Revision Date: Thursday 23 January 1997 2:42 pm
'*
'*Version Number: 1.00
'****************************************************************

Dim myRect As RECT
Dim formWidth%, formHeight%, i%, X%, Y%, Cx%, Cy%
Dim TheScreen As Long
Dim Brush As Long

GetWindowRect F.hWnd, myRect
formWidth = (myRect.Right - myRect.Left)
formHeight = myRect.Bottom - myRect.Top
TheScreen = GetDC(0)
Brush = CreateSolidBrush(F.BackColor)

For i = Movement To 1 Step -1
Cx = formWidth * (i / Movement)
Cy = formHeight * (i / Movement)
X = myRect.Left + (formWidth - Cx) / 2
Y = myRect.Top + (formHeight - Cy) / 2
Rectangle TheScreen, X, Y, X + Cx, Y + Cy
Next i

X = ReleaseDC(0, TheScreen)
DeleteObject (Brush)

End Sub




'计算用户输入的表达式
'如 "3+2*2" 结果为 "7"


tccth4091 2003-09-30
  • 打赏
  • 举报
回复
up 学习
加载更多回复(9)
​ 博主介绍:✌在职Java研发工程师、专注于程序设计、源码分享、技术交流、专注于Java技术领域和毕业设计✌项目名称基于Web的酒店客房管理系统的设计与实现系统说明3.2.1  用户模块功能分析前台模块应主要包括用户登录模块、用户注册模块、查看客房信息模块、客房预定 模块、用户留言模块、充模块和个人信息维护模块。用户登录模块:用户通过输入注册的的账号和密码,然后进行身份验证,匹配成功 后实现登录功能。用户注册模块:用户输入合法的账号和密码可以实现注册功能。 查看客房信息模块:用户可以通过首页查看客房的价格,图片,详情等信息,从而   可以选择想要预定的房间。 客房预定模块:用户选择自己想要预定的客房后,可以通过输入相关信息进行订房操作。用户留言模块:用户可以向管理员发送留言。 充模块:用户可以通过添加银行卡再为自己进行充操作。 个人信息维护模块:用户可以修改自己的姓名、密码、身份证号等信息,还可以查看自己的订单。3.2.2  管理员模块功能分析对于管理员而言,一个好的管理系统总是能让酒店的管理工作事半功倍[7]。管理员 能够通过这个系统对自己的酒店情况一目了然,应该包括客户留言模块、客房管理模 块、订房信息管理模块、入住信息管理模块、统计分析模块、酒店新闻管理模块、会 员信息管理模块、员工信息管理模块、系统用户管理模块、个人信息维护模块。客户留言模块:管理员可以查看并回复用户的留言。客房管理模块:管理员可以管理客房信息,可以添加新的客房,删除已经停用的客房信息,还可以修改现有的客房信息。订房信息管理模块:管理员可以处理用户的订房请求,为用户办理入住手续。入住信息管理模块:对于没有注册的线下客户,也可以办理入住手续。统计分析模块:可以对酒店所有的入住信息进行记录和总结分析。酒店新闻管理模块:管理员可以更新网站上的新闻公告,展示图片等信息。会员信息管理模块:管理员可以查看到所有的注册会员信息,可以对会员信息进行删除,修改,添加操作。员工信息管理模块:管理员可以查看到自己公司所有的员工信息,而且还可以对员工的相关信息进行管理。系统用户管理模块:管理员可以查看到所有的系统管理员信息并对管理员信息进行管理。个人信息维护模块:管理员可以更改自己的登录密码或者是姓名、性别、手机号等 相关个人信息。​编辑 环境需要1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.数据库:MySql 5.7版本;6.是否Maven项目:否;技术栈1. 后端:Spring+SpringMVC+Mybatis2. 前端:JSP+CSS+JavaScript+jQuery使用说明1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;3. 将项目中springmvc-servlet.xml配置文件中的数据库配置改为自己的配置;4. 运行项目,在浏览器中输入http://localhost:8080/ 登录运行截图​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑 用户管理控制层:package com.houserss.controller;import javax.servlet.http.HttpSession;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import com.houserss.common.Const;import com.houserss.common.Const.Role;import com.houserss.common.ServerResponse;import com.houserss.pojo.User;import com.houserss.service.IUserService;import com.houserss.service.impl.UserServiceImpl;import com.houserss.util.MD5Util;import com.houserss.util.TimeUtils;import com.houserss.vo.DeleteHouseVo;import com.houserss.vo.PageInfoVo;/** * Created by admin */@Controller@RequestMapping(/user/)public class UserController if (ip != null && ip.length() > 0) String[] ips = ip.split(,);for (int i = 0; i  

7,763

社区成员

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

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