1

I need to make a form based calculator in Visual Studio Web Developer (Asp C#) I have a text-box that shows values of the clicked button (including +, -, /, *)...

Now i want it to calculate the string value of that text-box, if that is possible...

Edit: I know that JS has a very easy way of doing this using eval(stringholdername); but I can't seem to find any equivalent to this for ASP C#

Dino Prašo
  • 569
  • 4
  • 19
  • Asp-Classic is VB Script. – Diodeus - James MacFarlane Mar 14 '13 at 17:37
  • it is possible and should be very simple. – Mike C. Mar 14 '13 at 17:37
  • 2
    Asked more times than I can fit in this box; [c# evaluating string "3\*(4+2)" yield int 18](http://stackoverflow.com/questions/333737/), [Compile and execute new code at runtime?](http://stackoverflow.com/questions/234217/), [Shortest way to evaluate mathematical expressions](http://stackoverflow.com/questions/1437964/), ... – Dour High Arch Mar 14 '13 at 17:44

2 Answers2

0

Look into NCalc, it's an open source mathematical expression evaluator.

Chris
  • 2,807
  • 16
  • 25
  • isn't there any way to do it without plug-ins because i don't know if i am allowed to use theml. – Dino Prašo Mar 14 '13 at 17:38
  • @DinoPrašo It's not a "plug in", it's a library DLL you can simply leverage. If this is a school assignment you should mention that so we don't give you the simple answer of "don't re-invent the wheel, use this, this, or this instead!" – Chris Sinclair Mar 14 '13 at 17:51
0

You could use some simple recusive method like this:

    public int Calculate(string expression)
    {
        int result = 0;
        string[] expressions = expression.Split('+');
        if (expressions.Length > 1)
        {
            result = 0;
            foreach (string expr in expressions)
                result += Calculate(expr);
            return result;
        }
        expressions = expression.Split('-');
        if (expressions.Length > 1)
        {
            result = Calculate(expressions[0]);
            for (int i = 1; i < expressions.Length; i++)
                result -= Calculate(expressions[i]);
            return result;
        }
        expressions = expression.Split('*');
        if (expressions.Length > 1)
        {
            result = 1;
            foreach (string expr in expressions)
                result *= Calculate(expr);
            return result;
        }
        expressions = expression.Split('/');
        if (expressions.Length > 1)
        {
            result = Calculate(expressions[0]);
            for (int i = 1; i < expressions.Length; i++)
                result /= Calculate(expressions[i]);
            return result;
        }

        if (!int.TryParse(expression, out result))
            throw new ArgumentException("Expression was not nummeric", "expression");

        return result;
    }

It is very simple and it doesn't support ( and ) and negative number, it only supports +, -, * and /.

You use it like this:

int result = Calculate("20*10+200/100");

Good luck with your quest.

Casperah
  • 4,246
  • 17
  • 13