Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String calculator [closed]

Hi fellow programmers,

I am creating a calculator in C#

and I have a string variable math which contains 100 * 5 - 2

How can I display its output which is 498 in my console?

My code is this:

String math = "100 * 5 - 2"; 

Console.WriteLine(math);

Console.ReadLine(); // For Pause

So basically, what my code will give me is the string itself which 100 * 5 - 2

but I want it to give me 498 as a result.

Idea about this is pretty much appreciated.

Thanks

like image 265
GM-Xile GM-Xile Avatar asked Feb 22 '14 05:02

GM-Xile GM-Xile


2 Answers

Regular Expression evaluation can be done using DataTable.Compute method (from MSDN) :

Computes the given expression on the current rows that pass the filter criteria.

Try this:

using System.Data;//import this namespace

 string math = "100 * 5 - 2";
 string value = new DataTable().Compute(math, null).ToString();
like image 74
Sudhakar Tillapudi Avatar answered Nov 13 '22 06:11

Sudhakar Tillapudi


Simply try this

String math = (100 * 5 - 2).ToString(); 

I don't know, Why you want more complex? It's very easy ..

And if you want surely that,You can do that by using EvaluateExpression

public int EvaluateExpression(string math )
    {
       return Convert.ToInt32(math);
    }

........................

String math = "100 * 5 - 2"; 

int result = EvaluateExpression(math );

Console.WriteLine(result );

See this discussions

Evaluating string "3*(4+2)" yield int 18

Update:

If those values came from input textbox, then write this way

String math = txtCalculator.Text.Trim();

    int result = EvaluateExpression(math );

    Console.WriteLine(result );

And also you can find out some pretty answer from this discussion

Is it possible to compile and execute new code at runtime in .NET?

Update 2:

Finally I have tried this sample for you :

My full code for class library

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.XPath;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String math = "100 * 5 - 2";

        Console.WriteLine(Evaluate(math));
    }

    public static double Evaluate(string expression)
    {
        var xsltExpression =
            string.Format("number({0})",
                new Regex(@"([\+\-\*])").Replace(expression, " ${1} ")
                                        .Replace("/", " div ")
                                        .Replace("%", " mod "));

        // ReSharper disable PossibleNullReferenceException
        return (double)new XPathDocument
            (new StringReader("<r/>"))
                .CreateNavigator()
                .Evaluate(xsltExpression);
        // ReSharper restore PossibleNullReferenceException
    }
}
like image 37
Ramesh Rajendran Avatar answered Nov 13 '22 06:11

Ramesh Rajendran