Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hai. I'm new in programming. May I know is there a way to evaluate mathematical equations that I had stored in mySQL database together with its variables in C#.NET?
For example, in my database, there is one equation let say x+b+x+d. The variables stored are x=1, b=2 and d=2. How am I going to evaluate this equation? Any clue that I should find. I'm looking for tutorial online but still cannot find it. I do not know the right keywords. Thanks
Posted
Comments
walterhevedeich 21-Nov-13 21:15pm    
How complex are the equations? Can't you just replace the equations with the given values using String.Replace()?
Sergey Alexandrovich Kryukov 21-Nov-13 21:18pm    
What's wrong with just reading of some elementary tutorial? Please read it, and ask a question if you have some doubt.
This is not "equation", this is a numerical expression...
You can either do a calculation in SQL (but why?) or write a query to get all the numbers using, say, the names of columns/tables and calculate the expression in C#. What could be unclear?
—SA
Nooa 21-Nov-13 21:19pm    
why dont you store the values in the database and the calculations in your program?

otherwhise you have to create a huge logical interpreter to identyfy the calculations which have to be done.
Peter Leow 21-Nov-13 22:40pm    
Your question seems to suggest that you are creating some kind of question bank in the database. Am I right?

Usually, we store values in database and write programs in the form of methods/functions to perform calculations. We retrieve values from database using SQL and pass them to the appropriate methods/functions to process and get the results.

1 solution

The easy problem is replacing variable names with the values from your program. You should read up on Regular Expressions which are perhaps the most appropriate tool for that purpose.

The hard part is evaluating the expression after you've put values where variable names were. For that, you have a number of options:

1. Find a third-party numerical expression evaluation program to which you can pass a string that is the expression to evaluate; write your program to run that program, giving it the expression with its variables replace.

2. Use SQL to do that evaluation. The statement SELECT 1+2*3 AS [Result] (that is valid Microsoft T-Sql; for MySql there may be minor differences) will evaluate your answer for you. But, you'll have to ensure that none of your expressions use operators or functions that SQL can't understand, or that are SQL aggregation operators.

3. Hard, especially since you're new to programming: translate your database-stored expressions into .Net expression trees. Essentially, this is dynamically creating a .net program from a text string.

4.You could also look into Reflection (look for Emit) which can be used to generate .Net classes which you can then compile and link to. Probably far more work than you need, but it's always there as an option.

5. Simpler than Nos. 3 and 4, if you have a well-defined set of operators - implement your own calculation engine. You'll have to dynamically parse the expressions (or, save cached pre-parsed expressions when they get added to the database) and build a graph of objects that your engine can traverse, calculating a result as it goes.

None of these options is without pitfalls. If your expressions are strings typed by a user they are almost guaranteed to have random errors - unbalanced parentheses, strange characters that don't belong in a mathematical expression, etc. Whichever approach you take, you'll have to handle the errors that arise from such expressions.

If you're in a hurry, No. 1 is probably your best option.

If you can't find a solution to No. 1, No. 2 is the next easiest (actually, maybe easier than No. 1).

No. 3 is the most technically challenging. If you have the time and inclination it will be very rewarding.

No. 4 I really don't recommend. It'll generate a lot of executable code (DLLs) lying around, there'll be a lot to learn, and there's a risk that you'll fall into the error of "when the only tool you have is a hammer, everything is a nail."

No. 5 would be fun, easier than No. 3, and will teach you some valuable things about data structures and algorithms (and, will help you understand what's going on if you try No. 3).

Have fun!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900