question

HemanthB-9452 avatar image
0 Votes"
HemanthB-9452 asked TimonYang-MSFT commented

Automatic Math Sum Solver - C#

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?

dotnet-csharp
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I would do an internet search for an existing NuGet package that can parse a text based math equation that fits your requirements rather than writing your own.

EquationSolver

There are also math programs that have this capability which have been around a very long time. Again, just a Google search.


0 Votes 0 ·

Thanks @AgaveJoe , but how shall I use that in c#?

0 Votes 0 ·

@HemanthB-9452
A code example is provided in the GitHub repository of this package, which should help you understand how to use it.
JeffBramlett/EquationSolvr

0 Votes 0 ·
Castorix31 avatar image
0 Votes"
Castorix31 answered

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());


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.