Frage

I've been trying to create a database of equations that my windows form application will supply variables to and then calculate the solution. I was trying to store my equations in a text file and load them into a list but I haven't been able to think of how to translate my equation from a string to an actual equation.

So, does anyone know how I could store and call on a collection of equations that use variables from my windows form application.

P.S. Please keep it simple or explain thing fully. I get confused easily.

War es hilfreich?

Lösung

Your best bet is to use a math library to evaluate your math expressions.

Here are some links to get you started: https://stackoverflow.com/questions/1387430/recommended-math-library-for-c-net

You can also take a look in http://ncalc.codeplex.com/ that may be exaclty what you want.

Andere Tipps

You can do this using dynamic SQL. Here is how in SQL Server.

You can store the equation in a table, along with the variables that it needs (or if you have a fixed set of variables, that's fine).

Fetch the equation into a C# string.

Then construct the following statement:

select <equation>
from (select <val1> as variable1, <val2> as variable2) t

Where you put in the equation where is and the values of the variables where the values are.

Execute this statement in the database. Voila! The result will be the result of the equation.

There are some downsides to this approach. It requires two round trips to the database (one to get the equation and the other to run the equation). You are also limited to the functionality of the database. Also, if you are not using SQL Server, the syntax could be different (for instance, in Oracle, the inner subquery would have "from dual" in it).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top