Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai. I was thinking how to evaluate mathematical equations based on user defined variable separately. for example
let say user input (y4df +rew)/4dwcf*rte
when user click evaluate, it will prompt user to input values for variables as below
y4df = ?
rew= ?
4dwcf=?
rte= ?

any suggestion for solve this problem or tutorial. i plan to do it in vb.net
tq.
Posted
Comments
OriginalGriff 6-Mar-13 14:11pm    
And what have you tried?
Where are you stuck?

I have an alternative suggestion: you can put an expression string in appropriate C# or VB.NET program text, build it using CodeDOM (present compilation errors if any), load the resulting assembly in an Application Domain, execute the method containing the expression, if required, present the result (in your original Application Domain of you UI), delete the Application Domain. It works very quickly, and those compilers are a part of .NET Framework, they are always available to the user and are invoked via CodeDOM.

Also, this way is very, very reliable, it relies on available well debugged and tested compilers and CLR.

I provided detailed explanations and further referenced in my past answers:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^].

—SA
 
Share this answer
 
Comments
Matt T Heffron 6-Mar-13 18:11pm    
Sergey, the OP's example seemed to have user-defined variables (as opposed to pre-defined) and not necessarily valid C#/VB identifier names! So, even if he used this way to evaluate the expression, he would still need to identify the variables and query for the values.
Can using CodeDOM help in this part of his problem? (Probably not unless invalid identifiers were excluded...)
Sergey Alexandrovich Kryukov 6-Mar-13 18:20pm    
Yes, CodeDOM is the perfect robust solution. This is as you wrote C# or VB.NET code. I have such a calculator. The user writes only the central part of the code, and I wrap it in more text in the application (all those using, the declaring class, depending on the goal of the application). When the compilation errors are shown, the line numbers from a compiler are translated accordingly. The users get the full illusion that she/he only writes expressions and some output. The result of output is collected and shown in the UI, for example. It makes a very compact project...
—SA
arave0521 6-Mar-13 21:54pm    
I'm quite new in using codeDom. im not really understand about it related to my problem.thanks
Sergey Alexandrovich Kryukov 6-Mar-13 22:26pm    
OK, you are just saying that you are a beginner. As I always say, the beginner is the one who begins, not the one who uses it as an excuse. If you want to solve the problem, you just need to get to understanding of CodeDOM and Application Domain. Or expression parsers and evaluators.

"There is no try. Do it or do not".

However, you can try this and that, and get required knowledge and experience, and solve the problem. Of not. If you dig through all my references (there are many more than these two), you will be able to see all the API you need and understand how it works. Do you think there is another way? To copy some code, without understanding it, and make it work? :-)

Cheers,
—SA
I would suggest using regular expressions to find the mathematical operators and split the string on those.

System.Text.RegularExpressions[^]

It looks like it has a Split Method[^]. I've never used it before...I've used the Replace Method before. But I'm sure you can google for more specific examples once you get started trying it out.

If you haven't ever worked with Regular Expressions before, you might want to research that first too. Here is one (of probably many) articles on CP that could help get you started: The 30 Minute Regex Tutorial[^]
 
Share this answer
 
I suggest you to search with the key word "Pascal Ganaye evaluator". His code (vb.net) will help you, definitely.
 
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