/// <summary> /// MainPageViewModel constructor. /// In this constructor, The view initialization and commands bindings are completed. </summary> public MainPageViewModel() { SetDisplayEmpty(); SetDegreeButton(true); if (Calculator.InputParserInstance.IsEmptyInputElements == false) { IEnumerable <InputElement> plainTexts = Calculator.InputParserInstance.ExpressionElements; FormattedString result; FormattedString expression; GetCalculatedResult(plainTexts, false, out expression, out result); ResultText = result; ExpressionText = expression; UpdateDisplay(); } this.PressButton = new Command((value) => { string input = value.ToString(); IEnumerable <InputElement> plainTexts; AddingElementResult res = Calculator.InputParserInstance.GetseparatedPlainText(input, out plainTexts); if ((res is AddingPossible) == false) { DisplayError(res.Message); return; } var commands = new Dictionary <string, string> { { "F", "Factorial [ x! ]" }, { "Q", "Square Root" }, { "S", "Sine [ sin(x) ]" }, { "C", "Cosine [ cos(x) ]" }, { "T", "Tangent [ tan(x) ]" }, { "N", "Natural Logarithm [ ln(x) ]" }, { "G", "Common Logarithm [ log(x) ]" }, { "O", "Multiplicative Inverse [ 1/x ]" }, { "V", "Square [ x^2 ]" }, { "P", "Exponent of 10 [ 10^x ]" }, { "W", "Exponent [ y^x ]" }, { "A", "Absolute Value [ |x| ]" }, { "+", "Addition" }, { "-", "Subtraction" }, { "*", "Multiplication" }, { "/", "Division" }, { "%", "Percentage" } }; string commandValue; try { if (commands.TryGetValue(input, out commandValue)) { Analytics.TrackEvent("Operation", new Dictionary <string, string> { { "operation", commandValue }, { "Mode", (Calculator.FormatterInstance.IsLandscapeOrientation ? "Scientific" : "Regular") } }); } } catch (System.Exception exc) { MobileCenterLog.Debug(MobileCenterLog.LogTag, $"{exc}"); } FormattedString result; FormattedString expression; GetCalculatedResult(plainTexts, false, out expression, out result); ExpressionText = expression; ResultText = result; UpdateDisplay(); }); this.Clear = new Command(() => { SetDisplayEmpty(); Calculator.InputParserInstance.Clear(); }); this.RemoveLast = new Command(() => { IEnumerable <InputElement> plainTexts; if (Calculator.InputParserInstance.DeleteLast(out plainTexts) == false) { return; } FormattedString result; FormattedString expression; GetCalculatedResult(plainTexts, false, out expression, out result); ExpressionText = expression; ResultText = result; UpdateDisplay(); }); this.Equal = new Command(() => { IEnumerable <InputElement> plainTexts; if (Calculator.InputParserInstance.Equal(out plainTexts) == false) { return; } FormattedString result; FormattedString expression; if (GetEqualResult(plainTexts, out expression, out result) == false) { return; } Calculator.InputParserInstance.Set(Calculator.CalculatorInstance.Result.ToString()); ExpressionText = ""; ResultText = result; UpdateDisplay(true); }); this.Reverse = new Command(() => { IEnumerable <InputElement> plainTexts; if (Calculator.InputParserInstance.ReverseSign(out plainTexts) == false) { DisplayError("Invalid format used."); return; } FormattedString result; FormattedString expression; GetCalculatedResult(plainTexts, false, out expression, out result); ExpressionText = expression; ResultText = result; UpdateDisplay(); }); this.ChangeDegreeMetric = new Command(() => { SetDegreeButton(); FormattedString result; FormattedString expression; GetCalculatedResult(Calculator.InputParserInstance.ExpressionElements, false, out expression, out result); ExpressionText = expression; ResultText = result; UpdateDisplay(); }); }
/// <summary> /// A method adds inputted element to exists expression and validate the expression with adding a inputted element. </summary> /// <param name="input"> A inputted element </param> /// <param name="separated"> A infix notation expression InputElement list </param> /// <returns> A element adding result status </returns> public AddingElementResult GetseparatedPlainText(string input, out IEnumerable <InputElement> separated) { double inputNumber; separated = expressionElements; if (input.Length < 1) { IsLastValidationSucceed = false; return(new InvalidFormatUsed()); } if (Double.TryParse(input, out inputNumber)) { Literal InputLiteral = new Literal(input); if (IsEmptyInputElements) { expressionElements.Add(InputLiteral); IsEqualUsed = false; IsLastValidationSucceed = true; return(new AddingPossible()); } else { AddingElementResult res = expressionElements.Last().CheckPossibilityAddingElement(ref expressionElements, ref IsEqualUsed, ref IsLastValidationSucceed, InputLiteral); IsEqualUsed = false; if (res is AddingPossible) { IsLastValidationSucceed = true; } else { IsLastValidationSucceed = false; } return(res); } } InputElement inputElem = Operators.GetOperatorAsInputElement(input); IOperator inputOper = inputElem as IOperator; if (inputOper == null) { IsLastValidationSucceed = false; return(new InvalidFormatUsed()); } if (IsEmptyInputElements) { if (OperandType.LEFT == inputOper.OperandType || OperandType.BOTH == inputOper.OperandType) { // Exceptional Case : '.' => '0.' if (inputOper is Point) { expressionElements.Add(new Literal("0")); } else { return(new InvalidFormatUsed()); } } expressionElements.Add(inputElem); Operators.GetOperatorAsInputElement(input)?.PostAddingWork(ref expressionElements); IsEqualUsed = false; IsLastValidationSucceed = true; return(new AddingPossible()); } if (inputElem.AlternativeWork(ref expressionElements, ref IsEqualUsed, ref IsLastValidationSucceed)) { IsEqualUsed = false; IsLastValidationSucceed = true; return(new AddingPossible()); } // Exceptional case : Only one '.' if (input.CompareTo(".") == 0) { if (CheckDotExist()) { separated = expressionElements; IsLastValidationSucceed = true; return(new AddingImpossible()); } } else if (input.CompareTo("R") == 0) { if (ReverseSign(out separated)) { return(new AddingPossible()); } return(new InvalidFormatUsed()); } expressionElements.Last().CheckPossibilityAddingElement(ref expressionElements, ref IsEqualUsed, ref IsLastValidationSucceed, inputOper as InputElement); IsEqualUsed = false; IsLastValidationSucceed = true; return(new AddingPossible()); }
/// <summary> /// MainPageViewModel constructor. /// In this constructor, The view initialization and commands bindings are completed. </summary> private MainPageViewModel() { SetDisplayEmpty(); SetDegreeButton(true); if (Calculator.InputParserInstance.IsEmptyInputElements == false) { IEnumerable <InputElement> plainTexts = Calculator.InputParserInstance.ExpressionElements; FormattedString result; FormattedString expression; GetCalculatedResult(plainTexts, false, out expression, out result); ResultText = result; ExpressionText = expression; UpdateDisplay(); } this.PressButton = new Command((value) => { string input = value.ToString(); IEnumerable <InputElement> plainTexts; AddingElementResult res = Calculator.InputParserInstance.GetseparatedPlainText(input, out plainTexts); if ((res is AddingPossible) == false) { DisplayError(res.Message); return; } FormattedString result; FormattedString expression; GetCalculatedResult(plainTexts, false, out expression, out result); ExpressionText = expression; ResultText = result; UpdateDisplay(); }); this.Clear = new Command(() => { SetDisplayEmpty(); Calculator.InputParserInstance.Clear(); }); this.RemoveLast = new Command(() => { IEnumerable <InputElement> plainTexts; if (Calculator.InputParserInstance.DeleteLast(out plainTexts) == false) { return; } FormattedString result; FormattedString expression; GetCalculatedResult(plainTexts, false, out expression, out result); ExpressionText = expression; ResultText = result; UpdateDisplay(); }); this.Equal = new Command(() => { IEnumerable <InputElement> plainTexts; if (Calculator.InputParserInstance.Equal(out plainTexts) == false) { return; } FormattedString result; FormattedString expression; if (GetEqualResult(plainTexts, out expression, out result) == false) { return; } Calculator.InputParserInstance.Set(Calculator.CalculatorInstance.Result.ToString()); ExpressionText = string.Empty; ResultText = result; UpdateDisplay(true); }); this.Reverse = new Command(() => { IEnumerable <InputElement> plainTexts; if (Calculator.InputParserInstance.ReverseSign(out plainTexts) == false) { DisplayError("Invalid format used."); return; } FormattedString result; FormattedString expression; GetCalculatedResult(plainTexts, false, out expression, out result); ExpressionText = expression; ResultText = result; UpdateDisplay(); }); this.ChangeDegreeMetric = new Command(() => { SetDegreeButton(); FormattedString result; FormattedString expression; GetCalculatedResult(Calculator.InputParserInstance.ExpressionElements, false, out expression, out result); ExpressionText = expression; ResultText = result; UpdateDisplay(); }); }