Automatic Math Sum Solver - C#

Hemanth B 886 Reputation points
2021-09-08T17:46:18.04+00:00

Hi, How do I write code for a c# app in such a way that it automatically simplifies a given math problem? Like for example, can it automatically simplify 9(6-3) which is 54 - 27 = 27 so that I don't have to manually provide the code saying

 if(operator == "-")
{
first - second;
}
else if(operator == "+")
{
first + second;
}

and so on. Instead I just provide "simply(userprovidedvalue)". How do I do that? Is there any way?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,219 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 81,481 Reputation points
    2021-09-08T18:41:24.87+00:00

    Maybe you can use the MS Script control, like :

    var scriptType = Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC"));
    dynamic sc = Activator.CreateInstance(scriptType);
    sc.Language = "VBScript"; 
    string sExpression = "9*(6-3)";
    // or for example
    // sc.ExecuteStatement("a = 9");
    // sc.ExecuteStatement("b = 6");
    // sc.ExecuteStatement("c = 3");
    // string sExpression = "a*(b-c)";
    object objResult = null;
    try
    {
        objResult = sc.Eval(sExpression);
    }
    catch (System.Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
    MessageBox.Show(objResult.ToString());
    
    0 comments No comments

  2. Timon Yang-MSFT 9,571 Reputation points
    2021-09-09T02:16:03.323+00:00

    I feel that your requirements are like expression trees. Is this what you want?

    This is an expression to calculate the Pythagorean Theorem.

            static void Main(string[] args)  
            {  
                LambdaExpression lambdaExpression = GetExpression();  
                var re=  lambdaExpression.Compile().DynamicInvoke(3, 4);  
                Console.WriteLine(re);  
                Console.ReadLine();  
            }  
            public static LambdaExpression GetExpression()  
            {  
                var xParameter = Expression.Parameter(typeof(double), "x");  
                var yParameter = Expression.Parameter(typeof(double), "y");  
      
                var xSquared = Expression.Multiply(xParameter, xParameter);  
                var ySquared = Expression.Multiply(yParameter, yParameter);  
      
                var sum = Expression.Add(xSquared, ySquared);  
                var sqrtMethod = typeof(Math).GetMethod("Sqrt", new[] { typeof(double) });  
                var distance = Expression.Call(sqrtMethod, sum);  
      
                var distanceLambda = Expression.Lambda(  
                                distance,  
                                xParameter,  
                                yParameter);  
                return distanceLambda;  
            }  
    

    Hope I did not misunderstand what you mean.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments