示例#1
0
        private static void CalcPi()
        {
            ConstPI = new NumberType(Math.PI, UnitType.Empty());
            return;

            NumberType m = new NumberType("100000", UnitType.Empty());
            NumberType inc = new NumberType("2", UnitType.Empty());
            NumberType a = new NumberType("2", UnitType.Empty());
            NumberType b = new NumberType("3", UnitType.Empty());
            NumberType c = new NumberType("4", UnitType.Empty());
            NumberType v = new NumberType("0", UnitType.Empty());
            NumberType four = new NumberType("4", UnitType.Empty());
            for (NumberType n = new NumberType("1", UnitType.Empty()); n < m; n += inc)
            {
                v += four / (a * b * c);
                a += inc;
                b += inc;
                c += inc;

                v -= four / (a * b * c);
                a += inc;
                b += inc;
                c += inc;
            }
            ConstPI = v + new NumberType("3", UnitType.Empty());
        }
示例#2
0
 internal static NumberType FromDegrees(NumberType a)
 {
     if (a.Unit.units.Count == 0)
         return a;
     if (a.Unit.units.Count > 1)
         throw new Exception("Invalid units, expected degrees.");
     if (!a.Unit.units.ContainsKey("degrees") || a.Unit.units["degrees"].Power != 1)
         throw new Exception("Invalid units, expected degrees.");
     return new NumberType(a.value * (Math.PI / 180), UnitType.Empty());
 }
示例#3
0
 private static NumberType Factorial(NumberType n)
 {
     var inc = new NumberType("1", UnitType.Empty());
     var mv = n;
     var tv = new NumberType("1", UnitType.Empty());
     for (NumberType i = new NumberType("1", UnitType.Empty()); i <= mv; i += inc)
     {
         tv *= i;
     }
     return tv;
 }
示例#4
0
        private static void CalcE()
        {
            ConstE = new NumberType(Math.E, UnitType.Empty());
            return;

            NumberType v = new NumberType("1", UnitType.Empty());
            NumberType inc = new NumberType("1", UnitType.Empty());
            NumberType m = new NumberType("1000", UnitType.Empty());
            for (NumberType n = new NumberType("1", UnitType.Empty()); n < m; n += inc)
            {
                v += inc / Factorial(n);
            }

            ConstE = v;
        }
示例#5
0
        public Expression(object[] tokens)
        {
            try
            {
                List<object> ex = new List<object>();
                for (int i = 0; i < tokens.Length; i++)
                    ex.Add(tokens[i]);

                int index = 0;

                for (index = 1; index < ex.Count; index++)
                {
                    if (ex[index - 1] is NumberToken && ex[index] is IdentifierToken)
                    {
                        var num = ex[index - 1] as NumberToken;
                        var unit = ex[index] as IdentifierToken;
                        ex.RemoveAt(index);
                        var d = new Dictionary<string, Unit>();
                        d.Add(unit.Text, new Unit(unit.Text, 1));
                        ex[index - 1] = new NumberType(num.Text, new UnitType(d));
                        index--;
                    }
                }
                for (index = 0; index < ex.Count; index++)
                {
                    if (ex[index] is ParenthOpenToken)
                    {
                        int ti = index;
                        index++;
                        int nest = 1;
                        List<object> tt = new List<object>();
                        while (true)
                        {
                            if (ex[index] is ParenthOpenToken)
                                nest++;
                            if (ex[index] is ParenthCloseToken)
                                nest--;
                            if (nest == 0)
                            {
                                index++;
                                break;
                            }

                            tt.Add(ex[index]);

                            index++;
                        }

                        for (int i = index - 1; i > ti; i--)
                        {
                            ex.RemoveAt(i);
                        }
                        ex[ti] = new Expression(tt.ToArray()).RootExpr;
                        index = ti + 1;
                    }
                }
                for (index = 0; index < ex.Count - 2; index++)
                {
                    if (ex[index] is IdentifierToken && ex[index + 1] is ListStartToken)
                    {
                        int ti = index + 2;
                        List<ExprNode> p = new List<ExprNode>();
                        int nest = 0;
                        while (!(ex[ti] is ListEndToken))
                        {
                            List<object> tl = new List<object>();
                            if (ex[ti] is ListSeperatorToken)
                                ti++;

                            while (!(ex[ti] is ListSeperatorToken) && !(ex[ti] is ListEndToken) || nest != 0)
                            {
                                if (ex[ti] is ListStartToken)
                                    nest++;
                                if (ex[ti] is ListEndToken)
                                    nest--;
                                tl.Add(ex[ti++]);
                            }
                            p.Add(new Expression(tl.ToArray()).RootExpr);
                        }
                        for (int i = ti; i > index; i--)
                            ex.RemoveAt(i);
                        ex[index] = new FunctionNode(((IdentifierToken)ex[index]).Text, p.ToArray());
                    }
                }
                //Number Lists
                for (index = 0; index < ex.Count - 1; index++)
                {
                    if (ex[index] is ListStartToken)
                    {
                        int ti = index + 1;
                        List<ExprNode> p = new List<ExprNode>();
                        int nest = 0;
                        while (!(ex[ti] is ListEndToken))
                        {
                            List<object> tl = new List<object>();
                            if (ex[ti] is ListSeperatorToken)
                                ti++;

                            while (!(ex[ti] is ListSeperatorToken) && !(ex[ti] is ListEndToken) || nest != 0)
                            {
                                if (ex[ti] is ListStartToken)
                                    nest++;
                                if (ex[ti] is ListEndToken)
                                    nest--;
                                tl.Add(ex[ti++]);
                            }
                            p.Add(new Expression(tl.ToArray()).RootExpr);
                        }
                        for (int i = ti; i > index; i--)
                            ex.RemoveAt(i);
                        ex[index] = new ListNode(p.ToArray());
                    }
                }
                //=======
                for (index = 0; index < ex.Count; index++)
                {
                    if (ex[index] is PowToken)
                    {
                        var left = ex[index - 1];
                        var right = ex[index + 1];
                        ex.RemoveAt(index + 1);
                        ex.RemoveAt(index);
                        ex[index - 1] = new PowerNode(Expr(left), Expr(right));
                        index--;
                    }
                }
                for (index = 0; index < ex.Count; index++)
                {
                    if (ex[index] is MulToken)
                    {
                        var left = ex[index - 1];
                        var right = ex[index + 1];
                        ex.RemoveAt(index + 1);
                        ex.RemoveAt(index);
                        ex[index - 1] = new MultiplicationNode(Expr(left), Expr(right));
                        index--;
                    }
                    if (ex[index] is DivToken)
                    {
                        var left = ex[index - 1];
                        var right = ex[index + 1];
                        ex.RemoveAt(index + 1);
                        ex.RemoveAt(index);
                        ex[index - 1] = new DivisionNode(Expr(left), Expr(right));
                        index--;
                    }
                    if (ex[index] is ModToken)
                    {
                        var left = ex[index - 1];
                        var right = ex[index + 1];
                        ex.RemoveAt(index + 1);
                        ex.RemoveAt(index);
                        ex[index - 1] = new ModuloNode(Expr(left), Expr(right));
                        index--;
                    }
                }
                for (index = 0; index < ex.Count; index++)
                {
                    if (ex[index] is AddToken)
                    {
                        var left = ex[index - 1];
                        var right = ex[index + 1];
                        ex.RemoveAt(index + 1);
                        ex.RemoveAt(index);
                        ex[index - 1] = new AdditionNode(Expr(left), Expr(right));
                        index--;
                    }
                    if (ex[index] is SubToken)
                    {
                        var left = ex[index - 1];
                        var right = ex[index + 1];
                        ex.RemoveAt(index + 1);
                        ex.RemoveAt(index);
                        ex[index - 1] = new SubtractionNode(Expr(left), Expr(right));
                        index--;
                    }
                }
                for (index = 0; index < ex.Count; index++)
                {
                    if (ex[index] is EqualToken)
                    {
                        var left = ex[index - 1];
                        var right = ex[index + 1];
                        ex.RemoveAt(index + 1);
                        ex.RemoveAt(index);
                        ex[index - 1] = new CompareNode(Expr(left), Expr(right), CompareType.Equal);
                        index--;
                    }
                    if (ex[index] is NotEqualToken)
                    {
                        var left = ex[index - 1];
                        var right = ex[index + 1];
                        ex.RemoveAt(index + 1);
                        ex.RemoveAt(index);
                        ex[index - 1] = new CompareNode(Expr(left), Expr(right), CompareType.NotEqual);
                        index--;
                    }
                    if (ex[index] is LessToken)
                    {
                        var left = ex[index - 1];
                        var right = ex[index + 1];
                        ex.RemoveAt(index + 1);
                        ex.RemoveAt(index);
                        ex[index - 1] = new CompareNode(Expr(left), Expr(right), CompareType.Less);
                        index--;
                    }
                    if (ex[index] is LessEqualToken)
                    {
                        var left = ex[index - 1];
                        var right = ex[index + 1];
                        ex.RemoveAt(index + 1);
                        ex.RemoveAt(index);
                        ex[index - 1] = new CompareNode(Expr(left), Expr(right), CompareType.LessEqual);
                        index--;
                    }
                    if (ex[index] is GreaterToken)
                    {
                        var left = ex[index - 1];
                        var right = ex[index + 1];
                        ex.RemoveAt(index + 1);
                        ex.RemoveAt(index);
                        ex[index - 1] = new CompareNode(Expr(left), Expr(right), CompareType.Greater);
                        index--;
                    }
                    if (ex[index] is GreaterEqualToken)
                    {
                        var left = ex[index - 1];
                        var right = ex[index + 1];
                        ex.RemoveAt(index + 1);
                        ex.RemoveAt(index);
                        ex[index - 1] = new CompareNode(Expr(left), Expr(right), CompareType.GreaterEqual);
                        index--;
                    }
                }

                RootExpr = Expr(ex[0]);
                Console.WriteLine(RootExpr);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                throw new ExpressionException(((Token)tokens[0]).Line, -1, "Invalid expression.  (Forgot closing parenthesis?)");
            }
        }