public override int Calculate() { if (RightChild.Calculate() == 0) { throw new DivideByZeroException(); } return(LeftChild.Calculate() / RightChild.Calculate()); }
public override double Calculate() { if (Math.Abs(RightChild.Calculate()) < double.Epsilon) { throw new DivideByZeroException(); } return(LeftChild.Calculate() / RightChild.Calculate()); }
/// <summary> /// Calculates the node value /// </summary> /// <returns>Value of node after calculation</returns> public override int Calculate() => LeftChild.Calculate() / RightChild.Calculate();
public override double Calculate() => LeftChild.Calculate() *RightChild.Calculate();
public override int Calculate() { return(LeftChild.Calculate() + RightChild.Calculate()); }
public List <String> Calculate() { if (LeftChild == null && RightChild == null) { return(StringListValue); } if (LeftChild.IsLeaf && RightChild.IsLeaf) { if (LeftChild.IsVariable || RightChild.IsVariable) { var result = new List <String>(); switch (Operation) { case Operations.Add: result.Add("("); result.AddRange(LeftChild.StringListValue); result.Add("+"); result.AddRange(RightChild.StringListValue); result.Add(")"); return(result); case Operations.Subtract: result = new List <String>(); result.Add("("); result.AddRange(LeftChild.StringListValue); result.Add("-"); result.AddRange(RightChild.StringListValue); result.Add(")"); return(result); case Operations.Multiply: result = new List <String>(); result.AddRange(LeftChild.StringListValue); result.Add("*"); result.AddRange(RightChild.StringListValue); return(result); case Operations.Divide: result = new List <String>(); result.AddRange(LeftChild.StringListValue); result.Add("/"); result.AddRange(RightChild.StringListValue); return(result); default: throw new UnacceptableExpressionException("Undefined operation"); } } else { switch (Operation) { case Operations.Add: Double?result = LeftChild.NumberValue + RightChild.NumberValue; return(new List <String>() { result.ToString() }); case Operations.Subtract: result = LeftChild.NumberValue - RightChild.NumberValue; return(new List <String>() { result.ToString() }); case Operations.Multiply: result = LeftChild.NumberValue * RightChild.NumberValue; return(new List <String>() { result.ToString() }); case Operations.Divide: result = LeftChild.NumberValue / RightChild.NumberValue; return(new List <String>() { result.ToString() }); default: throw new UnacceptableExpressionException("Undefined operation"); } } } else { var leftResult = LeftChild.Calculate(); var rightResult = RightChild.Calculate(); var finalResult = new List <String>(); if (Operation == Operations.Add || Operation == Operations.Subtract) { Boolean readyToUse = !canBeSimlified(leftResult); while (!readyToUse) { readyToUse = !canBeSimlified(leftResult); } readyToUse = !canBeSimlified(rightResult); while (!readyToUse) { readyToUse = !canBeSimlified(rightResult); } } if (leftResult.Count == 1 && rightResult.Count == 1 && Double.TryParse(leftResult[0], out Double leftValue) && Double.TryParse(rightResult[0], out Double rightValue)) { switch (Operation) { case Operations.Add: finalResult.Add((leftValue + rightValue).ToString()); break; case Operations.Subtract: finalResult.Add((leftValue - rightValue).ToString()); break; case Operations.Multiply: finalResult.Add((leftValue * rightValue).ToString()); break; case Operations.Divide: finalResult.Add((leftValue / rightValue).ToString()); break; default: throw new UnacceptableExpressionException("Undefined operation"); } return(finalResult); } else { finalResult = new List <string>(); finalResult.Add("("); finalResult.AddRange(leftResult); switch (Operation) { case Operations.Add: finalResult.Add("+"); break; case Operations.Subtract: finalResult.Add("-"); break; case Operations.Multiply: finalResult.Add("*"); break; case Operations.Divide: finalResult.Add("/"); break; default: throw new UnacceptableExpressionException("Undefined operation"); } finalResult.AddRange(rightResult); finalResult.Add(")"); switch (Operation) { case Operations.Multiply: finalResult.RemoveAt(0); finalResult.RemoveAt(finalResult.Count - 1); break; case Operations.Divide: finalResult.RemoveAt(0); finalResult.RemoveAt(finalResult.Count - 1); break; } return(finalResult); } } }