public void AddOperation(CalculatorOperation op) { countAfterPoint = 1; isPoint = false; if (LeftValue == null) { LeftValue = 0; } if (Operation != null) { if (RightValue != 0) { Compute(); } } else { LeftValue = RightValue; OnDidChangeLeft?.Invoke(this, new CalculatorEventArgs("Left change", LeftValue, RightValue, Operation)); } Operation = op; RightValue = null; OnDidChangeOperation?.Invoke(this, new CalculatorEventArgs("Change operation", LeftValue, RightValue, Operation)); }
public void Compute() { try { switch (Operation) { case CalculatorOperation.Add: LeftValue += RightValue; break; case CalculatorOperation.Mul: if (LeftValue != 0) { LeftValue *= RightValue; } else { LeftValue = RightValue; } break; case CalculatorOperation.Sub: if (LeftValue != 0) { LeftValue -= RightValue; } else { LeftValue = RightValue; } break; case CalculatorOperation.Div: if (LeftValue != 0) { LeftValue /= RightValue; } else { LeftValue = RightValue; } break; default: LeftValue = RightValue; break; } OnDidChangeLeft?.Invoke(this, new CalculatorEventArgs("Left change", LeftValue, RightValue, Operation)); //OnDidChangeRight?.Invoke(this, new CalculatorEventArgs("Right change", LeftValue, RightValue, Operation)); Result = LeftValue; OnDidCompute?.Invoke(this, new ComputeEventArgs (LeftValue, RightValue, Operation, Result)); } catch { OnUnableToCompute?.Invoke(this, new CalculatorEventArgs("Error", LeftValue, RightValue, Operation)); throw new ArgumentException(); } }
public void AddOperation(CalculatorOperation op) { if (Operation != null) { Compute(); } else { LeftValue = RightValue; OnDidChangeLeft?.Invoke(this, new CalculatorEventArgs("Left change", LeftValue, RightValue, Operation)); } Operation = op; OnDidChangeOperation?.Invoke(this, new CalculatorEventArgs("Change operation", LeftValue, RightValue, Operation)); RightValue = null; }