Пример #1
0
 private static bool NeedToPop(Stack <char> operatorStack, ExpressionEvaluator.Operator newOperator)
 {
     if (operatorStack.LongCount <char>() > 0L)
     {
         ExpressionEvaluator.Operator @operator = ExpressionEvaluator.CharToOperator(operatorStack.Peek());
         if (ExpressionEvaluator.IsOperator(@operator.character) && (newOperator.associativity == ExpressionEvaluator.Associativity.Left && newOperator.presedence <= @operator.presedence || newOperator.associativity == ExpressionEvaluator.Associativity.Right && newOperator.presedence < @operator.presedence))
         {
             return(true);
         }
     }
     return(false);
 }
Пример #2
0
        private static string[] InfixToRPN(string[] tokens)
        {
            Stack <char>   charStack = new Stack <char>();
            Stack <string> source    = new Stack <string>();

            foreach (string token in tokens)
            {
                if (ExpressionEvaluator.IsCommand(token))
                {
                    char character = token[0];
                    switch (character)
                    {
                    case '(':
                        charStack.Push(character);
                        continue;

                    case ')':
                        while (charStack.LongCount <char>() > 0L && (int)charStack.Peek() != 40)
                        {
                            source.Push(charStack.Pop().ToString());
                        }
                        if (charStack.LongCount <char>() > 0L)
                        {
                            int num = (int)charStack.Pop();
                            continue;
                        }
                        continue;

                    default:
                        ExpressionEvaluator.Operator newOperator = ExpressionEvaluator.CharToOperator(character);
                        while (ExpressionEvaluator.NeedToPop(charStack, newOperator))
                        {
                            source.Push(charStack.Pop().ToString());
                        }
                        charStack.Push(character);
                        continue;
                    }
                }
                else
                {
                    source.Push(token);
                }
            }
            while (charStack.LongCount <char>() > 0L)
            {
                source.Push(charStack.Pop().ToString());
            }
            return(source.Reverse <string>().ToArray <string>());
        }
Пример #3
0
        private static string[] InfixToRPN(string[] tokens)
        {
            Stack <char>   stack  = new Stack <char>();
            Stack <string> stack2 = new Stack <string>();

            for (int i = 0; i < tokens.Length; i++)
            {
                string text = tokens[i];
                if (ExpressionEvaluator.IsCommand(text))
                {
                    char c = text[0];
                    if (c == '(')
                    {
                        stack.Push(c);
                    }
                    else if (c == ')')
                    {
                        while (stack.LongCount <char>() > 0L && stack.Peek() != '(')
                        {
                            stack2.Push(stack.Pop().ToString());
                        }
                        if (stack.LongCount <char>() > 0L)
                        {
                            stack.Pop();
                        }
                    }
                    else
                    {
                        ExpressionEvaluator.Operator newOperator = ExpressionEvaluator.CharToOperator(c);
                        while (ExpressionEvaluator.NeedToPop(stack, newOperator))
                        {
                            stack2.Push(stack.Pop().ToString());
                        }
                        stack.Push(c);
                    }
                }
                else
                {
                    stack2.Push(text);
                }
            }
            while (stack.LongCount <char>() > 0L)
            {
                stack2.Push(stack.Pop().ToString());
            }
            return(stack2.Reverse <string>().ToArray <string>());
        }
Пример #4
0
        private static string[] InfixToRPN(string[] tokens)
        {
            Stack <char>   stack = new Stack <char>();
            Queue <string> queue = new Queue <string>();

            for (int i = 0; i < tokens.Length; i++)
            {
                string text = tokens[i];
                if (ExpressionEvaluator.IsCommand(text))
                {
                    char c = text[0];
                    if (c == '(')
                    {
                        stack.Push(c);
                    }
                    else if (c == ')')
                    {
                        while (stack.Count > 0 && stack.Peek() != '(')
                        {
                            queue.Enqueue(stack.Pop().ToString());
                        }
                        if (stack.Count > 0)
                        {
                            stack.Pop();
                        }
                    }
                    else
                    {
                        ExpressionEvaluator.Operator newOperator = ExpressionEvaluator.CharToOperator(c);
                        while (ExpressionEvaluator.NeedToPop(stack, newOperator))
                        {
                            queue.Enqueue(stack.Pop().ToString());
                        }
                        stack.Push(c);
                    }
                }
                else
                {
                    queue.Enqueue(text);
                }
            }
            while (stack.Count > 0)
            {
                queue.Enqueue(stack.Pop().ToString());
            }
            return(queue.ToArray());
        }
Пример #5
0
        private static T Evaluate <T>(string[] tokens)
        {
            Stack <string> stack = new Stack <string>();
            T result;

            for (int i = 0; i < tokens.Length; i++)
            {
                string text = tokens[i];
                if (ExpressionEvaluator.IsOperator(text))
                {
                    ExpressionEvaluator.Operator @operator = ExpressionEvaluator.CharToOperator(text[0]);
                    List <T> list = new List <T>();
                    bool     flag = true;
                    while (stack.Count > 0 && !ExpressionEvaluator.IsCommand(stack.Peek()) && list.Count < @operator.inputs)
                    {
                        T item;
                        flag &= ExpressionEvaluator.TryParse <T>(stack.Pop(), out item);
                        list.Add(item);
                    }
                    list.Reverse();
                    if (!flag || list.Count != @operator.inputs)
                    {
                        result = default(T);
                        return(result);
                    }
                    Stack <string> arg_CF_0 = stack;
                    T t = ExpressionEvaluator.Evaluate <T>(list.ToArray(), text[0]);
                    arg_CF_0.Push(t.ToString());
                }
                else
                {
                    stack.Push(text);
                }
            }
            if (stack.Count == 1)
            {
                T t2;
                if (ExpressionEvaluator.TryParse <T>(stack.Pop(), out t2))
                {
                    result = t2;
                    return(result);
                }
            }
            result = default(T);
            return(result);
        }
Пример #6
0
        private static bool NeedToPop(Stack <char> operatorStack, ExpressionEvaluator.Operator newOperator)
        {
            bool result;

            if (operatorStack.Count > 0)
            {
                ExpressionEvaluator.Operator @operator = ExpressionEvaluator.CharToOperator(operatorStack.Peek());
                if (ExpressionEvaluator.IsOperator(@operator.character))
                {
                    if ((newOperator.associativity == ExpressionEvaluator.Associativity.Left && newOperator.presedence <= @operator.presedence) || (newOperator.associativity == ExpressionEvaluator.Associativity.Right && newOperator.presedence < @operator.presedence))
                    {
                        result = true;
                        return(result);
                    }
                }
            }
            result = false;
            return(result);
        }
Пример #7
0
        private static T Evaluate <T>(string[] tokens)
        {
            Stack <string> source = new Stack <string>();

            foreach (string token in tokens)
            {
                if (ExpressionEvaluator.IsOperator(token))
                {
                    ExpressionEvaluator.Operator @operator = ExpressionEvaluator.CharToOperator(token[0]);
                    List <T> objList = new List <T>();
                    bool     flag    = true;
                    while (source.LongCount <string>() > 0L && !ExpressionEvaluator.IsCommand(source.Peek()) && objList.Count < @operator.inputs)
                    {
                        T result;
                        flag &= ExpressionEvaluator.TryParse <T>(source.Pop(), out result);
                        objList.Add(result);
                    }
                    objList.Reverse();
                    if (!flag || objList.Count != @operator.inputs)
                    {
                        return(default(T));
                    }
                    source.Push(ExpressionEvaluator.Evaluate <T>(objList.ToArray(), token[0]).ToString());
                }
                else
                {
                    source.Push(token);
                }
            }
            T result1;

            if (source.LongCount <string>() == 1L && ExpressionEvaluator.TryParse <T>(source.Pop(), out result1))
            {
                return(result1);
            }
            return(default(T));
        }