예제 #1
0
        /// <summary>
        /// draw evaluation tree to treeview
        /// </summary>
        /// <param name="parent">parent or null for root</param>
        /// <param name="node">node to draw</param>
        private void DrawTree(TreeNode parent, LeafNode node)
        {
            if (node != null)
            {
                // null parent is root
                if (parent == null)
                {
                    parent = this.treeView1.Nodes.Add(node.Result.ToString());
                    parent = this.treeView1.Nodes[0];
                }

                // create tree node
                TreeNode p = new TreeNode(node.ToString());
                if (node.Type == NodeType.Operator) // operator - colorize and set tooltip
                {
                    // farba
                    p.ForeColor = Color.Green;
                    // tooltip
                    if (node.Right.Result < 0) // negative number to brackets
                        p.ToolTipText = string.Format(
                            System.Globalization.CultureInfo.CurrentCulture,
                            "{0}{1}({2})={3}",
                            node.Left.Result, node.ToString(), node.Right.Result, node.Result);
                    else
                        p.ToolTipText = string.Format(
                            System.Globalization.CultureInfo.CurrentCulture,
                            "{0}{1}{2}={3}",
                            node.Left.Result, node.ToString(), node.Right.Result, node.Result);
                }
                parent.Nodes.Add(p);
                this.DrawTree(p, node.Left);  // left
                this.DrawTree(p, node.Right); // right
            }
        }
예제 #2
0
        /// <summary>
        /// reates evaluation tree from postfix
        /// </summary>
        /// <param name="postfixNodes">postfix array</param>
        /// <returns>tree root node</returns>
        private LeafNode ToEvaluationTree(Node[] postfixNodes)
        {
            Stack stack = new Stack();
            foreach (Node node in postfixNodes)
            {
                LeafNode ln;
                switch (node.Type)
                {
                    case NodeType.Operand: // operand - only push to stack
                        ln = new LeafNode();
                        ln.Data = node.Value;
                        stack.Push(ln);
                        break;
                    case NodeType.Operator: // operator: setup left and right side and push to stack
                        ln = new LeafNode();
                        ln.Data = node.Value;
                        ln.Right = (LeafNode)stack.Pop();
                        ln.Left = (LeafNode)stack.Pop();
                        stack.Push(ln);
                        break;
                }
            }

            if (stack.Count > 0)
                return (LeafNode)stack.Pop();
            else
                return null;
        }
예제 #3
0
 private void MakeXmlTree(XmlElement parent, LeafNode node)
 {
     if (node != null)
     {
         // creating element
         XmlElement el = parent.OwnerDocument.CreateElement(node.Type.ToString());
         XmlAttribute attr = parent.OwnerDocument.CreateAttribute("data");
         attr.Value=node.ToString();
         el.Attributes.Append(attr);
         if (node.Type == NodeType.Operator) // operator - expression and result attributes
         {
             XmlAttribute a = el.Attributes.Append(parent.OwnerDocument.CreateAttribute("expression"));
             if (node.Right.Result < 0) // negative number to bracket
                 a.Value = string.Format(
                     System.Globalization.CultureInfo.CurrentCulture,
                     "{0}{1}({2})",
                     node.Left.Result, node.ToString(), node.Right.Result);
             else
                 a.Value = string.Format(
                     System.Globalization.CultureInfo.CurrentCulture,
                     "{0}{1}{2}",
                     node.Left.Result, node.ToString(), node.Right.Result);
             el.Attributes.Append(a);
             a = el.Attributes.Append(parent.OwnerDocument.CreateAttribute("result"));
             a.Value = node.Result.ToString();
             el.Attributes.Append(a);
         }
         parent.AppendChild(el);
         this.MakeXmlTree(el, node.Left);  // left
         this.MakeXmlTree(el, node.Right); // right
     }
 }