コード例 #1
0
        private static string OnTraversePrefixExpression(this IPrefixExpression prefix, Func <string, Operators, string, string> action)
        {
            Stack <string>    stack  = new Stack <string>();
            IParsedExpression parsed = prefix.ParseExpression();
            // Length of expression
            int l = parsed.Value.Length;

            // Reading from right to left
            for (int i = l - 1; i >= 0; i--)
            {
                char c = parsed.Value[i];

                // Check whether the character is an operator
                if (c.IsOperator())
                {
                    // Check whether the character is operator of type Negation
                    if (((char)Operators.Negation).Equals(c))
                    {
                        // Get the previous result from the stack,calculate the next nandified result and put it in the stack
                        string value = stack.Pop();
                        stack.Push(action(value, Operators.Negation, null));
                    }
                    else
                    {
                        // Get the previous result from the stack,calculate the next nandified result and put it in the stack
                        string value1 = stack.Pop();
                        string value2 = stack.Pop();
                        stack.Push(action(value1, (Operators)c, value2));
                    }
                }
                else
                {
                    // Put the variable in the stack
                    stack.Push($@"{c}");
                }
            }
            return(stack.Pop());
        }
コード例 #2
0
ファイル: ExpressionStructure.cs プロジェクト: borislavvp/ALE
 public ExpressionStructure(IPrefixExpression expression)
 {
     this.PrefixExpression = expression ?? throw new NullReferenceException("Invalid prefix expression");
 }