public AdderViewModel() { ClearCommand = new Command( execute: () => { HistoryString = ""; accumulatedSum = 0; CurrentEntry = "0"; isSumDisplayed = false; RefreshCanExecutes(); }); ClearEntryCommand = new Command( execute: () => { CurrentEntry = "0"; isSumDisplayed = false; RefreshCanExecutes(); }); BackspaceCommand = new Command( execute: () => { CurrentEntry = CurrentEntry.Substring(0, CurrentEntry.Length - 1); if (CurrentEntry.Length == 0) { CurrentEntry = "0"; } RefreshCanExecutes(); }, canExecute: () => { return(!isSumDisplayed && (CurrentEntry.Length > 1 || CurrentEntry[0] != '0')); }); NumericCommand = new Command <string>( execute: (string parameter) => { if (isSumDisplayed || CurrentEntry == "0") { CurrentEntry = parameter; } else { CurrentEntry += parameter; } isSumDisplayed = false; RefreshCanExecutes(); }, canExecute: (string parameter) => { return(isSumDisplayed || CurrentEntry.Length < 16); }); DecimalPointCommand = new Command( execute: () => { if (isSumDisplayed) { CurrentEntry = "0."; } else { CurrentEntry += "."; } isSumDisplayed = false; RefreshCanExecutes(); }, canExecute: () => { return(isSumDisplayed || !CurrentEntry.Contains(".")); }); AddCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " + "; accumulatedSum += value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); }, canExecute: () => { return(!isSumDisplayed); }); }
int op = -1; //1+,2-,3*,4/ public CalcViewModel() { ClearCommand = new Command( execute: () => { HistoryString = ""; accumulatedSum = 0; CurrentEntry = "0"; isSumDisplayed = false; RefreshCanExecutes(); }); ClearEntryCommand = new Command( execute: () => { CurrentEntry = "0"; isSumDisplayed = false; RefreshCanExecutes(); }); BackspaceCommand = new Command( execute: () => { CurrentEntry = CurrentEntry.Substring(0, CurrentEntry.Length - 1); if (CurrentEntry.Length == 0) { CurrentEntry = "0"; } RefreshCanExecutes(); }, canExecute: () => { return(!isSumDisplayed && (CurrentEntry.Length > 1 || CurrentEntry[0] != '0')); }); NumericCommand = new Command <string>( execute: (string parameter) => { if (isSumDisplayed || CurrentEntry == "0") { CurrentEntry = parameter; } else { CurrentEntry += parameter; } isSumDisplayed = false; RefreshCanExecutes(); }, canExecute: (string parameter) => { return(isSumDisplayed || CurrentEntry.Length < 16); }); DecimalPointCommand = new Command( execute: () => { if (isSumDisplayed) { CurrentEntry = "0."; } else { CurrentEntry += "."; } isSumDisplayed = false; RefreshCanExecutes(); }, canExecute: () => { return(isSumDisplayed || !CurrentEntry.Contains(".")); }); AddCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); accumulatedSum += value; CurrentEntry = "0"; op = 1; RefreshCanExecutes(); }, canExecute: () => { return(true); }); SubCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); if (accumulatedSum != 0) { accumulatedSum -= value; } else { accumulatedSum = value; } CurrentEntry = "0"; op = 2; RefreshCanExecutes(); }, canExecute: () => { return(true); }); MulCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); if (accumulatedSum != 0) { accumulatedSum *= value; } else { accumulatedSum = value; } CurrentEntry = "0"; op = 3; RefreshCanExecutes(); }, canExecute: () => { return(true); }); DivCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); if (accumulatedSum == 0) { accumulatedSum = value; } else { if (value == 0) { accumulatedSum = 0; } else { accumulatedSum /= value; } } CurrentEntry = "0"; op = 4; RefreshCanExecutes(); }, canExecute: () => { return(true); }); SumCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); if (op == 1) { accumulatedSum += value; } else if (op == 2) { accumulatedSum += (-value); } else if (op == 3) { accumulatedSum *= value; } else if (op == 4) { if (value == 0) { accumulatedSum = 0; } else { accumulatedSum /= value; } } CurrentEntry = accumulatedSum.ToString(); accumulatedSum = 0; op = -1; isSumDisplayed = true; RefreshCanExecutes(); }, canExecute: () => { return(!isSumDisplayed); }); FactCommand = new Command( execute: () => { int N = Int32.Parse(CurrentEntry); int fact = 1; for (int i = 1; i <= N; i++) { fact *= i; } CurrentEntry = fact.ToString(); RefreshCanExecutes(); }, canExecute: () => { return(!CurrentEntry.Contains(".") && Double.Parse(CurrentEntry) >= 0); }); PlusMinusCommand = new Command( execute: () => { double v = -Double.Parse(CurrentEntry); CurrentEntry = v.ToString(); RefreshCanExecutes(); }); SqrCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); CurrentEntry = (value * value).ToString(); RefreshCanExecutes(); }, canExecute: () => { return(true); }); OneOverCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); CurrentEntry = (1 / value).ToString(); RefreshCanExecutes(); }, canExecute: () => { return(Double.Parse(CurrentEntry) != 0); }); SqrtCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); CurrentEntry = (Math.Sqrt(value)).ToString(); RefreshCanExecutes(); }, canExecute: () => { return(Double.Parse(CurrentEntry) >= 0); }); cubertCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); CurrentEntry = (Math.Pow(value, (double)1 / 3)).ToString(); RefreshCanExecutes(); }, canExecute: () => { return(Double.Parse(CurrentEntry) >= 0); }); lnCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); CurrentEntry = Math.Log(value).ToString(); RefreshCanExecutes(); }, canExecute: () => { return(Double.Parse(CurrentEntry) != 0); }); log10Command = new Command( execute: () => { double value = Double.Parse(CurrentEntry); CurrentEntry = Math.Log10(value).ToString(); RefreshCanExecutes(); }, canExecute: () => { return(Double.Parse(CurrentEntry) != 0); }); exCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); CurrentEntry = Math.Exp(value).ToString(); RefreshCanExecutes(); }, canExecute: () => { return(true); }); sinCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); CurrentEntry = (Math.Sin(value)).ToString(); RefreshCanExecutes(); }, canExecute: () => { return(true); }); cosCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); CurrentEntry = (Math.Cos(value)).ToString(); RefreshCanExecutes(); }, canExecute: () => { return(true); }); tanCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); CurrentEntry = (Math.Tan(value)).ToString(); RefreshCanExecutes(); }, canExecute: () => { return(true); }); randCommand = new Command( execute: () => { Random rand = new System.Random(); CurrentEntry = rand.NextDouble().ToString(); RefreshCanExecutes(); }, canExecute: () => { return(Double.Parse(CurrentEntry) == 0); }); secondCommand = new Command( execute: () => { if (Second == "10ˣ") { Second = "2ˣ"; SecondLabel = "#212121"; } else { Second = "10ˣ"; SecondLabel = "#FF0000"; } }); tenxCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); if (Second == "10ˣ") { CurrentEntry = (Math.Pow(10, value)).ToString(); } else { CurrentEntry = (Math.Pow(2, value)).ToString(); } }); }
public CalcViewModel() { displayLabel = new Label { Text = "" }; ClearCommand = new Command( execute: () => { CurrentEntry = ""; count = 0; negativevalueassigned = 0; RefreshCanExecutes(); }); EqualsCommand = new Command( execute: () => { if (negativevalueassigned > 2) { CurrentEntry = "not valid"; } else { compute(count); } RefreshCanExecutes(); }); BackspaceCommand = new Command( execute: () => { CurrentEntry = CurrentEntry.Substring(0, CurrentEntry.Length - 1); if (CurrentEntry.Length == 0) { CurrentEntry = "0"; } RefreshCanExecutes(); }, canExecute: () => { return(isAnswerDisplayed && (CurrentEntry.Length > 1 || CurrentEntry[0] != '0')); }); NumericCommand = new Command <string>( execute: (string parameter) => { if (isAnswerDisplayed || CurrentEntry == "0") { CurrentEntry = parameter; } else { CurrentEntry += parameter; } isAnswerDisplayed = false; RefreshCanExecutes(); }, canExecute: (string parameter) => { return(isAnswerDisplayed || CurrentEntry.Length < 16); }); DecimalPointCommand = new Command( execute: () => { if (isAnswerDisplayed) { CurrentEntry = "0."; } else { CurrentEntry += "."; } isAnswerDisplayed = false; RefreshCanExecutes(); }, canExecute: () => { return(isAnswerDisplayed || !CurrentEntry.Contains(".")); }); AddCommand = new Command( execute: () => { if (negativevalueassigned > 1) { CurrentEntry = "not valid"; } else if (CurrentEntry != "") { num1 = float.Parse(CurrentEntry); CurrentEntry = ""; count = 2; } RefreshCanExecutes(); }, canExecute: () => { return(!isAnswerDisplayed); }); SubtractCommand = new Command( execute: () => { if (negativevalueassigned > 1) { CurrentEntry = "not valid"; } else if (CurrentEntry != "") { num1 = float.Parse(CurrentEntry); CurrentEntry = ""; count = 1; } RefreshCanExecutes(); }, canExecute: () => { return(!isAnswerDisplayed); }); DivideCommand = new Command( execute: () => { if (negativevalueassigned > 1) { CurrentEntry = "not valid"; } else if (CurrentEntry != "") { num1 = float.Parse(CurrentEntry); CurrentEntry = ""; count = 4; } RefreshCanExecutes(); }, canExecute: () => { return(!isAnswerDisplayed); }); MultiplyCommand = new Command( execute: () => { if (negativevalueassigned > 1) { CurrentEntry = "not valid"; } else if (CurrentEntry != "") { num1 = float.Parse(CurrentEntry); CurrentEntry = ""; count = 3; } RefreshCanExecutes(); }, canExecute: () => { return(!isAnswerDisplayed); }); ZeroCommand = new Command( execute: () => { CurrentEntry = currentEntry + 0; }, canExecute: () => { return(!isAnswerDisplayed); }); OneCommand = new Command( execute: () => { CurrentEntry = currentEntry + 1; }, canExecute: () => { return(!isAnswerDisplayed); }); TwoCommand = new Command( execute: () => { CurrentEntry = currentEntry + 2; }, canExecute: () => { return(!isAnswerDisplayed); }); ThreeCommand = new Command( execute: () => { CurrentEntry = currentEntry + 3; }, canExecute: () => { return(!isAnswerDisplayed); }); FourCommand = new Command( execute: () => { CurrentEntry = currentEntry + 4; }, canExecute: () => { return(!isAnswerDisplayed); }); FiveCommand = new Command( execute: () => { CurrentEntry = currentEntry + 5; }, canExecute: () => { return(!isAnswerDisplayed); }); SixCommand = new Command( execute: () => { CurrentEntry = currentEntry + 6; }, canExecute: () => { return(!isAnswerDisplayed); }); SevenCommand = new Command( execute: () => { CurrentEntry = currentEntry + 7; }, canExecute: () => { return(!isAnswerDisplayed); }); EightCommand = new Command( execute: () => { CurrentEntry = currentEntry + 8; }, canExecute: () => { return(!isAnswerDisplayed); }); NineCommand = new Command( execute: () => { CurrentEntry = currentEntry + 9; }, canExecute: () => { return(!isAnswerDisplayed); }); }
public AdderViewModel() { ClearCommand = new Command( execute: () => { HistoryString = ""; accumulatedSum = 0; CurrentEntry = "0"; isSumDisplayed = false; RefreshCanExecutes(); }); ClearEntryCommand = new Command( execute: () => { CurrentEntry = "0"; isSumDisplayed = false; RefreshCanExecutes(); }); BackspaceCommand = new Command( execute: () => { CurrentEntry = CurrentEntry.Substring(0, CurrentEntry.Length - 1); if (CurrentEntry.Length == 0) { CurrentEntry = "0"; } RefreshCanExecutes(); }, canExecute: () => { return(!isSumDisplayed && (CurrentEntry.Length > 1 || CurrentEntry[0] != '0')); }); NumericCommand = new Command <string>( execute: (string parameter) => { if (isSumDisplayed || CurrentEntry == "0") { CurrentEntry = parameter; } else { CurrentEntry += parameter; } isSumDisplayed = false; RefreshCanExecutes(); }, canExecute: (string parameter) => { return(isSumDisplayed || CurrentEntry.Length < 16); }); DecimalPointCommand = new Command( execute: () => { if (isSumDisplayed) { CurrentEntry = "0."; } else { CurrentEntry += "."; } isSumDisplayed = false; RefreshCanExecutes(); }, canExecute: () => { return(isSumDisplayed || !CurrentEntry.Contains(".")); }); AddAndExecuteCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); if (lastCommand == "") { CurrentEntry = value.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } else { switch (lastCommand) { case "+": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " + "; accumulatedSum += value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; case "-": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " + "; accumulatedSum -= value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; case "*": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " + "; accumulatedSum *= value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; case "/": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " + "; accumulatedSum /= value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; default: break; } } lastCommand = "+"; }, canExecute: () => { return(!isSumDisplayed); }); SubAndExecuteCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); if (lastCommand == "") { CurrentEntry = value.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } else { switch (lastCommand) { case "+": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " - "; accumulatedSum += value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; case "-": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " - "; accumulatedSum -= value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; case "*": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " - "; accumulatedSum *= value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; case "/": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " - "; accumulatedSum /= value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; default: break; } } lastCommand = "-"; }, canExecute: () => { return(!isSumDisplayed); }); MultiAndExecuteCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); if (lastCommand == "") { CurrentEntry = value.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } else { switch (lastCommand) { case "+": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " * "; accumulatedSum += value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; case "-": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " * "; accumulatedSum -= value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; case "*": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " * "; accumulatedSum *= value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; case "/": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " * "; accumulatedSum /= value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; default: break; } } lastCommand = "*"; }, canExecute: () => { return(!isSumDisplayed); }); DivideAndExecuteCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); if (lastCommand == "") { CurrentEntry = value.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } else { switch (lastCommand) { case "+": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " / "; accumulatedSum += value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; case "-": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " / "; accumulatedSum -= value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; case "*": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " / "; accumulatedSum *= value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; case "/": { value = Double.Parse(CurrentEntry); HistoryString += value.ToString() + " / "; accumulatedSum /= value; CurrentEntry = accumulatedSum.ToString(); isSumDisplayed = true; RefreshCanExecutes(); } break; default: break; } } lastCommand = "/"; }, canExecute: () => { return(!isSumDisplayed); }); }