/// <summary> /// 計算出運算樹的計算結果 /// </summary> /// <param name="tree">運算樹</param> /// <returns>計算結果</returns> public decimal EvaluateTree(ExpressionTree tree) { Node top = tree.GetTop(); decimal ans = PackNode(top).Value.Number ?? 0; return(ans); }
/// <summary> /// 加一棵子樹進來 /// </summary> /// <param name="tree">子樹</param> public void Add(ExpressionTree tree) { //如果是第一個數字 if (Root == null) { this.Root = tree.Root; this.CurrentNode = tree.CurrentNode; } if (CurrentNode.IsOperator()) { Node topNode = tree.GetTop(); CurrentNode.RightNode = topNode; topNode.ParentNode = CurrentNode; //主樹現有node必須是子樹的頂端 CurrentNode = topNode; } this.LastCurrentNode = tree.GetTop(); }
/// <summary> /// 計算樹的結果 /// </summary> /// <param name="tree">運算樹</param> /// <returns>運算答案</returns> public decimal EvaluateTree(ExpressionTree tree) { Node top = tree.GetTop(); if (top.NodeValue.Number.HasValue) { return(top.NodeValue.Number.Value); } decimal ans = PackNode(top).NodeValue.Number.Value; return(ans); }
/// <summary> /// 計算樹的結果 /// </summary> /// <param name="tree">運算樹</param> /// <returns>運算答案</returns> public decimal EvaluateTree(ExpressionTree tree) { System.Diagnostics.Debug.WriteLine($"there 11"); Node top = tree.GetTop(); if (top.NodeValue.Number.HasValue) { return(top.NodeValue.Number.Value); } decimal ans = PackNode(top).NodeValue.Number.Value; return(ans); }