如果用户输入1 + 1,我该如何使这个计算器工作? 它只有在我输入1 + 1; C时才有效 计算必须在1行。
x = inputbox("Calculation", "Calculator", "1+1")
y = Split(x)
if not isnumeric (y(0)) then
wscript.quit
end if
if not isnumeric (y(2)) then
wscript.quit
end if
if y(1) = "+" then
z = int(y(0)) + int(y(2))
msgbox(z)
end if
if y(1) = "-" then
z = int(y(0)) - int(y(2))
msgbox(z)
end if
if y(1) = "*" then
z = int(y(0)) * int(y(2))
msgbox(z)
end if
if y(1) = "/" then
z = int(y(0)) / int(y(2))
msgbox(z)
end if
if y(1) = "%" then
z = int(y(0)) MOD int(y(2))
msgbox(z)
end if
感谢您的回答!
答案 0 :(得分:3)
尝试下一个代码段(注释以作解释):
ng2-ace
使用Eval
Function(评估表达式并返回结果)的另一种方法(允许超过纯算术操作)和基本的error handling:
Dim ii, sOperator, strExpr, y
strExpr = inputbox("Calculation", "Calculator", "1+1")
' insert spaces around all operators
For Each sOperator in Array("+","-","*","/","%")
strExpr = Trim( Replace( strExpr, sOperator, Space(1) & sOperator & Space(1)))
Next
' replace all multi spaces with a single space
Do While Instr( strExpr, Space(2))
strExpr = Trim( Replace( strExpr, Space(2), Space(1)))
Loop
y = Split(strExpr)
''' your script continues here
示例输出:
option explicit
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName
Dim strExpr, strInExp, strLastY, yyy
strInExp = "1+1"
strLastY = ""
Do While True
strExpr = inputbox("Last calculation:" & vbCR & strLastY, "Calculator", strInExp)
If Len( strExpr) = 0 Then Exit Do
''' in my locale, decimal separator is a comma but VBScript arithmetics premises a dot
strExpr = Replace( strExpr, ",", ".") ''' locale specific
On Error Resume Next ' enable error handling
yyy = Eval( strExpr)
If Err.Number = 0 Then
strInExp = CStr( yyy)
strLastY = strExpr & vbTab & strInExp
strResult = strResult & vbNewLine & strLastY
Else
strLastY = strExpr & vbTab & "!!! 0x" & Hex(Err.Number) & " " & Err.Description
strInExp = strExpr
strResult = strResult & vbNewLine & strLastY
End If
On Error GoTo 0 ' disable error handling
Loop
Wscript.Echo strResult
Wscript.Quit
答案 1 :(得分:0)
如果有多个文件,将其记录到.txt文件中并在批处理中使用“ set / a”进行简单计算,我发现了一种更简单的方法。
在vbs文件中:
obj2
在批处理文件(calculator.bat)中:
calculation = inputbox("What do you want to calculate?(include =)", "Eclips-Terminal Calculator")
oFile.write "" & calculation & ""
oFile.close
wsh.run "calculator.bat"
wscript.sleep 3000
msgbox rfile.readall
oFile.close
然后是一个名为“ RAM.txt”的文本文件。 这是我的好,干净和简单的方式,这太难了。
编辑:我知道这是一个4岁的问题,我只是找不到简单的修复程序,所以我在这里添加了它,以便其他人可以找到它。