Пример #1
0
        private String parse(string[] tokens, Stack<string> vStk, Stack<char> sStk)
        {
            for (int i = 0; i < tokens.Length; i++)
            {
                string token = tokens[i];

                switch (token)
                {
                    case "+":
                    case "-":
                    case "*":
                    case "/":
                        while (!sStk.isEmpty())
                        {
                            if (!sStk.Peek().Equals("("))
                                if (precedence(sStk.Peek()) >= precedence(token[0]))
                                {
                                    vStk.Push(sStk.Pop() + "");
                                }
                                else
                                    break;
                        }
                        sStk.Push(token[0]);
                        break;
                    case "(":
                        sStk.Push(token[0]);
                        break;
                    case ")":
                        while (!sStk.isEmpty())
                        {
                            if (sStk.Peek().Equals('('))
                            {
                                sStk.Pop();
                                break;
                            }
                            vStk.Push(sStk.Pop() + "");
                        }
                        break;
                    default:
                        vStk.Push(token);
                        break;

                }
            }
            while (!sStk.isEmpty())
                vStk.Push(sStk.Pop() + "");

            String result = "";
            while (!vStk.isEmpty())
                result = vStk.Pop() + " " + result;
            return result.Trim();
        }