public String calculate(CalculatorBean calculatorBean, String mnemonic) { if ("%".Equals(mnemonic)) { Double? value = null; if (calculatorBean.Buffer != null) { value = Double.Parse(calculatorBean.Buffer.ToString()); calculatorBean.Buffer = null; } else if (calculatorBean.FirstNumber != null) { value = calculatorBean.FirstNumber.GetValueOrDefault(0); } if (value != null) { value = value * 0.01; log.Debug(value); if (calculatorBean.Operation != null) { calculatorBean.SecondNumber = value; } else { calculatorBean.FirstNumber = value; } log.InfoFormat("Percentage calculated: {0}.", value); return CalculatorUtils.formatNumber(value.GetValueOrDefault(0)); } } return null; }
public String calculate(CalculatorBean calculatorBean, String mnemonic) { if ("=".Equals(mnemonic)) { // jeśli operacja i bufor jest dostępny to wartość przepisywana jest do drugiej liczby if (calculatorBean.Operation != null && calculatorBean.Buffer != null) { calculatorBean.SecondNumber = Double.Parse(calculatorBean.Buffer.ToString()); log.InfoFormat("Second number accepted: {0}", calculatorBean.SecondNumber); } // operacja zawsze czyści bufor calculatorBean.Buffer = null; // jeżeli dostępna jest operacja do wykonania, ale drugiej liczby nie ma, powtarzana jest pierwsza z liczb if (calculatorBean.Operation != null && calculatorBean.SecondNumber == null) { log.InfoFormat("First number copied to be second number : {0}", calculatorBean.FirstNumber); calculatorBean.SecondNumber = calculatorBean.FirstNumber; } // jeżeli dwie liczby znajdują się w pamięci to wykonujemy na nich operację if (calculatorBean.FirstNumber != null && calculatorBean.SecondNumber != null) { Double result = calculatorBean.Operation(calculatorBean.FirstNumber.GetValueOrDefault(0), calculatorBean.SecondNumber.GetValueOrDefault(0)); log.InfoFormat("Calucation result: {0}", result); if (Double.IsInfinity(result) || Double.IsNaN(result)) { CalculationException calculationException = new CalculationException(); log.Warn("Calcuation error!", calculationException); throw calculationException; } log.InfoFormat("Result copied to first number: {0}", result); calculatorBean.FirstNumber = result; return CalculatorUtils.formatNumber(result); } } return null; }
public String calculate(CalculatorBean calculatorBean, String mnemonic) { if (!"+/-".Equals(mnemonic)) { return null; } // jeżeli bufor jest dostępny to zmieniamy w nim pierwszy znak if (calculatorBean.Buffer != null) { if (calculatorBean.Buffer[0] == '-') { calculatorBean.Buffer.Remove(0, 1); } else { calculatorBean.Buffer.Insert(0, '-'); } log.InfoFormat("Sign changed!"); return calculatorBean.Buffer.ToString(); // jeżeli w pamięci istnieje już jakaś liczba to zmieniamy jej znak } else if (calculatorBean.FirstNumber != null) { Double value = calculatorBean.FirstNumber.GetValueOrDefault(0); if (Double.IsInfinity(value) || Double.IsNaN(value)) { throw new CalculationException(); } calculatorBean.FirstNumber = -value; return CalculatorUtils.formatNumber(-value); } return null; }
public String calculate(CalculatorBean calculatorBean, String mnemonic) { if ("C".Equals(mnemonic)) { calculatorBean.IsBlocked = false; calculatorBean.Buffer = null; calculatorBean.FirstNumber = null; calculatorBean.SecondNumber = null; calculatorBean.Operation = null; return "0"; } return null; }
public String calculate(CalculatorBean calculatorBean, String mnemonic) { if ("sqrt".Equals(mnemonic)) { Double value; if (calculatorBean.Buffer != null) { value = Double.Parse(calculatorBean.Buffer.ToString()); calculatorBean.Buffer = null; } else { value = calculatorBean.FirstNumber.GetValueOrDefault(0); } value = Math.Sqrt(value); if (Double.IsNaN(value) || Double.IsInfinity(value)) { throw new CalculationException(); } log.InfoFormat("Square root calculated and copied to first number: {0}", value); calculatorBean.FirstNumber = value; return CalculatorUtils.formatNumber(value); } return null; }
public String calculate(CalculatorBean calculatorBean, String mnemonic) { if (!SIGNS.Contains(mnemonic)) { return null; } StringBuilder buffer; if (calculatorBean.Buffer != null) { buffer = calculatorBean.Buffer; } else { buffer = new StringBuilder(); calculatorBean.Buffer = buffer; if (calculatorBean.FirstNumber != null && calculatorBean.Operation == null) { calculatorBean.FirstNumber = null; } } if (!CalculatorUtils.hasMaxNumberOfDigits(buffer.ToString())) { if (".".Equals(mnemonic) && buffer.Length == 0) { buffer.Append("0").Append(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator); } else if ("0".Equals(buffer.ToString()) && !".".Equals(mnemonic)) { calculatorBean.Buffer = buffer = new StringBuilder(mnemonic); } else if (!(".".Equals(mnemonic) && buffer.ToString() .Contains(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator))) { if (".".Equals(mnemonic)) { buffer.Append(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator); } else { buffer.Append(mnemonic); } } } return buffer.ToString(); }
public String calculate(CalculatorBean calculatorBean, String mnemonic) { if (MATH_OPERATIONS.ContainsKey(mnemonic)) { Double? newValue = null; if (calculatorBean.Buffer != null) { newValue = Double.Parse(calculatorBean.Buffer.ToString()); if (calculatorBean.FirstNumber != null) { log.InfoFormat("Accepted the second number to calculate: {0}", newValue); calculatorBean.SecondNumber = newValue; } else { log.InfoFormat("Accepted the first number to calculate: {0}", newValue); calculatorBean.FirstNumber = newValue; } calculatorBean.Buffer = null; } if (calculatorBean.Operation != null && calculatorBean.SecondNumber != null) { newValue = calculatorBean.Operation(calculatorBean.FirstNumber.GetValueOrDefault(0), calculatorBean.SecondNumber.GetValueOrDefault(0)); log.InfoFormat("Calucation result: {0}", newValue); if (Double.IsInfinity(newValue.GetValueOrDefault(0)) || Double.IsNaN(newValue.GetValueOrDefault(0))) { CalculationException calculationException = new CalculationException(); log.Warn("Calcuation error!", calculationException); throw calculationException; } calculatorBean.FirstNumber = newValue; calculatorBean.SecondNumber = null; } if (MATH_OPERATIONS.ContainsKey(mnemonic)) { log.InfoFormat("Accepted the operation to calculate: {0}", mnemonic); calculatorBean.Operation = MATH_OPERATIONS[mnemonic]; } if (newValue != null) { return CalculatorUtils.formatNumber(newValue.GetValueOrDefault(0)); } } return null; }