Пример #1
0
        public static string GetMessage(string message)
        {
            var output    = string.Empty;
            var operStack = new Stack <char>();

            for (var i = 0; i < message.Length; i++)
            {
                if (IsDelimeter(message[i]))
                {
                    continue;
                }
                if (Char.IsDigit(message[i]))
                {
                    while (!IsDelimeter(message[i]) && !IsOperator(message[i]))
                    {
                        output += message[i++];
                        if (i == message.Length)
                        {
                            break;
                        }
                    }
                    output += ' ';
                    i--;
                }
                if (IsOperator(message[i]))
                {
                    if (message[i] == '-' && output == string.Empty)
                    {
                        output += "0 ";
                    }
                    if (operStack.Count() > 0)
                    {
                        if (message[i] == '-' && operStack.Peek() == '(')
                        {
                            output += "0 ";
                        }
                    }
                    switch (message[i])
                    {
                    case '(':
                        operStack.Push(message[i]);
                        break;

                    case ')':
                    {
                        var s = operStack.Pop();
                        if (s == '(' && IsOperator(output[output.Length - 1]))
                        {
                            output += "0 ";
                        }
                        while (s != '(')
                        {
                            output += s.ToString() + ' ';
                            s       = operStack.Pop();
                        }
                    }
                    break;

                    default:
                        if (operStack.Count > 0)
                        {
                            if (GetPriority(message[i]) <= GetPriority(operStack.Peek()))
                            {
                                output += operStack.Pop().ToString() + " ";
                            }
                        }
                        operStack.Push(char.Parse(message[i].ToString()));
                        break;
                    }
                }
            }
            while (operStack.Count > 0)
            {
                output += operStack.Pop() + " ";
            }
            return(output);
        }