如何使用字符串"*"或"\"作为数学运算符?

Mic*_*ins 4 vb.net math

我想使用"*"或"\"作为数学运算符:

"我要澄清一下"

dim tbox as textbox
tbox.text = "*"

dim i as integer = 8 tbox.text 3
Run Code Online (Sandbox Code Playgroud)

"结束澄清"

dim [mult] as string = "*"
dim [div] as string = "\"


dim i as integer = 8 [mult] 3
Run Code Online (Sandbox Code Playgroud)

结果将是我等于24

要么

dim i as integer = 8 [div] 2 
Run Code Online (Sandbox Code Playgroud)

结果将是i等于4

任何人都可以在一行中解决这个问题而无需构建冗长复杂的功能吗?我也希望这是一个已经是VB.NET结构的一部分并且不需要导入的东西.

如果不存在这样的解决方案,我该如何使用函数或.dll导入?

Jim*_*ark 6

将其插入您要评估的位置.

dim [mult] as string = "*"
dim [div] as string = "\"


'dim i as integer = 8 [mult] 3

op = mult

dim i As Integer = Eval("8" & op & "3")

'i is your result


  Private Function Eval(ByVal command As String) As Object
    Dim MyProvider As New VBCodeProvider 'Create a new VB Code Compiler
    Dim cp As New CodeDom.Compiler.CompilerParameters     'Create a new Compiler parameter object.

    cp.GenerateExecutable = False        'Don't create an object on disk
    cp.GenerateInMemory = True           'But do create one in memory.

    'If cp.OutputAssembly is used with a VBCodeProvider, it seems to want to read before it is executed. 

    'See C# CodeBank example for explanation of why it was used.



    'the below is an empty VB.NET Project with a function that simply returns the value of our command parameter.

    Dim ClassName As String = "class" & Now.Ticks

    Dim TempModuleSource As String = "Imports System" & Environment.NewLine & _
                                     "Namespace ns " & Environment.NewLine & _
                                     "    Public Class " & ClassName & Environment.NewLine & _
                                     "        Public Shared Function Evaluate()" & Environment.NewLine & _
                                     "            Return (" & command & ")" & Environment.NewLine & _
                                     "        End Function" & Environment.NewLine & _
                                     "    End Class" & Environment.NewLine & _
                                     "End Namespace"

    'Create a compiler output results object and compile the source code.

    Dim cr As CodeDom.Compiler.CompilerResults = MyProvider.CompileAssemblyFromSource(cp, TempModuleSource)

    If cr.Errors.Count > 0 Then

      'If the expression passed is invalid or "", the compiler will generate errors.

      'Throw New ArgumentOutOfRangeException("Invalid Expression - please use something VB could evaluate")
      Return Nothing
    Else

      'Find our Evaluate method.
      Dim methInfo As Reflection.MethodInfo = cr.CompiledAssembly.GetType("ns." & ClassName).GetMethod("Evaluate")

      'Invoke it on nothing, so that we can get the return value
      Return methInfo.Invoke(methInfo, New Object() {})
    End If
  End Function
Run Code Online (Sandbox Code Playgroud)


Jon*_*n B 5

您可以使用CodeDom来计算表达式.您的示例中的表达式将是"8 * 3",或"8" + tbox.Text + "3".

这是一个如何在VB.NET中执行此操作的示例.这个功能肯定不仅仅是一个班轮,但称之为简单.


Mar*_*age 5

任何人都可以在一行中解决这个问题而无需构建冗长复杂的功能吗?

或许,是的,但我认为一个小功能更好:

Function Compute(a as integer, oper as String, b as integer) as integer
  If oper = "*" Then
    return a*b
  ElseIf oper = "\" Then
    Return a\b
  Else
    Throw New InvalidOperationException()
  End If
End Function
Run Code Online (Sandbox Code Playgroud)

如果你真的想要一个单行,你可以使用这个If()功能:

Dim i As Integer = If(oper = "*", a*b, a\b)
Run Code Online (Sandbox Code Playgroud)

这不会检查无效的运算符.