当运算符是变量字符时,执行数学计算的正确方法是什么。换句话说,在编译时不知道该操作。需要在运行时动态选择操作。
例如,假设我需要计算+
的总和,但Char
运算符存储在Dim operator As Char = "+"c
Dim result As Long = 10 operator 20
变量中,如下所示:
package hgp.pkg13.pkg16;
import java.util.Scanner;
public class HGP1316 {
//Declaration of variables
static int Running_total = 0;
static double Average = 0.0;
static int User_input = 0;
static double counter = 0.0 ;
static int Highest_input = -1;
static int Lowest_input = -1;
public static void main(String[] args) {
//Intro Program (Optional)
{
System.out.println("This program will give you the highest, lowest, and average of the integers you \n"
+ "enter. To end enter a negative number ");
}
//2. Ask user to enter a positive integer (or a negative integer to stop)
//3. Record the user's input
//4. Test the input >= 0
while (User_input >=0){
Scanner user_input = new Scanner( System.in );
Integer User_input;
System.out.print("Please enter an integer: ");
User_input = user_input.nextInt();
//4a. If it is true that input >= 0
if (User_input >= 0)
{
//4a1Add the input to "running total"
Running_total = Running_total + User_input;
//4a2. Increment "number of inputs counter"
counter = counter + 1;
//4a3. Test if "number of inputs counter" is equal to 1
if (counter == 1)
{
//4a31. If true, replace both "highest input" and "lowest input" with the input
Highest_input = User_input;
Lowest_input = User_input;
}
//4a5. If the input > "highest input" then replace "highest input" with input
if (User_input > Highest_input)
{
Highest_input = User_input;
}
//4a6. If the input < "lowest input" then replace "lowest input" with input
if (User_input < Lowest_input)
{
Lowest_input = User_input;
}
//4b. If false
//4b1. Goto step 5
else;
{
//5. Calculate average (formula: "running total" /"number of inputs counter" )
Average = (Running_total / counter);
//6. Display/communicate "highest input", "lowest input" and average
System.out.println ("The Highest value entered was : "+ Highest_input);
System.out.println ("The Lowest value entered was : " + Lowest_input);
System.out.println("The Average of enteries was : "+ Average);
//7. Terminate
}
}
}
}
}
答案 0 :(得分:2)
简单的方法是使用Select Case
语句使用基于输入字符的正确操作来执行计算。例如:
' Binary, as in not unary nor ternary
Public Function PerformBinaryOperation(operand1 As Long, operator As Char, operand2 As Long)
Select Case operator
Case "+"c
Return operand2 + operand2
Case "-"c
Return operand2 - operand2
' ...
End Select
End Function
更高级的解决方案是编写表达式解析器来分析表达式字符串,例如"10 + 20"
,并将其转换为expression tree。如果您不想编写自己的解析器,可以使用其中一个库来执行此操作,例如NCalc。
表达式树解析的另一种替代方法是生成一段.NET代码,然后在运行时动态构建和执行该代码。例如,如果你做了这样的事情:
Dim operator As Char = "+"c
Dim codeBuilder As New StringBuilder()
codeBuilder.AppendLine("Public Class MyOperation")
codeBuilder.AppendLine(" Public Function Calculate() As Long")
codeBuilder.AppendLine(" Return 10 " & operator & " 20")
codeBuilder.AppendLine(" End Function")
codeBuilder.AppendLine("End Class")
Dim myOperation As Object = ' ... compile code and instantiate object dynamically
Dim result As Long = myOperation.Calculate()
传统上,您可以使用CodeDom类来执行动态编译,但现在使用.NET 4.6中的新Roslyn编译器,您可以使用新的Emit
功能。 Here是一篇介绍各种动态编译选项的文章。