-3

How do i Evaluate the following expression from a string to a answer as an integer?

Expression:

√(7+74) + √(30+6)

Do i have to evaluate each one of the parameters like Sqroot(7+74) and Sqroot(30+6) or is it possible to evaluate the whole expression. Any ideas?

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
parek
  • 772
  • 1
  • 13
  • 41
  • 3
    Could you elaborate a little more on the context? Do you have a parser? You've tagged the question "c" and "C#" - which is it? – Rune Mar 19 '12 at 10:52
  • possible duplicates: http://stackoverflow.com/questions/2859111/c-sharp-math-calculator – Rotem Mar 19 '12 at 10:52

2 Answers2

2

If this string is user-supplied (or anyway available only at runtime) what you need is a mathematical expressions parser (maybe replacing the √ character in the text with sqrt or whatever the parser likes before feeding the string to it). There are many free ones available on the net, personally I used info.lundin.math several times without any problem.

Quick example for your problem:

info.lundin.Math.ExpressionParser parser = new info.lundin.Math.ExpressionParser();
double result = parser.Parse("sqrt(7+74)+sqrt(30+6)", null);

(on the site you can find more complex examples with e.g. parameters that can be specified programmatically)

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • Could be an even better answer if you showed how to use the parser you link to – Rune FS Mar 19 '12 at 10:57
  • @RuneFS: the linked page contains four lines of text presenting the parser and then an example immediately follows, I don't think that copy-pasting it would be particularly useful. Also, that particular parser is just an example, I thought that this question is of the category "I'm looking for something that does this but I can't find it because I don't know its name", now that he knows that he's looking for a parser he'll be able to find an even better one. – Matteo Italia Mar 19 '12 at 11:07
  • that particular link might become invalid and does not help in indexing for some one searching something that fits your answer but not the question and similar. but it's only a suggestion for improvement not critisism – Rune FS Mar 19 '12 at 14:27
  • @RuneFS: if the link becomes invalid, then the example would be pointless, since the library itself would not be downloadable. :) By the way, I'm sorry if I seemed harsh, it wasn't in my intentions. – Matteo Italia Mar 19 '12 at 18:40
  • why would it be pointless. The change could be that the site switched to say php instead of asp or more likely switched to asp.net mvc rather than asp.net in either case the url would change but the ressource would still be available and knowing the name would make it possible for people that already had the module to find information here. No harm done :) – Rune FS Mar 19 '12 at 19:22
  • @RuneFS: you've got a point, I added a small example and specified the name of the class. :) – Matteo Italia Mar 19 '12 at 19:28
1

You can use NCalc for this purpose

NCalc.Expression expr = new NCalc.Expression("Sqrt(7+74) + Sqrt(30+6)");
object result = expr.Evaluate();
L.B
  • 114,136
  • 19
  • 178
  • 224