public CalculatorViewModel() { ClearCommand = new Command( execute: () => { accumulatedNum = 0; CurrentEntry = "0"; isNumDisplayed = false; RefreshCanExecutes(); }); NumericCommand = new Command <string>( execute: (string parameter) => { if (isNumDisplayed || CurrentEntry == "0") { CurrentEntry = parameter; } else { CurrentEntry += parameter; } isNumDisplayed = false; RefreshCanExecutes(); }, canExecute: (string parameter) => { if (parameter == "0" && operatorPressed == "/") { return(!isNumDisplayed); } return(isNumDisplayed || CurrentEntry.Length < 16); }); DecimalPointCommand = new Command( execute: () => { if (isNumDisplayed) { CurrentEntry = "0."; } else { CurrentEntry += "."; } isNumDisplayed = false; RefreshCanExecutes(); }, canExecute: () => { return(isNumDisplayed || !CurrentEntry.Contains(".")); }); OperatorCommand = new Command <string>( execute: (string parameter) => { double value = Double.Parse(CurrentEntry); accumulatedNum = value; isNumDisplayed = true; operatorPressed = parameter; RefreshCanExecutes(); }, canExecute: (string parameter) => { return(!isNumDisplayed); }); FactorialCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); if (value == 0) { CurrentEntry = "1"; } else { double tmp = value; for (int i = 1; i < value; i++) { tmp *= i; } CurrentEntry = tmp.ToString(); } isNumDisplayed = false; operatorPressed = ""; RefreshCanExecutes(); }, canExecute: () => { double checkValue = Double.Parse(CurrentEntry); return(!isNumDisplayed && (checkValue % 1 == 0) && (checkValue >= 0)); }); SquareCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); double square = value * value; CurrentEntry = square.ToString(); isNumDisplayed = false; operatorPressed = ""; RefreshCanExecutes(); }, canExecute: () => { return(!isNumDisplayed); }); OneOverCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); double oneOver = 1 / value; CurrentEntry = oneOver.ToString(); isNumDisplayed = false; operatorPressed = ""; RefreshCanExecutes(); }, canExecute: () => { return(!isNumDisplayed && (currentEntry != "0")); }); RootCommand = new Command <string>( execute: (string parameter) => { double value = Double.Parse(CurrentEntry); double root = Double.Parse(parameter); double res = Math.Pow(value, 1.0 / root); CurrentEntry = res.ToString(); isNumDisplayed = false; operatorPressed = ""; RefreshCanExecutes(); }, canExecute: (string parameter) => { if (parameter == "2") { double tmp = Double.Parse(currentEntry); return(!isNumDisplayed && tmp >= 0); } return(!isNumDisplayed); }); lnCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); double res = Math.Log(value); CurrentEntry = res.ToString(); isNumDisplayed = false; operatorPressed = ""; RefreshCanExecutes(); }, canExecute: () => { double tmp = Double.Parse(currentEntry); return(!isNumDisplayed && tmp > 0); }); LogCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); double res = Math.Log10(value); CurrentEntry = res.ToString(); isNumDisplayed = false; operatorPressed = ""; RefreshCanExecutes(); }, canExecute: () => { double tmp = Double.Parse(currentEntry); return(!isNumDisplayed && tmp > 0); }); EtoXCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); double res = Math.Pow(Math.E, value); CurrentEntry = res.ToString(); isNumDisplayed = false; operatorPressed = ""; RefreshCanExecutes(); }, canExecute: () => { return(!isNumDisplayed); }); TrigCommand = new Command <string>( execute: (string parameter) => { double value = Double.Parse(CurrentEntry); double res = 0; if (parameter == "sin") { res = Math.Sin(value); } if (parameter == "cos") { res = Math.Cos(value); } if (parameter == "tan") { res = Math.Tan(value); } CurrentEntry = res.ToString(); isNumDisplayed = false; operatorPressed = ""; RefreshCanExecutes(); }, canExecute: (string parameter) => { return(!isNumDisplayed); }); RandCommand = new Command <string>( execute: (string parameter) => { double value = Double.Parse(CurrentEntry); Random random = new Random(); double res = random.NextDouble(); CurrentEntry = res.ToString(); isNumDisplayed = false; operatorPressed = ""; RefreshCanExecutes(); }, canExecute: (string parameter) => { return(!isNumDisplayed); }); EqualsCommand = new Command( execute: () => { double value = Double.Parse(CurrentEntry); if (operatorPressed == "+") { accumulatedNum += value; } if (operatorPressed == "-") { accumulatedNum -= value; } if (operatorPressed == "*") { accumulatedNum *= value; } if (operatorPressed == "/") { accumulatedNum /= value; } CurrentEntry = accumulatedNum.ToString(); isNumDisplayed = false; operatorPressed = ""; RefreshCanExecutes(); }, canExecute: () => { return(!isNumDisplayed); }); PlusMinusCommand = new Command( execute: () => { double v = -Double.Parse(CurrentEntry); CurrentEntry = v.ToString(); RefreshCanExecutes(); }); SecondCommand = new Command( execute: () => { CurrentState = "2^x"; RefreshCanExecutes(); }); XtoXCommand = new Command <Button>( execute: (Button parameter) => { double value = Double.Parse(CurrentEntry); double res = 1; if (parameter.Text == "2^x") { res = Math.Pow(2, value); } if (parameter.Text == "10^x") { res = Math.Pow(10, value); } CurrentEntry = res.ToString(); isNumDisplayed = false; operatorPressed = ""; RefreshCanExecutes(); }, canExecute: (Button parameter) => { return(!isNumDisplayed); }); }
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); }); }