Ejemplo n.º 1
0
        /// <summary>
        /// 为表达式插入空格
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string InsertBlank(string source)
        {
            StringBuilder sb   = new StringBuilder();
            var           list = source.ToCharArray();

            foreach (var temp in list)
            {
                if (OperatorLevel.ContainsKey(temp.ToString()))
                {
                    sb.Append(" ");
                    sb.Append(temp.ToString());
                    sb.Append(" ");
                }
                else
                {
                    sb.Append(temp);
                }
            }
            return(sb.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 根据逆波兰表达式获取结构
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static double GetResult(string source)
        {
            Stack <string> stack = new Stack <string>();
            var            list  = source.Split(' ');

            for (int i = 0; i < list.Length; i++)
            {
                string current = list[i];
                if (Regex.IsMatch(current, "^([0-9]{1,}){1}"))             // "^(-?[0-9]*[.]*[0-9]{0,3})$"))
                {
                    stack.Push(current);
                }
                else if (OperatorLevel.ContainsKey(current))
                {
                    double right = double.Parse(stack.Pop());
                    double left  = double.Parse(stack.Pop());
                    stack.Push(GetValue(left, right, current[0]).ToString());
                }
            }
            return(double.Parse(stack.Pop()));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 转化为逆波兰表达式
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string ConvertToRPN(string source)
        {
            StringBuilder  result = new StringBuilder();
            Stack <string> stack  = new Stack <string>();

            string[] list = source.Split(' ');
            for (int i = 0; i < list.Length; i++)
            {
                string current = list[i];
                if (Regex.IsMatch(current, "^([0-9]{1,}){1}"))
                {
                    result.Append(current + " ");
                }
                else if (OperatorLevel.ContainsKey(current))
                {
                    if (stack.Count > 0)
                    {
                        var prev = stack.Peek();
                        if (prev == "(")
                        {
                            stack.Push(current);
                            continue;
                        }
                        if (current == "(")
                        {
                            stack.Push(current);
                            continue;
                        }
                        if (current == ")")
                        {
                            while (stack.Count > 0 && stack.Peek() != "(")
                            {
                                result.Append(stack.Pop() + " ");
                            }
                            //Pop the "("
                            stack.Pop();
                            continue;
                        }
                        if (OperatorLevel[current] < OperatorLevel[prev])
                        {
                            while (stack.Count > 0)
                            {
                                var top = stack.Pop();
                                if (top != "(" &&
                                    top != ")")
                                {
                                    result.Append(top + " ");
                                }
                                else
                                {
                                    break;
                                }
                            }
                            stack.Push(current);
                        }
                        else
                        {
                            stack.Push(current);
                        }
                    }
                    else
                    {
                        stack.Push(current);
                    }
                }
            }
            if (stack.Count > 0)
            {
                while (stack.Count > 0)
                {
                    var top = stack.Pop();
                    if (top != "(" && top != ")")
                    {
                        result.Append(top + " ");
                    }
                }
            }
            return(result.ToString());
        }