Ejemplo n.º 1
0
        private double ProgrammaticallyParseToNumber(string condition)
        {
            condition = condition.Replace("&&", "&");
            condition = condition.Replace("||", "|");
            condition = Regex.Replace(condition, @"\btrue\b", "1", RegexOptions.IgnoreCase);
            condition = Regex.Replace(condition, @"\bfalse\b", "0", RegexOptions.IgnoreCase);
            condition = condition.Replace("||", "|");
            condition = Regex.Replace(condition, @"\band\b", "&", RegexOptions.IgnoreCase);
            condition = Regex.Replace(condition, @"\bor\b", "|", RegexOptions.IgnoreCase);
            condition = condition.Trim();

            if (condition.Contains("("))
            {
                int open  = condition.IndexOf("(");
                int close = open + 1;

                char[]       myArray = condition.ToCharArray();
                Stack <char> myStack = new Stack <char>();
                for (int i = open; i < myArray.Length; i++)
                {
                    if (myArray[i] == '(')
                    {
                        myStack.Push(myArray[i]);
                    }
                    if (myArray[i] == ')')
                    {
                        myStack.Pop();
                    }
                    if (myStack.Count == 0)
                    {
                        close = i;
                        break;
                    }
                    if (i == myArray.Length - 1)
                    {
                        throw new Exception("Brackets don't match");
                    }
                }

                string innerCondition = condition.Substring(open + 1, (close) - (open + 1));
                // check to see if the brackets aren't part of a operator call, like abs()
                if (open == 0 || (!char.IsLetterOrDigit(condition[open - 1])))
                {
                    condition = condition.Replace("(" + innerCondition + ")", ProgrammaticallyParseToNumber(innerCondition) + "");
                }
            }
            string[] conditions = condition.Split('&', '|');
            foreach (string c in conditions)
            {
                condition = condition.Replace(c, mathParser.ProgrammaticallyParse(c).ToString(CultureInfo));
            }

            var tokens = Lexer(condition);

            return(BasicBooleanExpression(tokens));
        }
Ejemplo n.º 2
0
        public void ProgrmaticallyAddVariables()
        {
            /* 
             * when parsing an expression that requires 
             * for instance a variable name declaration 
             * or change, use ProgramaticallyParse().
             */
            MathParser parser = new MathParser();

            // first way, using let varname = value
            decimal resultA = parser.ProgrammaticallyParse("let a = 2pi");
            Assert.IsTrue(parser.Parse("a") == (decimal)Math.PI * 2);

            // second way, using varname :=  value
            decimal resultC = parser.ProgrammaticallyParse("b := 20");
            Assert.IsTrue(parser.Parse("b") == 20);

            // third way, using let varname be value
            decimal resultD = parser.ProgrammaticallyParse("let c be 25");
            Assert.IsTrue(resultD == 25);
        }
        private decimal IntegrateUsingSimpsonsRule(
            string expression,
            decimal lowerLimit,
            decimal upperLimit,
            decimal numberOfIntervals = 100000)
        {
            var parser = new MathParser();
            var sizeOfInterval = ((upperLimit - lowerLimit)/numberOfIntervals);

            parser.LocalVariables["x"] = lowerLimit;

            var sum = parser.ProgrammaticallyParse(expression);

            for (var i = 1; i < numberOfIntervals; i += 2)
            {
                parser.LocalVariables["x"] = lowerLimit + sizeOfInterval*i;
                sum += 4*parser.ProgrammaticallyParse(expression);
            }

            for (var i = 2; i < numberOfIntervals - 1; i += 2)
            {
                parser.LocalVariables["x"] = lowerLimit + sizeOfInterval*i;
                sum += 2*parser.ProgrammaticallyParse(expression);
            }

            parser.LocalVariables["x"] = upperLimit;
            sum += parser.ProgrammaticallyParse(expression);

            var result = sum*sizeOfInterval/3;

            return result;
        }
        private decimal IntegrateUsingTrapezoidalRule(
            string expression,
            decimal lowerLimit,
            decimal upperLimit,
            decimal numberOfIntervals = 100000)
        {
            var parser = new MathParser();
            var sizeOfInterval = ((upperLimit - lowerLimit)/numberOfIntervals);

            parser.LocalVariables["x"] = lowerLimit;
            var sum = parser.ProgrammaticallyParse(expression);

            parser.LocalVariables["x"] = upperLimit;
            sum += parser.ProgrammaticallyParse(expression);

            for (var i = 1; i < numberOfIntervals; i++)
            {
                parser.LocalVariables["x"] = lowerLimit + i*sizeOfInterval;
                sum += parser.ProgrammaticallyParse(expression)*2;
            }

            var result = sum*sizeOfInterval/2;

            return result;
        }
        private decimal IntegrateUsingRectangleMethod(
            string expression,
            decimal lowerLimit,
            decimal upperLimit,
            decimal numberOfIntervals = 100000)
        {
            decimal sum = 0;
            var parser = new MathParser();
            var sizeOfInterval = ((upperLimit - lowerLimit)/numberOfIntervals);

            for (var i = 0; i < numberOfIntervals; i++)
            {
                parser.LocalVariables["x"] = lowerLimit + sizeOfInterval*i;
                sum += parser.ProgrammaticallyParse(expression)*sizeOfInterval;
            }

            var result = sum;

            return result;
        }
        private static double EvaluateExpression(string expression, double x)
        {
            var parser = new MathParser {LocalVariables = {["x"] = (decimal) x}};

            var result = parser.ProgrammaticallyParse(expression);

            return (double) result;
        }
Ejemplo n.º 7
0
        public bool domathwithlabels(string data, out int outv)
        {
            MathParser parser = new MathParser();
            outv = 0;

            foreach (KeyValuePair<string, int> kvp in labels)
            {

                int v;
                parser.LocalVariables.Add(kvp.Key, kvp.Value);
            }

            try
            {
                //we only have ints in z80 so cast it to an int
                outv = (int)parser.ProgrammaticallyParse(data);
            }
            catch (Exception e)
            {
                Console.WriteLine("Math error");
                return false;
            }

            return true;
        }
Ejemplo n.º 8
0
        public bool domath(string data, out int outv)
        {
            MathParser parser = new MathParser();
            outv = 0;

            foreach(KeyValuePair<string,string>kvp in equs)
            {

                int v;
                if (!isnumber(kvp.Value, out v))
                    return false; // we failed to turn this into a number
                    parser.LocalVariables.Add(kvp.Key,(decimal)v);
            }

            try
            {
                //we only have ints in z80 so cast it to an int
                outv = (int)parser.ProgrammaticallyParse(data);
            }
            catch (Exception e)
            {
                Console.WriteLine("Math error");
                return false;
            }

            return true;
        }