0

How to perform this task?

int Amount, x=1000,y=200;
string BasedOn="x*12/100+y*5/100";
//In place of x and y in BasedOn I want to  replace with x,y values like (1000*12%+200*5%) 
//and the calculated result(130) should be assigned to Amount variable

For now, I split the BasedOn string

string[][] strings = BasedOn
    .Split(new char[] { '+' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(w => w.Split('*').ToArray())
    .ToArray();

What to do next? Please help me.

5 Answers 5

1

I made your code more flexible

    static void Main(string[] args)
    {
        Dictionary<string, object> variables = new Dictionary<string, object>();
        variables.Add("x", 1000);
        variables.Add("y", 200);
        string equation = "x*12/100+y*5/100";
        var result = Calculate(variables, equation);
    }
    static object Calculate(Dictionary<string, object> variables, string equation)
    {
        variables.ToList().ForEach(v => equation = equation.Replace(v.Key, v.Value.ToString()));
        return new DataTable().Compute(equation, string.Empty);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You can take a look on the DataTable.Compute method. It can be used like this on your case:

using System.Data;

DataTable dt = new DataTable();
var Amount = dt.Compute("1000*12%+200*5%","");

For replacing "x" and "y" with numeric values you can use string.Replace

Comments

0

Take a look NCalc (http://ncalc.codeplex.com/)

Expression e = new Expression("2 + 3 * 5");
Debug.Assert(17 == e.Evaluate());

Comments

0

If you want to replace the string AND calculate the result, you can do:

int Amount, x=1000,y=200;
string BasedOn=$"{x}*12/100+{y}*5/100";

DataTable dt = new DataTable();
var v = dt.Compute(BasedOn,"");

v will be your result (130).

EDIT: You have to replace your %'s with division by 100, as the datatable thinks its a mod operator.

3 Comments

@mybirthname Fixed.
@UmutSeven getting SyntaxExceptionError(Cannot interpret token '"' at position 2) at var v = dt.Comput(BasedOn,"")
@snehanagaruru Thats weird, it works in my machine. Try string.Empty instead of double quotes ("")?
0

With DataColumn.Expression:

int Amount, x = 1000, y = 200; string BasedOn = "x*12%+y*5%";

var dt = new DataTable();
dt.Columns.Add("x", typeof(int));   // in Visual Studio 2015 you can use nameof(x) instead of "x"
dt.Columns.Add("y", typeof(int));
dt.Columns.Add("Amount", typeof(int), BasedOn.Replace("%", "/100")); // "x*12/100+y*5/100" ("%" is considered modulus operator)

Amount = (int)dt.Rows.Add(x, y)["Amount"];  // 130

With RegEx.Replace and DataTable.Compute:

string expression = Regex.Replace(BasedOn.Replace("%", "/100"), "[a-zA-Z]+",
    m => (m.Value == "x") ? x + "" : (m.Value == "y") ? y + "" : m.Value);  // "1000*12/100+200*5/100"

Amount = (int)(double)new DataTable().Compute(expression, ""); // 130.0 (double)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.