I want to develop a calculator in C#. I have been programming Perl a bit. In Perl, I can use string eval in the following way:
while(<STDIN>) {
$i = $_; chomp($i); unless($i) { last }
$o = eval($i); print "$i = $o\n";
}
With this code, I can enter:
- (2+2+2)*5
- cos(5)
- sqrt(4)
It recognizes a lot of different expressions.
I am not very familiar with C# – so my question is:
Can I do something like the above just in C# instead of Perl?
Unfortunately there is not a straight way to do it as in Perl, since C# is a compiled language, it cannot interpret souce code strings on-the-fly.
So, you have at least two alternatives:
Use an expression evaluator, like the one here: http://flee.codeplex.com or http://ncalc.codeplex.com There are plenty as well in codeproject.com searching for the phrase “expression evaluator”
Hope this helps