Print expression values ​​as a string

I want to accept a mathematical expression that accepts variables and prints (assigns to a string) a formula with filled variables.

int iTwo = 2; int iResult = 0; iResult = iTwo * iTwo; string theString = (iTwo * iTwo).ToString(); 

In the above code iResult = 4 and theString = "4"

I would like to do something that fills the variables and returns a mathematical expression, for example:

 theString = (iTwo * iTwo).ExpressionToString(); 

and ultimately using String = "2 * 2";

Thoughts?

+6
source share
7 answers

You can use type Expression<> .

 public static string ExpressionToString<T>(Expression<Func<T>> e) { var un = e.Body as BinaryExpression; if (un != null) { var left = un.Left.ToString(); var leftEnd = left.Split('.').LastOrDefault(); var right = un.Right.ToString(); var rightEnd = right.Split('.').LastOrDefault(); return e.Body.ToString().Replace(left, leftEnd).Replace(right, rightEnd); } return e.Body.ToString(); } Console.WriteLine(ExpressionToString(() => iTwo * iTwo)); //prints (iTwo * iTwo) 

You will need to make the method more complex in order to parse things more complex than a simple binary expression, but this is a general idea. You can just do e.Body.ToString() , but because of the way anonymous types are created for your lambdas, it can lead to ugly results, for example: "(value(TestApp.Program+<>c__DisplayClass3).iTwo * value(TestApp.Program+<>c__Dis playClass3).iTwo)" .

+4
source

When overloading some operators ...

 class Expression { string exprStr; public static explicit operator Expression(int value) { return new Expression() { exprStr = value.ToString() }; } public static Expression operator *(Expression exp, int value) { return new Expression() { exprStr = exp.exprStr + " * " + value.ToString() }; } public override string ToString() { return exprStr; } } class Program { static void Main() { int iTwo = 2; string theString = ((Expression)iTwo * iTwo).ToString(); } } 

You, of course, will need to overload the other operators that you need in the same way (e.g. + , / , etc.).
You should also provide methods that accept types other than int if you need them, but the basic idea remains the same.

Please note that you should only give the first operand to Expression, otherwise you will only get the result for the conversion.

+1
source

You can use Expression<> and the parameter information stored in the expression to get what you want. Save the expression in the Expression<> variable.

 int n1 = 4; int n2 = 3; Expression<Func<int, int, int>> exp = (arg1, arg2) => arg1 * arg2; string expString = exp.ToString(); // (arg1, arg2) => arg1 * arg2 int startRHS = expString.IndexOf("=>") + 2; // starting index of RHS string onlyRHS = expString.Substring(startRHS).Trim(); // arg1 * arg2 // replace args with values string withValues = onlyRHS.Replace(exp.Parameters[0].Name, n1.ToString()); // 4 * arg2 withValues = withValues.Replace(exp.Parameters[1].Name, n2.ToString()); // 4 * 3 

Although this is not the most reliable solution, it works for simple scenarios.

+1
source

You can create your own method that would call something like this:

 public string ExpressionToString(int parameter1, string operator, int parameter2) { return string.Format("{0} {1} {2}", parameter1, operator, parameter2); } 
0
source

Please use create your own method and use the code below to achieve your requirement

 string answer = string.Format("{0} {1} {2} = {3}", iTwo, iTwo, operator, result); 
0
source
 string ExpressionToString(int first, string operator, int second) { StringBuilder sb = new StringBuilder(); sb.Append(first.ToString()); sb.Append(operator); sb.Append(second.ToString()); return sb.ToString(); } 
0
source
 public static string ExpressionToString(params object[] param) { return string.Join(" ", param.Select(t => t.ToString()).ToArray()); } 

via:

 string result = Helper.ExpressionToString(2, "*", 2 , ">=", -10); //result = "2 * 2 >= -10"; 
0
source

Source: https://habr.com/ru/post/919801/


All Articles