Example #1
0
        private void ProcessOtherButtons(string button)
        {
            switch (button)
            {
            case "C":
                ClearDisplay();
                break;

            case "CE":
                CurrentInput = "0";
                break;

            case "±":
                if (!CurrentInput.Contains('-'))
                {
                    CurrentInput = "-" + CurrentInput;
                }
                else
                {
                    CurrentInput = CurrentInput.Substring(1);
                }
                break;

            case "<-":
                if (CurrentInput.Length == 0 || CurrentInput == "-" || CurrentInput == "Error" || CurrentInput == "NaN")
                {
                    CurrentInput = "0";
                }
                else
                {
                    CurrentInput = CurrentInput.Remove(CurrentInput.Length - 1);
                }
                break;

            case "x^2":
                NCalc.Expression powExp = new NCalc.Expression("Round(Pow(" + CurrentInput + ", 2), 3)");
                CurrentInput = powExp.Evaluate().ToString();
                break;

            case "√":
                NCalc.Expression sqrtExp = new NCalc.Expression("Round(Sqrt(" + CurrentInput + "), 3)");
                CurrentInput = sqrtExp.Evaluate().ToString();
                break;

            case "1/x":
                CurrentInput = new NCalc.Expression($"Round(1/{CurrentInput}, 3)").Evaluate().ToString();
                break;

            case "MS":
                calcmemory = CurrentInput;
                break;

            case "M+":
                calcmemory = new NCalc.Expression(calcmemory + "+" + CurrentInput).Evaluate().ToString();
                break;

            case "M-":
                calcmemory = new NCalc.Expression(calcmemory + "-" + CurrentInput).Evaluate().ToString();
                break;

            case "MR":
                if (!String.IsNullOrEmpty(calcmemory))
                {
                    CurrentInput = calcmemory;
                }
                break;

            case "MC":
                calcmemory = "0";
                break;

            case ".":
                if (!CurrentInput.Contains("."))
                {
                    CurrentInput += ".";
                }
                break;

            case "=":
                NCalc.Expression exp = new NCalc.Expression(TextExpression + CurrentInput);
                TextExpression = string.Empty;
                CurrentInput   = exp.Evaluate().ToString();
                break;

            default:
                if (binaryOperationPressed)
                {
                    // if previous operation is bin operation to clear old input values
                    CurrentInput           = button;
                    binaryOperationPressed = false;
                }
                else
                {
                    CurrentInput = CurrentInput.TrimStart('0') + button;                             // add new value to display and remove start 0
                }
                break;
            }
        }