public void StackEnumeratesCorrectly()
        {
            //Arrange
            var stack    = new StaticStack <string>(5);
            var expected = new List <string>()
            {
                "five", "four", "three", "two", "one"
            };

            //Act
            stack.Push("one");
            stack.Push("two");
            stack.Push("three");
            stack.Push("four");
            stack.Push("five");
            var list = new List <string>();

            foreach (var item in stack)
            {
                list.Add(item);
            }

            //Assert
            CollectionAssert.AreEqual(expected, list);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Numbers for Static Stack:");
            Console.WriteLine("(Until u enter empty line or invalid format for a number)");

            StaticStack<int> sampleStack = new StaticStack<int>();

            Program.ConsoleReader(sampleStack);
            Console.WriteLine("Result:");
            while (sampleStack.Count > 0)
            {
                Console.WriteLine(sampleStack.Pop());
            }

            Console.WriteLine();
            Console.WriteLine("Numbers for Dynamic Stack:");
            Console.WriteLine("(Until u enter empty line or invalid format for a number)");

            DynamicStack<int> sampleDynamicStack = new DynamicStack<int>();

            Program.ConsoleReader(sampleDynamicStack);
            Console.WriteLine("Result:");

            while (sampleDynamicStack.Count > 0)
            {
                Console.WriteLine(sampleDynamicStack.Pop());
            }
          
        }
        public void StackDoesntEnumerateEmpty()
        {
            //Arrange
            var stack    = new StaticStack <string>();
            var expected = false;

            //Act

            foreach (var item in stack)
            {
                expected = true;
            }

            stack.Push("one");
            stack.Push("two");
            stack.Push("three");
            stack.Push("four");
            stack.Clear();

            foreach (var item in stack)
            {
                expected = true;
            }

            //Assert
            Assert.IsFalse(expected);
        }
Esempio n. 4
0
 public override Number Calculate(StaticStack<MathOperation> NumStaticStack)
 {
     float a, b, c, d;
     if (NumStaticStack.Count < ArgumentCount)
     {
         throw new Exceptions.NotEnoughArgumentsException("Not enough arguments for \"" + ToString() +"\"");
     }
     switch (ArgumentCount)
     {
        case 0:
             return Calculate();
         case 1:
             return Calculate(NumStaticStack.Pop().Calculate(NumStaticStack));
         case 2:
             b = NumStaticStack.Pop().Calculate(NumStaticStack);
             a = NumStaticStack.Pop().Calculate(NumStaticStack);
             return Calculate(a,b);
         case 3:
             c = NumStaticStack.Pop().Calculate(NumStaticStack);
             b = NumStaticStack.Pop().Calculate(NumStaticStack);
             a = NumStaticStack.Pop().Calculate(NumStaticStack);
             return Calculate(a,b,c);
         case 4:
             d = NumStaticStack.Pop().Calculate(NumStaticStack);
             c = NumStaticStack.Pop().Calculate(NumStaticStack);
             b = NumStaticStack.Pop().Calculate(NumStaticStack);
             a = NumStaticStack.Pop().Calculate(NumStaticStack);
             return Calculate(a,b,c,d);
         default:
             throw new NotSupportedException("ArgumentCount not supported!");
     }
 }
Esempio n. 5
0
        public void TestClear_EmptyStack()
        {
            StaticStack <int> stack = new StaticStack <int>();

            stack.Clear();

            Assert.AreEqual(0, stack.Count);
            Assert.AreEqual(string.Empty, stack.ToString());
        }
        public void PopEmptyStackThrowsException()
        {
            //Arrange
            var stack = new StaticStack <string>(5);

            //Act
            stack.Pop();
            //Assert
        }
        public void CreateEmptyStackWorksCorrectly()
        {
            //Arrange
            var stack    = new StaticStack <string>();
            int expected = 0;
            //Act
            int count = stack.Count;

            //Assert
            Assert.AreEqual(expected, count);
        }
Esempio n. 8
0
        public void TestToString_NonEmptyStack()
        {
            StaticStack <int> stack = new StaticStack <int>();
            int stackCount          = 10;

            for (int i = 0; i < stackCount; i++)
            {
                stack.Push(i);
            }

            Assert.AreEqual("9876543210", stack.ToString());
        }
        public void PushTest_PushSeveralItems()
        {
            StaticStack <int> stack = new StaticStack <int>();

            for (int i = 1; i <= 5; i++)
            {
                stack.Push(i);
            }

            Assert.AreEqual(5, stack.Count);
            Assert.AreEqual(5, stack.Peek());
        }
Esempio n. 10
0
 public MathExpression(StaticStack<MathOperation> _ExpressionStack,Dictionary<string,float> defaultVariables, string originalExpression)
 {
     m_ExpressionStack = _ExpressionStack;
     OriginalExpression = originalExpression;
     m_Variables = defaultVariables;
     if (m_ExpressionStack.Count == 0)
         m_ExpressionStack.Push(new Number(0));
     if (m_ExpressionStack.Peek() is Operator)
     {
         IsImplicit = ((Operator)(m_ExpressionStack.Peek())).IsLogical;
     }
     UpdateExpressionTree();
 }
Esempio n. 11
0
        public void TestPeek_NonEmptyStack()
        {
            StaticStack <int> stack = new StaticStack <int>();

            int stackCount = 5;

            for (int i = 0; i < stackCount; i++)
            {
                stack.Push(i);
            }

            Assert.AreEqual(4, stack.Peek());
        }
Esempio n. 12
0
        public void TestPush_TenItems()
        {
            StaticStack <int> stack = new StaticStack <int>();

            int stackCount = 10;

            for (int i = 0; i < stackCount; i++)
            {
                stack.Push(i);
            }

            Assert.AreEqual(stackCount, stack.Count);
            Assert.AreEqual("9876543210", stack.ToString());
        }
Esempio n. 13
0
        public void TestClear_NonEmptyStack()
        {
            StaticStack <int> stack = new StaticStack <int>();
            int stackCount          = 5;

            for (int i = 0; i < stackCount; i++)
            {
                stack.Push(i);
            }

            stack.Clear();

            Assert.AreEqual(0, stack.Count);
            Assert.AreEqual(string.Empty, stack.ToString());
        }
        public void StackPopWorksCorrectly()
        {
            //Arrange
            var stack    = new StaticStack <string>(5);
            var expected = "three";

            //Act
            stack.Push("one");
            stack.Push("two");
            stack.Push("three");
            var result = stack.Pop();

            //Assert
            Assert.AreEqual(expected, result);
        }
Esempio n. 15
0
 public StaticStack<MathOperation> BuildExpressionStack(StaticStack<MathOperations.MathOperation> Stack)
 {
     if (FunctionParameters != null)
         foreach (var v in FunctionParameters)
             v.BuildExpressionStack(Stack);
     else
     {
         if(Left != null)
             Left.BuildExpressionStack(Stack);
         if (Right != null)
         Right.BuildExpressionStack(Stack);
     }
     Stack.Push(Value);
     return Stack;
 }
        public void StackPushWorksCorrectly()
        {
            //Arrange
            var stack    = new StaticStack <string>(5);
            var expected = new string[] { "three", "two", "one" };

            //Act
            stack.Push("one");
            stack.Push("two");
            stack.Push("three");
            var result = stack.ToArray();

            //Assert
            CollectionAssert.AreEqual(expected, result);
        }
        public void ToArrayTest_ValidStack()
        {
            StaticStack <int> stack = new StaticStack <int>();

            for (int i = 1; i <= 5; i++)
            {
                stack.Push(i);
            }

            int[] arr = stack.ToArray();

            for (int i = 0; i < 5; i++)
            {
                Assert.AreEqual(stack.Pop(), arr[arr.Length - 1 - i]);
            }
        }
        public void PopTest_PoPSeveralItems()
        {
            StaticStack <int> stack = new StaticStack <int>();

            int[] arr = new int[5];
            for (int i = 0; i < 5; i++)
            {
                stack.Push(i + 1);
                arr[arr.Length - 1 - i] = i + 1;
            }

            for (int i = 0; i < 5; i++)
            {
                Assert.AreEqual(stack.Pop(), arr[i]);
            }
        }
        public void StackCopyToWithBiggerIndexThanStackLengthThrowsException()
        {
            //Arrange
            var stack      = new StaticStack <string>(5);
            var emptyArray = new string[5];

            //Act
            stack.Push("one");
            stack.Push("two");
            stack.Push("three");
            stack.Push("four");
            stack.Push("five");
            stack.CopyTo(emptyArray, 6);

            //Assert
        }
        public void StackCopyToWithNullArrayThrowsException()
        {
            //Arrange
            var stack      = new StaticStack <string>(5);
            var emptyArray = default(string[]);

            //Act
            stack.Push("one");
            stack.Push("two");
            stack.Push("three");
            stack.Push("four");
            stack.Push("five");
            stack.CopyTo(emptyArray, 0);

            //Assert
        }
        public void StackCopyToWorksCorrectly()
        {
            //Arrange
            var stack         = new StaticStack <string>(5);
            var emptyArray    = new string[3];
            var expectedArray = new string[] { "three", "two", "one" };

            //Act
            stack.Push("one");
            stack.Push("two");
            stack.Push("three");
            stack.CopyTo(emptyArray, 0);

            //Assert
            CollectionAssert.AreEqual(expectedArray, emptyArray);
        }
Esempio n. 22
0
        public void TestClone_EmptyStack()
        {
            StaticStack <int> stack = new StaticStack <int>();
            StaticStack <int> clone = (StaticStack <int>)stack.Clone();

            int count = 5;

            for (int i = 0; i < count; i++)
            {
                stack.Push(i);
            }

            Assert.AreEqual(5, stack.Count);
            Assert.AreEqual(0, clone.Count);
            Assert.AreEqual(string.Empty, clone.ToString());
        }
        public void StackCountWorksCorrectly()
        {
            //Arrange
            var stack    = new StaticStack <string>(5);
            int expected = 5;

            //Act
            stack.Push("one");
            stack.Push("two");
            stack.Push("three");
            stack.Push("four");
            stack.Push("five");
            int count = stack.Count;

            //Assert
            Assert.AreEqual(expected, count);
        }
Esempio n. 24
0
        public void TestPop_OneItem()
        {
            StaticStack <int> stack = new StaticStack <int>();

            int stackCount = 5;

            for (int i = 0; i < stackCount; i++)
            {
                stack.Push(i);
            }

            int popped = stack.Pop();

            Assert.AreEqual(4, popped);
            Assert.AreEqual(4, stack.Count);
            Assert.AreEqual("3210", stack.ToString());
        }
        public void StackContainsWorksCorrectly()
        {
            //Arrange
            var stack = new StaticStack <string>(5);

            //Act
            stack.Push("one");
            stack.Push("two");
            stack.Push("three");
            bool found = stack.Contains("three");

            stack.Pop();
            bool notFound = stack.Contains("three");

            //Assert
            Assert.IsTrue(found);
            Assert.IsFalse(notFound);
        }
Esempio n. 26
0
        public void TestPop_MultipleItems()
        {
            StaticStack <int> stack = new StaticStack <int>();

            int stackCount = 10;

            for (int i = 0; i < stackCount; i++)
            {
                stack.Push(i);
            }

            for (int i = 0; i < 5; i++)
            {
                int popped = stack.Pop();
                Assert.AreEqual(9 - i, popped);
            }

            Assert.AreEqual(5, stack.Count);
            Assert.AreEqual("43210", stack.ToString());
        }
Esempio n. 27
0
        public void TestForeachLoop_Enumerator()
        {
            StaticStack <int> stack = new StaticStack <int>();

            int stackCount = 5;

            for (int i = 0; i < stackCount; i++)
            {
                stack.Push(i);
            }

            string        expected = "43210";
            StringBuilder actual   = new StringBuilder();

            foreach (var item in stack)
            {
                actual.Append(item.ToString());
            }

            Assert.AreEqual(expected, actual.ToString());
        }
Esempio n. 28
0
        public void TestClone_NonEmptyStack()
        {
            StaticStack <int> stack = new StaticStack <int>();

            int stackCount = 5;

            for (int i = 0; i < stackCount; i++)
            {
                stack.Push(i);
            }

            StaticStack <int> clone = (StaticStack <int>)stack.Clone();

            for (int i = 0; i < stackCount; i++)
            {
                stack.Pop();
            }

            Assert.AreEqual(0, stack.Count);
            Assert.AreEqual(5, clone.Count);
            Assert.AreEqual("43210", clone.ToString());
        }
Esempio n. 29
0
 public virtual void CopyDerivedFunctionToStack(ExpressionTree cur,ExpressionTree inner, StaticStack<MathOperation> DeriveStack,MathState State)
 {
     throw new MathEvaluator.Exceptions.UndefinedResultException(ToString() + " is not derivable");
 }
Esempio n. 30
0
                public override void DeriveToStack(ExpressionTree cur, MathState State, StaticStack<MathOperation> DeriveStack)
                {
                    // (f(x)^g(x))'
                    // f(x)^(g(x)-1) (g(x) f'(x)+f(x) ln(f(x)) g'(x))
                    // f(x) g(x) 1 - ^ g(x) f'(x) * f(x) f(x) ln * + * g'(x) *
                    //  f g 1 - ^ g v * f f ln * b * + *
                    ExpressionTree f = cur.Left;
                    ExpressionTree g = cur.Right;
                    f.CopyToStack(DeriveStack);
                    g.CopyToStack(DeriveStack);
                    DeriveStack.Push((Number)1, State.GetOperator("-"), State.GetOperator("^"));
                    g.CopyToStack(DeriveStack);
                    f.DeriveToStack(DeriveStack,State);
                    DeriveStack.Push(State.GetOperator("*"));
                    f.CopyToStack(DeriveStack);
                    f.CopyToStack(DeriveStack);
                    DeriveStack.Push(State.GetFunction("ln"), State.GetOperator("*"));
                    g.DeriveToStack(DeriveStack,State);
                    DeriveStack.Push(State.GetOperator("*"), State.GetOperator("+"), State.GetOperator("*"));

                    /*
                    if (cur.Left.ContainsVariable("x"))
                    {
                        //(x^n)' = n * x^(n-1)
                        ExpressionTree left = cur.Left;
                        ExpressionTree right = cur.Right;

                        right.CopyToStack(DeriveStack);
                        left.CopyToStack(DeriveStack);
                        right.CopyToStack(DeriveStack);
                        DeriveStack.Push((Number)1,State.GetOperator("-"),State.GetOperator("^"),State.GetOperator("*"));
                    }
                    else if (cur.Right.ContainsVariable("x"))
                    {
                        // (a^x)' = a^x * ln a
                        // a x ^ a ln *
                        ExpressionTree left = cur.Left;
                        ExpressionTree right = cur.Right;

                        left.CopyToStack(DeriveStack);
                        right.CopyToStack(DeriveStack);
                        DeriveStack.Push(State.GetOperator("^"));

                        left.CopyToStack(DeriveStack);
                        DeriveStack.Push(State.GetFunction("ln"), State.GetOperator("*"));

                        //chain rule
                        right.DeriveToStack(DeriveStack, State);
                        DeriveStack.Push(State.GetOperator("*"));
                    }
                    else if (cur.Left.ToString() == "e" && cur.Right.ToString() == "x")
                    {
                        cur.Left.CopyToStack(DeriveStack);
                        cur.Right.CopyToStack(DeriveStack);
                        DeriveStack.Push(this);
                    }
                    else
                    {
                        DeriveStack.Push((Number)0);
                    }*/
                }
Esempio n. 31
0
                public override void DeriveToStack(ExpressionTree cur, MathState State, StaticStack<MathOperation> DeriveStack)
                {
                    //f(x) = u(v(x))
                    //f'(x) = u'(v(x))*v'(x)
                    // v u' v' *
                    if(ArgumentCount != 1)
                        throw new MathEvaluator.Exceptions.UndefinedResultException(ToString() + " is not derivable");
                    ExpressionTree inner = cur.FunctionParameters[0];

                    CopyDerivedFunctionToStack(cur,inner, DeriveStack,State);
                    inner.DeriveToStack(DeriveStack,State);
                    DeriveStack.Push(new Multiplication());
                }
Esempio n. 32
0
 public override Number Calculate(StaticStack<MathOperation> NumStaticStack)
 {
     return Value;
 }
Esempio n. 33
0
 public override void DeriveToStack(ExpressionTree cur, MathState State, StaticStack<MathOperation> DeriveStack)
 {
     DeriveStack.Push((Number)(_name.ToLower() == "x" ? 1 : 0));
 }
Esempio n. 34
0
 public override void CopyDerivedFunctionToStack(ExpressionTree cur,ExpressionTree inner, StaticStack<MathOperation> DeriveStack,MathState State)
 {
     inner.CopyToStack(DeriveStack);
     DeriveStack.Push(State.GetFunction("cos"));
 }
Esempio n. 35
0
 public override void DeriveToStack(ExpressionTree cur, MathState State, StaticStack<MathOperation> DeriveStack)
 {
     DeriveStack.Push((Number)0);
 }
Esempio n. 36
0
 public ExpressionTree(StaticStack<MathOperation> NumStaticStack)
 {
     Value = NumStaticStack.Pop();
     BuildBinaryTree(NumStaticStack);
 }
Esempio n. 37
0
        public void TestPeek_EmptyStack()
        {
            StaticStack <int> stack = new StaticStack <int>();

            Assert.AreEqual(4, stack.Peek());
        }
        public void ToArrayTest_InvalidStack()
        {
            StaticStack <int> stack = new StaticStack <int>();

            int[] arr = stack.ToArray();
        }
Esempio n. 39
0
 public override void CopyDerivedFunctionToStack(ExpressionTree cur, ExpressionTree inner, StaticStack<MathOperation> DeriveStack, MathState State)
 {
     //ln(x)' = 1/x
     // 1 x /
     DeriveStack.Push((Number)1);
     inner.CopyToStack(DeriveStack);
     DeriveStack.Push(State.GetOperator("/"));
 }
Esempio n. 40
0
                public override void CopyDerivedFunctionToStack(ExpressionTree cur, ExpressionTree inner, StaticStack<MathOperation> DeriveStack, MathState State)
                {
                    //sqrt(x)' = 1/(x * ln 10)
                    // 1 x 10 ln * /

                    DeriveStack.Push((Number)1);
                    inner.CopyToStack(DeriveStack);
                    DeriveStack.Push((Number)10,State.GetFunction("ln"),State.GetOperator("*"), State.GetOperator("/"));
                }
Esempio n. 41
0
 public abstract void CopyToStack(ExpressionTree cur, StaticStack<MathOperation> Stack);
Esempio n. 42
0
                public override void BuildBinaryTree(StaticStack<MathOperation> NumStaticStack,  ExpressionTree b)
                {
                    b.Value = this;
                    b.FunctionParameters = new List<ExpressionTree>();

                    for(int i = 0;i< ArgumentCount;i++)
                    {
                        b.FunctionParameters.Add(new ExpressionTree(NumStaticStack));
                        //b.FunctionParameters[0].Value.BuildBinaryTree(NumStaticStack,b.FunctionParameters[0]);
                    }
                }
Esempio n. 43
0
        public MathExpression CreateExpression(StaticStack<MathOperation> expression)
        {
            expression.ResetTop();
            StaticStack<MathOperation> Expression = expression;
            //the new MathExpression gets the original
            Dictionary<string, float> var = m_Variables;
            //and we keep a copy so they dont intefere with eachother
            m_Variables = new Dictionary<string, float>(var);

            return new MathExpression(Expression, var, "created");
        }
        public void PopTest_PopFromEmptyStack()
        {
            StaticStack <int> stack = new StaticStack <int>();

            stack.Pop();
        }
Esempio n. 45
0
        public StaticStack<MathOperation> Convert(string expression)
        {
            StaticStack<MathOperation> Output = new StaticStack<MathOperation>();
            Stack<MathOperation> OperatorStack = new Stack<MathOperation>();
            NumberState NumberState = NumberState.None;

            float CommaMultiplier = 0;
            float minus = 1;
            bool useMinus = true;
            for (int i = 0; i < expression.Length; i++)
            {
                char token = expression[i];
                if (char.IsWhiteSpace(token))
                {
                    //fixes 5 2 = 52
                    //new bug: 5 2 + = 7 (infix)
                    NumberState = NumberState.None;
                    continue;
                }
                if(token == '-')
                {
                    if (useMinus)
                    {
                        minus = -1;
                        continue;
                    }
                }
                if (char.IsDigit(token))
                {
                    float numeric = (float)char.GetNumericValue(token);
                    switch (NumberState)
                    {
                        case NumberState.None:
                            Output.Push((Number)(numeric * minus));
                            NumberState = NumberState.Number;
                            break;
                        case NumberState.Number:
                            float num = (Number)Output.PopReal();
                            Output.Push((Number)(num * 10.0 + numeric * NonNullSign(num)));
                            break;
                        case NumberState.Comma:
                            float num2 = (Number)Output.PopReal();
                            Output.Push((Number)(num2 + numeric * CommaMultiplier * NonNullSign(num2)));
                            CommaMultiplier *= 0.1f;
                            break;
                        default:
                            throw new NotImplementedException();
                    }
                    minus = 1;
                    useMinus = false;
                }
                else if (token == '.')
                {
                    minus = 1;
                    NumberState = NumberState.Comma;
                    CommaMultiplier = 0.1f;
                }
                else if (token == ',')
                {
                    NumberState = NumberState.None;

                    if (OperatorStack.Count == 0)
                        throw new MismatchedParenthesisException("\"" + expression.Substring(i) + "\": Can't find opening parenthesis");
                    while (OperatorStack.Count > 0)
                    {
                        if (OperatorStack.Peek() is Parenthesis)
                            break;
                        Output.Push(OperatorStack.Pop());
                    }
                    if (OperatorStack.Count == 0 || !(OperatorStack.Peek() is Parenthesis))
                        throw new MismatchedParenthesisException("\"" + expression.Substring(i) + "\": Can't find opening parenthesis");

                }
                else if (token == ')')
                {
                    if (OperatorStack.Count == 0)
                        throw new MismatchedParenthesisException("\"" + expression.Substring(i) + "\": Can't find opening parenthesis");
                    while (OperatorStack.Count > 0)
                    {
                        MathOperation TopOperator = OperatorStack.Pop();
                        if (TopOperator is Parenthesis)
                            break;
                        else
                            Output.Push(TopOperator);
                        if (OperatorStack.Count == 0)
                            throw new MismatchedParenthesisException("\"" + expression.Substring(i) + "\": Can't find opening parenthesis");
                    }
                    if (OperatorStack.Count > 0 && OperatorStack.Peek() is Function)
                        Output.Push(OperatorStack.Pop());
                    useMinus = false;
                }
                else if (char.IsSymbol(token) || char.IsPunctuation(token))
                {
                    string fullname = "";
                    for (; i < expression.Length; i++)
                    {
                        char letter = expression[i];
                        if ((char.IsSymbol(letter) || char.IsPunctuation(letter)))
                            //we cant use letters or 5 + sin(x) is going to break
                            fullname += char.ToLower(letter);
                        else
                        {
                            --i;
                            break;
                        }
                        if (m_Operators.ContainsKey(fullname))
                            break;
                    }
                    if (m_Operators.ContainsKey(fullname))
                    {
                        MathOperation TokenOperator = m_Operators[fullname];
                        if (NumberState != MathState.NumberState.None)
                        {
                            NumberState = NumberState.None;
                            if (TokenOperator is Parenthesis)
                            {
                                // makes 5(x) possible
                                OperatorStack.Push(new MathOperations.Operators.Multiplication());
                            }
                        }
                        while (OperatorStack.Count > 0)
                        {
                            MathOperation TopOperator = OperatorStack.Peek();
                            if (TokenOperator.Association == Association.Left && TokenOperator.Precedence <= TopOperator.Precedence)
                                Output.Push(OperatorStack.Pop());
                            else if (TokenOperator.Association == Association.Right && TokenOperator.Precedence < TopOperator.Precedence)
                                Output.Push(OperatorStack.Pop());
                            else
                                break;
                        }
                        OperatorStack.Push(TokenOperator);
                    }
                    useMinus = true;
                }
                else if (char.IsLetter(token))
                {
                    minus = 1;
                    string fullname = "";
                    for (; i < expression.Length; i++)
                    {
                        char letter = expression[i];
                        if (char.IsLetter(letter))
                            fullname += char.ToLower(letter);
                        else
                        {
                            --i;
                            break;
                        }
                    }
                    if (NumberState != MathState.NumberState.None)
                    {
                        NumberState = NumberState.None;
                        OperatorStack.Push(new MathOperations.Operators.Multiplication());
                    }
                    if (m_Functions.ContainsKey(fullname))
                        OperatorStack.Push(m_Functions[fullname]);
                    else if (m_Variables.ContainsKey(fullname))
                    {
                        Output.Push(new Variable(fullname, m_Variables));
                    }
                    else if (m_Constants.ContainsKey(fullname))
                    {
                        Output.Push(new Constant(fullname, m_Constants[fullname]));
                    }
                    else if (DefineUnknownVariables)
                        Output.Push(new Variable(fullname,m_Variables));
                    else
                        throw new UndefinedFunctionException("Function \"" + fullname + "\" not defined");
                    useMinus = false;
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
            while (OperatorStack.Count > 0)
            {
                if (OperatorStack.Peek() is Parenthesis)
                    throw new MismatchedParenthesisException("Can't find closing parenthesis");
                Output.Push(OperatorStack.Pop());
            }
            return Output;
        }
Esempio n. 46
0
 public StaticStack<MathOperation> GetExpressionStack(StaticStack<MathOperations.MathOperation> Stack)
 {
     if (FunctionParameters != null)
         foreach (var v in FunctionParameters)
             v.GetExpressionStack(Stack);
     else
     {
         Stack.Push(Left.Value);
         Stack.Push(Right.Value);
     }
     Stack.Push(Value);
     return Stack;
 }
Esempio n. 47
0
        public MathExpression Derive(MathState state)
        {
            StaticStack<MathOperation> stack = m_ExpressionStack;
            StaticStack<MathOperation> derived = new StaticStack<MathOperation>();
            ExpressionTree tree = new ExpressionTree(stack);
            tree.DeriveToStack(derived, state);

            Dictionary<string, float> var = new Dictionary<string, float>(m_Variables);

            for (int i = 0; i < derived.Count; i++)
            {
                if (derived[i] is Variable)
                {
                    Variable v = (Variable)derived[i];
                    derived[i] = new Variable(v.ToString(), var);
                }

            }
            return new MathExpression(derived,var, "(" + OriginalExpression + ")'");
        }
Esempio n. 48
0
        public void TestToString_EmptyStack()
        {
            StaticStack <int> stack = new StaticStack <int>();

            Assert.AreEqual(string.Empty, stack.ToString());
        }
Esempio n. 49
0
 public override void BuildBinaryTree(StaticStack<MathOperation> NumStaticStack,ExpressionTree b)
 {
     b.Value = this;
 }
Esempio n. 50
0
 public void DeriveToStack(StaticStack<MathOperations.MathOperation> Stack,MathState State)
 {
     Value.DeriveToStack(this,State,Stack);
 }
Esempio n. 51
0
 public override void CopyToStack(ExpressionTree cur, StaticStack<MathOperation> Stack)
 {
     Stack.Push(this);
 }
Esempio n. 52
0
 public void CopyToStack(StaticStack<MathOperations.MathOperation> Stack)
 {
     Value.CopyToStack(this, Stack);
 }
Esempio n. 53
0
 public override void DeriveToStack(ExpressionTree cur, MathState State, StaticStack<MathOperation> DeriveStack)
 {
     throw new UndefinedResultException(ToString() + " is not derivable");
 }
Esempio n. 54
0
 public void BuildBinaryTree(StaticStack<MathOperation> NumStaticStack)
 {
     Value.BuildBinaryTree(NumStaticStack,this);
 }
Esempio n. 55
0
 public override void CopyDerivedFunctionToStack(ExpressionTree cur, ExpressionTree inner, StaticStack<MathOperation> DeriveStack,MathState State)
 {
     inner.CopyToStack(DeriveStack);
     DeriveStack.Push(State.GetFunction("sin"), (Number)(-1), State.GetOperator("*"));
 }
Esempio n. 56
0
 public override void CopyToStack(ExpressionTree cur, StaticStack<MathOperation> Stack)
 {
     foreach (var b in cur.FunctionParameters)
         b.CopyToStack(Stack);
     Stack.Push(this);
 }
Esempio n. 57
0
 public override void CopyDerivedFunctionToStack(ExpressionTree cur, ExpressionTree inner, StaticStack<MathOperation> DeriveStack, MathState State)
 {
     //sqrt(x)' = 1/(2*sqrt(x))
     // 1 2 x sqrt * /
     DeriveStack.Push((Number)1,(Number)2);
     inner.CopyToStack(DeriveStack);
     DeriveStack.Push(State.GetFunction("sqrt"), State.GetOperator("*"), State.GetOperator("/"));
 }
Esempio n. 58
0
                public override void DeriveToStack(ExpressionTree cur, MathState State, StaticStack<MathOperation> DeriveStack)
                {
                    //(u * v)' = u' * v + u * v'
                    //PF:      = u' v * u v' * +
                    ExpressionTree left = cur.Left;
                    ExpressionTree right = cur.Right;

                    left.DeriveToStack(DeriveStack,State);
                    right.CopyToStack(DeriveStack);
                    DeriveStack.Push(State.GetOperator("*"));
                    left.CopyToStack(DeriveStack);
                    right.DeriveToStack(DeriveStack,State);
                    DeriveStack.Push(State.GetOperator("*"), State.GetOperator("+"));
                }