Beispiel #1
0
        public ExpressionTree(string expression)
        {
            string postfixExpression = ExpressionConverter.ConvertToPostFixNotation(expression);

            List <string>           tokens     = postfixExpression.Split(' ').ToList();
            Stack <IExpressionNode> tokenStack = new Stack <IExpressionNode>();

            for (int i = 0; i < tokens.Count; i++)
            {
                if (tokens[i].IsNumber())
                {
                    var newNode = new Constant(double.Parse(tokens[i]));
                    tokenStack.Push(newNode);
                }
                else if (tokens[i].IsVariable())
                {
                    var newNode = new Variable(tokens[i]);
                    tokenStack.Push(newNode);
                }
                else if (tokens[i].IsOperator())
                {
                    var op1       = tokenStack.Pop();
                    var op2       = tokenStack.Pop();
                    var temporary = (IOperation)Activator.CreateInstance(ParseHelper.tokenInformation[tokens[i]].nodeClass);
                    temporary.LeftOperand  = op2;
                    temporary.RightOperand = op1;
                    tokenStack.Push(temporary);
                }
                else if (tokens[i].IsFunctionCall())
                {
                    if (ParseHelper.tokenInformation[tokens[i]].numberOfParameters == 1)
                    {
                        var op1       = tokenStack.Pop();
                        var temporary = (IFunctionSingular)Activator.CreateInstance(ParseHelper.tokenInformation[tokens[i]].nodeClass);
                        temporary.Parameter = op1;
                        tokenStack.Push(temporary);
                    }
                    else
                    {
                        var op1       = tokenStack.Pop();
                        var op2       = tokenStack.Pop();
                        var temporary = (IFunctionDual)Activator.CreateInstance(ParseHelper.tokenInformation[tokens[i]].nodeClass);
                        temporary.FirstParameter  = op2;
                        temporary.SecondParameter = op1;
                        tokenStack.Push(temporary);
                    }
                }
            }
            this.root = tokenStack.Pop().Simplify();
        }