0

Possible Duplicate:
Is there a method that will evaluate a string and produce an integer (assuming the string is an equation) in C#

Hi all, i just wonder how to make a realtime complination in C# For example: i have a string like this

string math = "1 + 2 + (4 - 6)";

And i want to complie it to get the result How to do that? and is that the bad idea because i want to make a calculator in C#?

Edited: The main question properly is that i want to do it in WP7, not exactly in C# windows lol, i tried all the solutions below but not at all is correct!

Community
  • 1
  • 1
Hieu Nguyen Trung
  • 1,624
  • 5
  • 21
  • 32
  • 7
    I always complinate in real time – Ed S. Dec 12 '10 at 04:58
  • http://forums.techarena.in/software-development/1109842.htm ? – Pauli Østerø Dec 12 '10 at 04:58
  • Also see: http://stackoverflow.com/questions/355062/is-there-a-string-math-evaluator-in-net, http://stackoverflow.com/questions/2607798/converting-string-expression-to-integer-value-using-c, http://stackoverflow.com/questions/333737/c-evaluating-string-342-yield-int-18, http://stackoverflow.com/questions/1437964/best-and-shortest-way-to-evaluate-mathematical-expressions, http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-code-fragments – Cody Gray - on strike Dec 12 '10 at 05:03
  • Similar question: https://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-code-fragments – Andrey Dec 12 '10 at 05:00
  • Several hints there, including a possible code that solves the problem : http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-csharp/31854/How-to-evaluate-a-math-expression-contained-in-a-string – almathie Dec 12 '10 at 05:02

2 Answers2

0

and is that the bad idea because i want to make a calculator in C#?

One problem with that is that your calculator language is probably supposed to be just a subset of C#. So using the C# compiler may be too flexible and allow arbitrary C#. Kind of like problems with SQL injection attacks.

Mike S
  • 3,058
  • 1
  • 22
  • 12
0

Expresion Evalution is an application of STACK (Data Structure)

You can see these link If you want Sample projects

  1. http://www.vbforums.com/showthread.php?t=397264
  2. http://www.codeproject.com/KB/cs/runtime_eval.aspx
  3. http://www.c-sharpcorner.com/uploadfile/mgold/codedomcalculator08082005003253am/codedomcalculator.aspx
Javed Akram
  • 15,024
  • 26
  • 81
  • 118