Diemauerdk
Diemauerdk

Reputation: 5948

Calculator in C# like in Perl

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:

  1. (2+2+2)*5
  2. cos(5)
  3. 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?

Upvotes: 0

Views: 539

Answers (2)

user694833
user694833

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:

Hope this helps

Upvotes: 2

walther
walther

Reputation: 13600

Yes, certainly there are options how to do this. Have a look at this for instance:

http://www.c-sharpcorner.com/UploadFile/mgold/CodeDomCalculator08082005003253AM/CodeDomCalculator.aspx

Upvotes: 1

Related Questions