예제 #1
0
        public static void ReadEquation()
        {
            {
                try
                {
                    string        equation = "(a4234) * (3)  + 12432 + b123";
                    List <string> operands = FindOperands(expression: equation);

                    // Remove the numaric values
                    var variables = operands.Where(p => p.Any(char.IsLetter)).ToArray();

                    DataTable dt = new DataTable();
                    // create columns with type
                    dt.AddColumnsWithType(typeof(double), variables);

                    // add values
                    // right now adding 10 random values
                    for (int i = 0; i < 10; i++)
                    {
                        DataRow dr = dt.NewRow(); //Creating a New Row
                        for (int k = 0; k < variables.Length; k++)
                        {
                            dr[variables[k]] = (1 * i) + k;
                        }
                        dt.Rows.Add(dr); // Add the Row to the DataTable
                    }

                    dt.CreateSolutionColumn(equation, typeof(double));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }
        public double[] Post([FromBody] Data value)
        {
            var table     = JsonConvert.DeserializeObject <List <Dictionary <string, string> > >(value.JsonData);
            var variables = GetVariablesName(value.Equation);

            DataTable dt = new DataTable();

            var tableColHeader = table[0].Select(tk => tk.Key).ToArray();

            if (tableColHeader.Any(bs => !variables.Contains(bs)))
            {
                throw new Exception("header do not match with variables");
            }
            ;

            // create columns with type
            dt.AddColumnsWithType(typeof(double), tableColHeader);

            // add values
            // right now adding 10 random values
            for (int i = 0; i < table.Count; i++)
            {
                DataRow dr  = dt.NewRow(); //Creating a New Row
                var     row = table[i];

                for (int k = 0; k < variables.Length; k++)
                {
                    dr[variables[k]] = row[variables[k]];
                }
                dt.Rows.Add(dr); // Add the Row to the DataTable
            }

            dt.CreateSolutionColumn(value.Equation, typeof(double), value.resultColName);

            var result = Enumerable.Range(0, dt.Rows.Count).Select(p => (double)dt.Rows[p][value.resultColName]).ToArray();

            return(result);
        }