/// <summary> /// Initializes a new instance of the <see cref="CalculatorControl"/> class. /// </summary> public CalculatorControl() { this.InitializeComponent(); this.InputCommand = new RelayCommand(this.OnInputCommandRequested); this.CopyCommand = new RelayCommand(this.OnCopyResultRequested); this.lastButton = new CalculatorButton() { FunctionType = FunctionType.Clear }; }
/// <summary> /// Finds a calculator button of a given item in the visual tree. /// </summary> /// <param name="parent">The dependency object parent.</param> /// <param name="content">The buttons' content.</param> /// <returns>Null if nothing found.</returns> public static CalculatorButton FindButton(DependencyObject parent, string content) { // Confirm parent and childName are valid. if (parent == null) { return(null); } CalculatorButton foundChild = null; int childrenCount = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < childrenCount; i++) { var child = VisualTreeHelper.GetChild(parent, i); // If the child is not of the request child type child CalculatorButton childType = child as CalculatorButton; if (childType == null) { // Recursively drill down the tree foundChild = FindButton(child, content); // If the child is found, break so we do not overwrite the found child. if (foundChild != null) { break; } } else if (!string.IsNullOrEmpty(content)) { var button = child as CalculatorButton; // If the button text is set for search if (button != null && button.Text == content) { // If the child's name is of the request name foundChild = (CalculatorButton)child; break; } } else { // Child element found. foundChild = (CalculatorButton)child; break; } } return(foundChild); }
private static void buttonValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CalculatorButton CB = (CalculatorButton)d; CB.Text.Content = CB.buttonValue; }
/// <summary> /// Performs the needed operations after the user input. /// </summary> /// <param name="button">The button, which has been pressed.</param> private void ProcessUserInput(CalculatorButton button) { if (button == null) { throw new ArgumentNullException("button"); } if (button.FunctionType == FunctionType.Clear) { this.Result = "0"; this.Expression = string.Empty; this.lastButton = button; return; } else if (button.FunctionType == FunctionType.ClearEntry) { this.Result = "0"; } if (button.FunctionType == FunctionType.LeftBracket) { if (this.lastButton.FunctionType == FunctionType.LeftBracket || this.lastButton.IsOperatorButton || this.lastButton.FunctionType == FunctionType.Clear) { this.Expression += button.Text; this.Result = "0"; this.lastButton = button; } return; } else if (button.FunctionType == FunctionType.RightBracket) { int leftBracketsCount = 0; int rightBracketsCount = 0; foreach (var ch in this.Expression) { if (ch == '(') { leftBracketsCount++; } else if (ch == ')') { rightBracketsCount++; } } if (leftBracketsCount - rightBracketsCount < 1) { this.lastButton = button; return; } this.Expression += this.Result + button.Text; this.Result = string.Empty; this.lastButton = button; return; } // Checking if the result is not a text (i.e we have an error). double resultNumber; bool isParseOK = double.TryParse( this.Result, NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent | NumberStyles.AllowLeadingSign, CultureInfo.CurrentCulture, out resultNumber); if (this.Result.Length != 0 && (!isParseOK || double.IsInfinity(resultNumber) || double.IsNaN(resultNumber))) { // The user can only use the 'C' (clear all) button. return; } if (button.IsDigitButton) { if (this.Result == "0") { this.Result = string.Empty; } if (this.lastButton.IsOperatorButton) { this.Result = button.Text; } else { this.Result += button.Text; } this.lastButton = button; return; } if (button.IsOperatorButton) { if (this.lastButton.IsOperatorButton) { // Changing the last operator var oldExpression = this.Expression; var index = oldExpression.LastIndexOf(" " + this.lastButton.Text[0] + " "); var newExpression = oldExpression.Remove(index, 3); newExpression += " " + button.Text[0] + " "; this.Expression = newExpression; } else if (this.lastButton.IsBracket) { this.Expression += " " + button.Text + " "; } else { this.Expression += this.Result + " " + button.Text + " "; } this.lastButton = button; return; } if (button.FunctionType == FunctionType.Equals) { this.Expression += this.Result; try { this.Result = CalculatorCore.Operations.Compute(this.Expression).ToString(); } catch (Exception ex) { this.Result = ex.Message; } this.Expression = string.Empty; } else if (button.FunctionType == FunctionType.Back) { if (this.Result.Length > 0 && this.lastButton.IsErasableButton) { this.Result = this.Result.Substring(0, this.Result.Length - 1); if (this.Result.Length == 0) { this.Result = "0"; } } } else if (button.FunctionType == FunctionType.Comma) { if (!this.lastButton.IsErasableButton) { this.Result = "0" + button.Text; this.lastButton = button; return; } if (!this.Result.Contains(button.Text)) { this.Result += button.Text; } } else if (button.IsCustomFunction) { string fname = button.FunctionType.ToString().ToLower(); if (this.Result == string.Empty) { this.Expression = string.Format("{0}({1})", fname, this.Expression); } else { this.Expression += string.Format("{0}({1})", fname, this.Result); } this.Result = string.Empty; } else if (button.FunctionType == FunctionType.Pi) { this.Result = Math.PI.ToString(); } else if (button.FunctionType == FunctionType.E) { this.Result = Math.E.ToString(); } this.lastButton = button; }