构造表达式树时未处理的 InvalidOperationException
Unhandled InvalidOperationException when constructing expression trees
我一直在尝试构建一个表达式树来评估 RPN 中的数学表达式。
我在尝试调用 MakeBinary 表达式来计算树中的数字时出现异常。
例外情况如下"System.InvalidOperationException: 'The binary operator Add is not defined for the types 'System.Char' and 'System.Char'.'"
这是我的代码:
public static bool isOp(char checkIt)
{
bool verify;
if(checkIt== '+'|| checkIt== '-'||checkIt== '*' || checkIt== '/'||checkIt== '^')
{
return verify= true;
}
else
{
return verify= false;
}
return verify;
}
public static BinaryExpression myMakeBiniray(char ops,Expression leftOp, Expression rightOp)
{
switch (ops)
{
case '+':
Expression left = leftOp;
Expression right = rightOp;
return Expression.MakeBinary(ExpressionType.Add, left, right);
// return Expression.MakeBinary(ExpressionType.Add, Expression.Constant(2), Expression.Constant(4));
case '-':
return Expression.MakeBinary(ExpressionType.Subtract, Expression.Constant(leftOp), Expression.Constant(rightOp));
case '*':
return Expression.MakeBinary(ExpressionType.Multiply, Expression.Constant(leftOp), Expression.Constant(rightOp));
case '/':
return Expression.MakeBinary(ExpressionType.Divide, Expression.Constant(leftOp), Expression.Constant(rightOp));
case '^':
return Expression.MakeBinary(ExpressionType.Power, Expression.Constant(leftOp), Expression.Constant(rightOp));
default:
return null;
}
}
static void Main(string[] args)
{
Console.WriteLine("Enter mathematical expression : ");
string input = "12+";
Stack<Expression> ex = new Stack<Expression>();
for (int i = 0; i < input.Length; i++)
{
var d = input[i];
if (!isOp(d))
{
Expression constacntNumberExpression = Expression.Constant(d);
ex.Push(constacntNumberExpression);
}
else
{
Expression popLeft = ex.Pop();
Expression popRight = ex.Pop();
BinaryExpression node= myMakeBiniray(d, popLeft, popRight);
ex.Push(node);
}
}
我很难理解问题的根源,
任何帮助将不胜感激!
将数字表达式添加到堆栈时,需要将输入从 char 转换为 int。
更改 Main 的这一部分:
var d = input[i];
if (!isOp(d))
{
Expression constacntNumberExpression = Expression.Constant(d);
ex.Push(constacntNumberExpression);
}
收件人:
var d = input[i];
if (!isOp(d) && Char.IsNumber(d))
{
var constantNumber = Char.GetNumericValue(d);
Expression constacntNumberExpression = Expression.Constant(constantNumber);
ex.Push(constacntNumberExpression);
}
我一直在尝试构建一个表达式树来评估 RPN 中的数学表达式。
我在尝试调用 MakeBinary 表达式来计算树中的数字时出现异常。
例外情况如下"System.InvalidOperationException: 'The binary operator Add is not defined for the types 'System.Char' and 'System.Char'.'"
这是我的代码:
public static bool isOp(char checkIt)
{
bool verify;
if(checkIt== '+'|| checkIt== '-'||checkIt== '*' || checkIt== '/'||checkIt== '^')
{
return verify= true;
}
else
{
return verify= false;
}
return verify;
}
public static BinaryExpression myMakeBiniray(char ops,Expression leftOp, Expression rightOp)
{
switch (ops)
{
case '+':
Expression left = leftOp;
Expression right = rightOp;
return Expression.MakeBinary(ExpressionType.Add, left, right);
// return Expression.MakeBinary(ExpressionType.Add, Expression.Constant(2), Expression.Constant(4));
case '-':
return Expression.MakeBinary(ExpressionType.Subtract, Expression.Constant(leftOp), Expression.Constant(rightOp));
case '*':
return Expression.MakeBinary(ExpressionType.Multiply, Expression.Constant(leftOp), Expression.Constant(rightOp));
case '/':
return Expression.MakeBinary(ExpressionType.Divide, Expression.Constant(leftOp), Expression.Constant(rightOp));
case '^':
return Expression.MakeBinary(ExpressionType.Power, Expression.Constant(leftOp), Expression.Constant(rightOp));
default:
return null;
}
}
static void Main(string[] args)
{
Console.WriteLine("Enter mathematical expression : ");
string input = "12+";
Stack<Expression> ex = new Stack<Expression>();
for (int i = 0; i < input.Length; i++)
{
var d = input[i];
if (!isOp(d))
{
Expression constacntNumberExpression = Expression.Constant(d);
ex.Push(constacntNumberExpression);
}
else
{
Expression popLeft = ex.Pop();
Expression popRight = ex.Pop();
BinaryExpression node= myMakeBiniray(d, popLeft, popRight);
ex.Push(node);
}
}
我很难理解问题的根源, 任何帮助将不胜感激!
将数字表达式添加到堆栈时,需要将输入从 char 转换为 int。
更改 Main 的这一部分:
var d = input[i];
if (!isOp(d))
{
Expression constacntNumberExpression = Expression.Constant(d);
ex.Push(constacntNumberExpression);
}
收件人:
var d = input[i];
if (!isOp(d) && Char.IsNumber(d))
{
var constantNumber = Char.GetNumericValue(d);
Expression constacntNumberExpression = Expression.Constant(constantNumber);
ex.Push(constacntNumberExpression);
}