Reputation: 5948
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:
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?
Upvotes: 0
Views: 539
Reputation:
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
Upvotes: 2
Reputation: 13600
Yes, certainly there are options how to do this. Have a look at this for instance:
Upvotes: 1