1

I have a string like str = " 4+ 6 * 30"; I have to do an arithmatic operation on this using c#. My solution to this problem is:

string temp = " 4 + 6 * 5";
int firstNaum = 0;
int secondNum = 0;
int ThirdNum = 0;
int finalResults = 0;

//Spliting strings
string[] withoutOperator = temp.Split('\t',' ','*' , '+');

//Iterating strings 
int counter = 0;
foreach (var res in withoutOperator)
{
    if (!string.IsNullOrEmpty(res) && counter ==1)
    {
        firstNaum = Convert.ToInt32(res);
    }
    if (!string.IsNullOrEmpty(res) && counter== 4)
    {
        secondNum = Convert.ToInt32(res);
    }
    if (!string.IsNullOrEmpty(res) && counter == 7)
    {
        ThirdNum = Convert.ToInt32(res);
    }
    counter += 1;
}
finalResults = firstNaum + secondNum * ThirdNum;

Is there better way to do that?

Kris Vandermotten
  • 10,111
  • 38
  • 49
  • 2
    What should the answer be? `(4 + 6) * 5 = 50` or `4 + (6 * 5) = 34`? This is a lot more complex than it might look on the surface. – Jonny Jul 24 '14 at 14:51
  • 1
    This would be better asked on http://codereview.stackexchange.com/ – Mike Cheel Jul 24 '14 at 14:52
  • Take a look [Is there a string math evaluator in .NET?](http://stackoverflow.com/questions/355062/is-there-a-string-math-evaluator-in-net) and [Math Parser .NET](http://www.codeproject.com/Articles/274093/Math-Parser-NET) and [muparser](http://muparser.beltoforion.de/) – Soner Gönül Jul 24 '14 at 14:53
  • 1
    @Jonny in the absence of parentheses, order of operations is used, not very complex at all, at least in that regard. – Wobbles Jul 24 '14 at 14:53
  • Did you hard-code the string offsets? That looks like a bad idea. – usr Jul 24 '14 at 14:54
  • @Wobbles: Where in the question do you see that the order of operations is supposed to be used? – O. R. Mapper Jul 24 '14 at 14:55
  • @O.R.Mapper it doesn't need to be explicitly said, its implied, order of operations is a mathematical rule that is always in effect. – Wobbles Jul 24 '14 at 14:59
  • yes if its not in () it means default precedence shall be applied – user3826166 Jul 24 '14 at 15:01
  • So does your solution need to cater for bracketing? – Jonny Jul 24 '14 at 15:03
  • yes indeed this is not a good idea to hardcode the string offsets it should check if res has integer it should put it in variable thats the reason to put this question here perhaps there should be better solution than this – user3826166 Jul 24 '14 at 15:05
  • @Wobbles: I'm often surprised by how often something that "doesn't need to be explicitly said, its implied" needs to be explicitly stated. – Jonny Jul 24 '14 at 15:09
  • 2
    @Wobbles: But that's just the question - *which* order of operations should be applied? The one in which the operations are written? Or the one that is commonly applied (multiplication/division before addition/subtraction)? And the latter case is exactly where it gets complex, because the OP cannot just read the next operation and execute it step by step, but actually they need to build an expression tree and evaluate subtrees only when it is clear nothing more will be added. – O. R. Mapper Jul 24 '14 at 15:11
  • http://www.codeproject.com/Articles/18880/State-of-the-Art-Expression-Evaluation makes for interesting reading on this subject. – Michael McGriff Jul 24 '14 at 15:11
  • @O.R.Mapper there is only one order of operations, left to right is not proper math, it is always PEMDAS. – Wobbles Jul 24 '14 at 15:12
  • @Wobbles: According to [this paragraph](http://en.wikipedia.org/wiki/Order_of_operations#Programming_languages), some programming languages such as [Smalltalk](http://en.wikipedia.org/wiki/Smalltalk) apply alternative orders of operation rather than the standard order of operations. That said, I am not claiming that the OP is *not* trying to use the standard order of operations. I am primarily refuting your claim that doing so is not "complex" (compared to the level of complexity we are seeing in the question). – O. R. Mapper Jul 24 '14 at 15:14
  • @user3826166 in light of discussions on the order of precedence, I would just use a library to do this. Several are suggested in the discussion and answers. – Jonny Jul 24 '14 at 15:33

1 Answers1

4

You can do this very simply (a bit hackish...) like this:

string expression = "4 + 6 * 5";

DataTable dt = new DataTable();
var result = dt.Compute(expression, "");

Console.WriteLine(result);//34

This also handles order of operations correctly like so:

string expression = "(4 + 6) * 5";

DataTable dt = new DataTable();
var result = dt.Compute(expression, "");

Console.WriteLine(result);//50
Michael McGriff
  • 793
  • 10
  • 20