How to make a Calculator using Dynamic Formula in C#

By FoxLearn 11/22/2024 2:06:38 PM   7.4K
Creating a calculator using dynamic formulas in a C# Windows Forms application involves several steps.

The idea is to allow users to input formulas dynamically, evaluate them, and display the results.

How to make a Calculator using Dynamic Formula in C#?

calculator using dynamic formula in c#

We'll walk you through creating a simple MyMath class that demonstrates how to evaluate dynamic mathematical formulas in a C# Windows Forms application. This implementation leverages the Mathematical.Expression library for parsing and evaluating formulas dynamically.

Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "DynamicFormula" and then click OK

Drag and drop the DataGridView and Button controls from the Visual Toolbox onto your form designer, then design your calculator form as shown below.

dynamic formula in c#

Create the MyMath class, this class will serve as the data structure for holding mathematical values and formulas.

public class MyMath
{
    public double a { get; set; }
    public int b { get; set; }
    public double c { get; set; }
    public string formula { get; set; }
    public double result { get; set; }
}

Next, You need to download Mathematical.Expression, this library is used to parse and evaluate mathematical formulas dynamically.

Populate the DataGridView with sample data when the form loads:

private void Form1_Load(object sender, EventArgs e)
{
    // Initialize a list of MyMath objects with sample data
    List<MyMath> list = new List<MyMath>();
    list.Add(new MyMath() { a = 2.1, b = 3, c = 4.5 });
    list.Add(new MyMath() { a = 3.5, b = 2, c = 3.5 });
    list.Add(new MyMath() { a = 4.2, b = 1, c = 1.5 });
    // Set the data source for the DataGridView
    dataGridView.DataSource = list;
}

Finally, handle the button click to evaluate formulas. When the btnCalculate button is clicked, iterate over the list of MyMath objects, parse their formulas, and evaluate the results dynamically.

private void btnCalculate_Click(object sender, EventArgs e)
{
    // Retrieve the data source from the DataGridView
    List<MyMath> list = dataGridView.DataSource as List<MyMath>;
    if (list != null)
    {
        foreach (MyMath math in list)
        {
            // Initialize the formula evaluator
            ExpressionEval eval = new ExpressionEval(math.formula);
            // Set the variable values
            eval.SetVariable("a", math.a);
            eval.SetVariable("b", math.b);
            eval.SetVariable("c", math.c);
            // Evaluate the formula and store the result
            math.result = (double)eval.Evaluate();
        }
        // Refresh the DataGridView to display updated results
        dataGridView.DataSource = list;
        dataGridView.Refresh();
    }
}

Build and run the application. The DataGridView will display a list of MyMath objects with initial values for a, b, c, and their formulas.

You can modify the formulas directly in the DataGridView. When you click Calculate, the results will be evaluated dynamically based on the entered formulas.

VIDEO TUTORIAL