Search code examples
c#parsingintegerformula

How to convent string math formula to int at run time in c#


I need to parse math formula with condition at run time in C#. For example

string code = "1+ 1 + 1*1 * ((5+4==8)?2:0 )";
int value = int.parse(code);  // value should be 2

I was able to parse math formula without any ternary operator. Im having problem with the ternary operator: string code = "1+ 1 + 1*1 * ((5+4==8)?2:0 )";

Any help would be appreciated


Solution

  • @Rand Random, @Charlieface

    First of all, if you come across a question in a language you don't know, I recommend you to skip it. Do not create invalid claims!

    int.parse(); is used to convert a string integer to an integer value.

    But clearly in this case this line =>

    int value = int.parse(code);  // value should be 2
    

    case's an error, (because code is not an integer)

    And to do what I was asking u don't need a compiler. I actually did it without it!!

        using static System.Math;
        using Project = System.IO;
        using System.Linq;
        using System.Collections.Generic;
        using System.Text.RegularExpressions; 
        class YesICanWithoutACompiler
                    {
                        static void Main(string[] args)
                        {
            
                            string code = "1+ 1 + 1*1 * (((((((3!=63))?(2==4?5:1):7))==8)?2:(4==6?7:3) ))";
            
                            string formula = getFormular(code);   // outputs: 1+ 1 + 1*1 * (3)
            
                            Console.WriteLine("yes I can!: " + new System.Data.DataTable().Compute(formula, ""));
                            Console.ReadLine();
            
                        }
                        static int IndexOfAny(string source, string[] items, int startat = 0)
                        {
                            List<int> indexes = new List<int>();
                            for (int f = 0; f < items.Length; f++)
                            {
                                int loo = source.IndexOf(items[f], startat);
                                if (loo != -1)
                                {
                                    indexes.Add(loo);
                                }
                            }
            
                            if (indexes.Count > 0)
                            {
                                return indexes.Min();
                            }
                            else
                            {
                                return -1;
                            }
            
            
                        }
            
            
                        static bool conditoin(int x, string mark, int y)
                        {
                            switch (mark)
                            {
                                case "!=":
                                    return (x != y);
                                case "==": return (x == y);
                                case ">=": return (x >= y);
                                case "<=": return (x <= y);
                                default: return false;
                            }
            
                        }
            
                        static string getFormular(string code)
                        {
                            int ind = IndexOfAny(code, new string[] { "==", ">=", "<=", "!=" });
            
                            while (ind != -1)
                            {
            
            
            
                                int condition1 = int.Parse(string.Join("", code.Substring(0, ind).Reverse().SkipWhile(c => !char.IsDigit(c)).TakeWhile(d => char.IsDigit(d))));
                                string conditioin = code.Substring(ind, 2);
            
                                int condition2 = int.Parse(string.Join("", code.Substring(ind + 2).TakeWhile(d => char.IsDigit(d))));
            
            
                                string arvo = "";
                                if (conditoin(condition1, conditioin, condition2))
                                {
                                    arvo = code.Substring(1 + code.IndexOf("?", ind + 2)).Trim();
            
                                    if (arvo.StartsWith("("))
                                    {
            
            
                                        int ndent = 1;
                                        int h = 0;
                                        while (h < arvo.Length && ndent != 0)
                                        {
                                            h++;
            
            
                                            if (arvo[h] == ')') ndent--;
                                            if (arvo[h] == '(') ndent++;
                                        }
            
                                        arvo = arvo.Substring(0, h + 1);
            
            
                                    }
                                    else
                                    {
                                        arvo = string.Join("", arvo.TakeWhile(s => s != ':'));
            
                                    }
            
            
            
                                }
                                else
                                {
            
                                    arvo = code.Substring(1 + code.IndexOf(":", ind + 2)).Trim();
            
                                    if (arvo.StartsWith("("))
                                    {
            
            
                                        int ndent = 1;
                                        int h = 0;
                                        while (h < arvo.Length && ndent != 0)
                                        {
                                            h++;
            
            
                                            if (arvo[h] == ')') ndent--;
                                            if (arvo[h] == '(') ndent++;
                                        }
            
                                        arvo = arvo.Substring(0, h + 1);
            
                                    }
                                    else
                                    {
                                        arvo = string.Join("", arvo.TakeWhile(s => s != ')'));
            
                                    }
            
            
            
            
            
            
            
            
                                }
            
                                int f = code.IndexOf("?", ind);
                                int indent = 0;
                                while (f > 0 && indent != -1)
                                {
                                    f--;
                                    if (code[f] == ')') indent++;
                                    if (code[f] == '(') indent--;
                                }
            
                                int t = f;
                                indent = 1;
            
                                while (t < code.Length && indent != 0)
                                {
                                    t++;
                                    if (code[t] == ')') indent--;
                                    if (code[t] == '(') indent++;
                                }
            
                                code = code.Remove(f, (t - f) + 1).Insert(f, arvo);
            
                                ind = IndexOfAny(code, new string[] { "==", ">=", "<=", "!=" });
            
                            }
                            return code;
                        }
    
     }