Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, August 20, 2009 10:12 AM
Hi
I am working a C#.NET 3.5 application. I want to create complex formula using the values shown on the screen and save in XML or Database.
Eg:
Formula1 = (Value1 * Value2 + Value3)/Value6 - Value5
Formula2 = If (Value1 > 1000) then (Value1 * Value2 + Value3)/Value6 - Value5
else (Value3 * Value2 + Value1)/Value5 - Value6
For the given values, User should have an option to select the foruma from the list and do the calculation.
I also need an option to select the same formula and edit and then save.
How to create and save formulas using C# code?
How to give options to users to add operators like +,-,*,/ and braces while creating the formula?
Thanks
Ashok
All replies (6)
Thursday, August 20, 2009 2:47 PM ✅Answered | 2 votes
You can also take a look at .NET 3.5 Expressions.
To generate an expression for
20.3 * Cos(30.2) + 20.1
You would say
Expression ex = Expression.Add(
Expression.Multiply(
Expression.Constant(20.3),
Expression.Call(null, typeof(Math).GetMethod("Cos"), Expression.Constant(30.2))
),
Expression.Constant(20.1)
);
The variable Ex is now an expression tree as shown by Viral.
Unfortunetely, expression only support upto 4 variables. The example I have shown does not use any variables, but you can find examples for it.
If you want to run the expression to get the result, you need to make a lambda expression delegate, like so
Expression<Func<double>> lEx = Expression.Lambda<Func<double>>(ex, new ParameterExpression[0]);
Now you can run the expression with the following
lEx.Compile().Invoke();
When you create lEx, you can pass the variables in the call. If you want to create a variable, you can use something like
ParameterExpression x = Expression.Parameter(typeof(double), “x”);
which you can then use in the expression tree and in the creation of lEx.
The expression tree Ex can be traversed and then convered into a XML file or database table, you can then write a function to convert the XML back into the Expression tree in a similar fashion shown above.
Craig
Hope this helps abit.
Btw, .NET 4.0 has a whole host of new features for expression trees.
Thursday, August 20, 2009 10:54 AM | 1 vote
You should think of a formula as tree Structure
For example (a * b) + (c / d)
can be thought of as
+
*
a
b
/
c
d
Thanks,
Viral.
Thursday, August 20, 2009 2:01 PM | 1 vote
Hi,
I hope it will help you.
private void Form1_Load(object sender, EventArgs e)
{
//Create 4 combo box with Formulla Conditions
FillCombo();
}
private void FillCombo()
{
cmbValues.Items.Add("Values");
cmbValues.Items.Add("value1");
cmbValues.Items.Add("Value2");
cmbValues.Items.Add("Value3");
cmbValues.SelectedIndex = 0;
cmbOperators.Items.Add("opeartors");
cmbOperators.Items.Add("+");
cmbOperators.Items.Add("-");
cmbOperators.Items.Add("*");
cmbOperators.SelectedIndex =0;
cmbValues2.Items.Add("Values");
cmbValues2.Items.Add("value1");
cmbValues2.Items.Add("Value2");
cmbValues2.Items.Add("Value3");
cmbValues2.SelectedIndex = 0;
cmbConditions.Items.Add("Conditions");
cmbConditions.Items.Add("If");
cmbConditions.Items.Add("Else");
cmbConditions.Items.Add("OR");
cmbConditions.SelectedIndex =0;
}
private void btnFormula_Click(object sender, EventArgs e)
{
StringBuilder str = new StringBuilder();
String space = " ";
//Added ~ for single string Formulla
string seperator = "~";
str.Append(cmbConditions.SelectedItem.ToString());
str.Append(seperator);
str.Append(cmbValues.SelectedItem.ToString());
str.Append(seperator);
str.Append(space);
str.Append(cmbOperators.SelectedItem.ToString());
str.Append(seperator);
str.Append(space);
str.Append(cmbValues2.SelectedItem.ToString());
str.Append(space);
MessageBox.Show(str.ToString());
SaveFormulla(str.ToString());
}
private void SaveFormulla(string formula)
{
//save ur formulla to DB or File..
StreamWriter wrter = new StreamWriter(@"c:\\text.txt");
wrter.WriteLine(formula);
wrter.Close();
}
private void LoadFormulla()
{
//Get the saved Formulla
StreamReader rdr = new StreamReader(@"c:\\text.txt");
string line = rdr.ReadLine();
rdr.Close();
if (!String.IsNullOrEmpty(line))
{
String[] lua=line.Split('~');
//Load Values To Combo box
}
}
private void button1_Click(object sender, EventArgs e)
{
//Load the Formulla again Combo
LoadFormulla();
}
Best Regards, C.Gnanadurai Please mark the post as answer if it is helpfull to you
Thursday, August 20, 2009 3:42 PM
Hi Gnanadurai,
After saving the formula i also need to bind the values to variables in the formula and calculate the value.
If A*B + C is the formula, i need to set A=10 ,B =20 and C =50 and calculate the value using the formula.
how to bind values to the formula and calculate the result?
thanks
Ashok
Friday, August 21, 2009 1:33 PM
Thanks Craig. Your post was helpful
Ashok
Wednesday, December 7, 2011 3:02 PM
I don't know if this is too late, but I was looking for a similar requirement and found this wonderful library which does most of simple arithmetic. It does not however include complex math functions.. It solved my problem.
http://www.codeproject.com/KB/recipes/dynamicformula.aspx
Hope this helps..
Reagrds
Krishna J Vedula