Пример #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);
        }
Пример #2
0
        static string InfixToPostfix(string opState)
        {
            if (0 != isOpt(opState[opState.Length - 1]))
            {
                return("错误");
            }
            Stack <char> opt = new Stack <char>();

            char[]        infix   = opState.ToCharArray();
            StringBuilder postfix = new StringBuilder();
            bool          isNum   = true;

            for (int i = 0; i < infix.Length; ++i)
            {
                if (infix[i] == ' ')
                {
                    continue;
                }
                else if ('0' <= infix[i] && infix[i] <= '9' || infix[i] == '.' || infix[i] == '-' && '0' <= infix[i + 1] && infix[i + 1] <= '9')
                {
                    if (!isNum)
                    {
                        postfix.Append(' ');
                    }
                    postfix.Append(infix[i]);
                    isNum = true;
                }
                else if ('(' == infix[i])
                {
                    isNum = false;
                    opt.Push('(');
                }
                else if (')' == infix[i])
                {
                    isNum = false;
                    try
                    {
                        while ('(' != opt.Peek())
                        {
                            postfix.Append(' ');
                            postfix.Append(opt.Pop());
                        }
                        opt.Pop();
                    }
                    catch (Exception)
                    {
                        return("错误");
                    }
                }
                else
                {
                    isNum = false;
                    if (opt.Count != 0 && Prior(infix[i], opt.Peek()) > 0)
                    {
                        opt.Push(infix[i]);
                    }
                    else
                    {
                        while (opt.Count != 0 && Prior(infix[i], opt.Peek()) <= 0)
                        {
                            postfix.Append(' ');
                            postfix.Append(opt.Pop());
                        }
                        opt.Push(infix[i]);
                    }
                }
            }
            while (opt.Count != 0)
            {
                postfix.Append(' ');
                postfix.Append(opt.Pop());
            }
            return(postfix.ToString());
        }