void Zero(string msg, bool isInput) { if (isInput) { calcState = CalcState.Zero; } else { if (Rules.IsNonZeroDigit(msg)) { AccumulateDigits(msg, true); } if (Rules.IsSeparator(msg)) { AccumulateDigits(msg, true); } if (Rules.IsMemoryOperation(msg)) { memoryNumberToFirst = true; MemorySave(msg, true); } } }
void MemorySave(string msg, bool isInput) { if (isInput) { calcState = CalcState.MemorySave; double result = double.Parse(memoryNumber.Replace(",", "."), System.Globalization.CultureInfo.InvariantCulture);; double num; if (secondNumber == "") { secondNumber = "0"; } if (resultToMemory) { num = double.Parse(resultNumber.Replace(",", "."), System.Globalization.CultureInfo.InvariantCulture); } else { num = double.Parse(secondNumber.Replace(",", "."), System.Globalization.CultureInfo.InvariantCulture); } if (msg == "M+") { result += num; memoryNumber = result.ToString(); memoryBox.Add(memoryNumber); } else if (msg == "M-") { result -= num; memoryNumber = result.ToString(); memoryBox.Add(memoryNumber); } else if (msg == "MS") { memoryNumber = num.ToString(); } else if (msg == "MR") { if (memoryNumberToFirst) { operation = ""; firstNumber = memoryNumber; changeTextDelegate.Invoke(firstNumber); } else { secondNumber = memoryNumber; changeTextDelegate.Invoke(secondNumber); } } else if (msg == "MC") { memoryNumber = "0"; memoryBox = new List <string>(); } memoryNumberToFirst = false; } else { if (Rules.IsDigit(msg)) { FullReset(); AccumulateDigits(msg, true); } else if (Rules.IsMemoryOperation(msg)) { MemorySave(msg, true); } else if (Rules.IsOperation(msg)) { Operation(msg, true); } else if (Rules.IsResult(msg)) { Result(msg, true); } else if (Rules.IsFullReset(msg)) { FullReset(); } } resultToMemory = false; }
void Result(string msg, bool isInput) { if (isInput) { calcState = CalcState.Result; if (operation == "Mod") { resultNumber = (int.Parse(firstNumber) % int.Parse(secondNumber)).ToString(); } else if (operation == "pow") { resultNumber = Math.Pow(double.Parse(firstNumber), double.Parse(secondNumber)).ToString(); } else if (operation == "root") { resultNumber = Math.Pow(double.Parse(firstNumber), 1.0 / double.Parse(secondNumber)).ToString(); } else if (isExpression == true || isAdvancedExpression == true) { if (operations.Peek() == "+") { sumNumbers.Push(int.Parse(secondNumber)); } else if (operations.Peek() == "-") { sumNumbers.Push(int.Parse(secondNumber) * (-1)); } else if (operations.Peek() == "x") { if (signs.Peek() == "-") { precendence *= (-1); } precendence *= int.Parse(secondNumber); sumNumbers.Push(precendence); } else if (operations.Peek() == "÷") { if (signs.Peek() == "-") { precendence *= (-1); } precendence /= int.Parse(secondNumber); sumNumbers.Push(precendence); } int temp = 0; while (sumNumbers.Count > 0) { temp += sumNumbers.Peek(); sumNumbers.Pop(); } resultNumber = temp.ToString(); } /*else if (isAdvancedExpression == true) * { * * }*/ else { resultNumber = PerformCalculation(); } changeTextDelegate.Invoke(resultNumber); firstNumber = resultNumber; isExpression = false; isAdvancedExpression = false; reccuring = new List <string>(); changeRecurringTextDelegate.Invoke(""); } else { if (Rules.IsOperation(msg)) { afterResult = true; Operation(msg, true); } else if (Rules.IsDigit(msg)) { AccumulateDigits(msg, true); } else if (Rules.IsResult(msg)) { Result(msg, true); } else if (Rules.IsMemoryOperation(msg)) { memoryNumberToFirst = true; resultToMemory = true; MemorySave(msg, true); } else if (Rules.IsFullReset(msg)) { FullReset(); } else if (Rules.IsReset(msg)) { Reset(); } } }
void Operation(string msg, bool isInput) { if (isInput) { calcState = CalcState.Operation; if (operation.Length != 0 && afterResult == false) { isExpression = true; if ((msg == "x" || msg == "÷") && (operations.Peek() == "+" || operations.Peek() == "-")) { operation = msg; operations.Push(operation); firstNumber = secondNumber; reccuring.Add(secondNumber); secondNumber = ""; precendence = int.Parse(firstNumber); } else if ((msg == "x" || msg == "÷") && (operations.Peek() == "x" || operations.Peek() == "÷")) { if (operation == "x") { precendence *= int.Parse(secondNumber); } else if (operation == "÷") { precendence /= int.Parse(secondNumber); } changeTextDelegate.Invoke(precendence.ToString()); operation = msg; operations.Push(operation); reccuring.Add(secondNumber); secondNumber = ""; } else if ((msg == "+" || msg == "-") && (operations.Peek() == "x" || operations.Peek() == "÷")) { if (operation == "x") { precendence *= int.Parse(secondNumber); } else if (operation == "÷") { precendence /= int.Parse(secondNumber); } if (signs.Count > 0 && signs.Peek() == "-") { precendence *= (-1); } sumNumbers.Push(precendence); Stack <int> stack = new Stack <int>(); int temp = 0; while (sumNumbers.Count > 0) { temp += sumNumbers.Peek(); stack.Push(sumNumbers.Peek()); sumNumbers.Pop(); } while (stack.Count > 0) { sumNumbers.Push(stack.Peek()); stack.Pop(); } changeTextDelegate.Invoke(temp.ToString()); precendence = 0; operation = msg; operations.Push(operation); signs.Push(operation); reccuring.Add(secondNumber); secondNumber = ""; } else if ((msg == "+" || msg == "-") && (operations.Peek() == "-" || operations.Peek() == "-")) { if (operation == "-") { sumNumbers.Push(int.Parse(secondNumber) * (-1)); } else if (operation == "+") { sumNumbers.Push(int.Parse(secondNumber)); } operation = msg; operations.Push(operation); signs.Push(operation); reccuring.Add(secondNumber); secondNumber = ""; } } else if (operation.Length != 0 && afterResult == true) { operation = msg; firstNumber = resultNumber; reccuring.Add(secondNumber); secondNumber = ""; afterResult = false; } else { operation = msg; operations.Push(operation); if (operation == "+" || operation == "-") { sumNumbers.Push(int.Parse(secondNumber)); signs.Push(operation); } else if (operation == "x" || operation == "÷") { precendence = int.Parse(secondNumber); } reccuring.Add(secondNumber); if (firstNumber == "") { firstNumber = secondNumber; } //reccuring.Add(firstNumber); secondNumber = ""; } string text = string.Join(" ", reccuring.ToArray()); changeRecurringTextDelegate.Invoke(text); } else { if (Rules.IsDigit(msg)) { AccumulateDigits(msg, true); } else if (Rules.IsResult(msg)) { secondNumber = firstNumber; Result(msg, true); } else if (Rules.IsFullReset(msg)) { FullReset(); } else if (Rules.IsReset(msg)) { Reset(); } else if (Rules.IsMemoryOperation(msg)) { MemorySave(msg, true); } else if (msg == "(") { reccuring.Add("("); string text = string.Join(" ", reccuring.ToArray()); changeRecurringTextDelegate.Invoke(text); Expression(msg, true); } } }
void AccumulateDigits(string msg, bool isInput) { if (isInput) { calcState = CalcState.AccumulateDigits; if (msg == "," && secondNumber.Contains(',') == false) { secondNumber = "0,"; changeTextDelegate.Invoke(secondNumber); } else { secondNumber += msg; changeTextDelegate.Invoke(secondNumber); } } else { if (Rules.IsDigit(msg)) { secondNumber += msg; changeTextDelegate.Invoke(secondNumber); } else if (Rules.IsSeparator(msg)) { if (secondNumber.Contains(',') == false) { secondNumber += ","; changeTextDelegate.Invoke(secondNumber); } } else if (Rules.IsChangingSign(msg)) { if (secondNumber[0] == '-') { secondNumber = secondNumber.Remove(0, 1); } else { secondNumber = "-" + secondNumber; } changeTextDelegate.Invoke(secondNumber); } else if (Rules.IsOperation(msg)) { Operation(msg, true); reccuring.Add(msg); string text = string.Join(" ", reccuring.ToArray()); changeRecurringTextDelegate.Invoke(text); } else if (Rules.IsPercent(msg)) { if (operation == "+" || operation == "-") { CalculatePercent(); } else if (operation == "x" || operation == "÷") { CalculateSecondPercent(); } changeTextDelegate.Invoke(secondNumber); } else if (Rules.IsResult(msg)) { Result(msg, true); } else if (Rules.IsFullReset(msg)) { FullReset(); } else if (Rules.IsReset(msg)) { Reset(); } else if (Rules.IsBackspace(msg)) { secondNumber = secondNumber.Remove(secondNumber.Length - 1); changeTextDelegate.Invoke(secondNumber); if (secondNumber.Length <= 0) { changeTextDelegate.Invoke("0"); } } else if (Rules.IsMemoryOperation(msg)) { MemorySave(msg, true); } else if (Rules.IsSinCos(msg)) { SinCos(msg, true); } else if (msg == "Mod") { Modulo(msg, true); } else if (msg == "log") { Logarithm(msg, true); } else if (msg == "x^y") { ParticularPower(msg, true); } else if (msg == "10^x") { PowerOfTen(msg, true); } else if (msg == "n!") { Factorial(msg, true); } else if (msg == "yrootx") { AnyRoot(msg, true); } else if (msg == ")") { reccuring.Add(secondNumber); reccuring.Add(")"); string text = string.Join(" ", reccuring.ToArray()); changeRecurringTextDelegate.Invoke(text); Expression(msg, true); } } }