Esempio n. 1
0
        /// <summary>
        /// 将字符串解析为中序表达式
        /// </summary>
        /// <param name="str">要解析的字符串</param>
        /// <returns>一个中序表达式结构体</returns>
        static public MiddleExpression TurnToMiddle(string str)
        {
            string           s        = str.Replace(" ", "");
            MiddleExpression m        = new MiddleExpression();
            CalcNode         LastNode = new CalcNode(NodeType.Num);

            char[] cs = s.ToCharArray();
            LastNode = null;
            foreach (char c in cs)
            {
                if (c >= 48 && c <= 57)
                {
                    if (LastNode != null && LastNode.Type == NodeType.Num)
                    {
                        LastNode.Num = LastNode.Num * 10 + c - 48;
                    }
                    else
                    {
                        LastNode = CalcNode.FromDouble(c - 48.0);
                        m.Add(LastNode);
                    }
                }
                else if (Operators.Contains(c))
                {
                    LastNode = CalcNode.FromChar(c);
                    m.Add(LastNode);
                }
                else
                {
                    if (LastNode != null && LastNode.Type == NodeType.Variable)
                    {
                        LastNode.Value += c;
                    }
                    else
                    {
                        LastNode       = new CalcNode(NodeType.Variable);
                        LastNode.Value = c.ToString();
                        m.Add(LastNode);
                    }
                }
            }
            // 检查是否有计算节点是函数
            foreach (CalcNode node in m)
            {
                if (node.Type == NodeType.Variable)
                {
                    if (Functions.Contains(node.Value))
                    {
                        node.Type = NodeType.Function;
                    }
                }
            }
            return(m);
        }
Esempio n. 2
0
        static public MiddleExpression TurnToMiddle(string str)
        {
            string           s        = str.Replace(" ", "");
            MiddleExpression m        = new MiddleExpression();
            CalcNode         LastNode = new CalcNode(NodeType.Num);

            char[] cs = s.ToCharArray();
            LastNode = null;
            foreach (char c in cs)
            {
                if (c >= 48 && c <= 57)
                {
                    if (LastNode != null && LastNode.Type == NodeType.Num)
                    {
                        LastNode.Num = LastNode.Num * 10 + c - 48;
                    }
                    else
                    {
                        LastNode = CalcNode.FromDouble(c - 48.0);
                        m.Add(LastNode);
                    }
                }
                else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '^' || c == '(' || c == ')')
                {
                    LastNode = CalcNode.FromChar(c);
                    m.Add(LastNode);
                }
                else
                {
                    LastNode       = new CalcNode(NodeType.Variable);
                    LastNode.Value = c;
                    m.Add(LastNode);
                }
            }
            return(m);
        }