132 lines
3.5 KiB
C#
132 lines
3.5 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
|
namespace SimpleCalculator;
|
|
|
|
public partial class CalculatorMV : ObservableObject {
|
|
|
|
private enum Action {
|
|
None,
|
|
Add,
|
|
Subtract,
|
|
Multiply,
|
|
Divide,
|
|
}
|
|
|
|
private double accumulator = 0;
|
|
private (Action, double) currentAction = (Action.None, 0);
|
|
private bool isDecimal = false;
|
|
private bool isNegative = false;
|
|
private bool isResult = false;
|
|
|
|
|
|
[ObservableProperty]
|
|
private string resultText = "0";
|
|
|
|
|
|
[RelayCommand]
|
|
private void numberClicked(string value) {
|
|
if (this.isResult) {
|
|
this.ResultText = "0";
|
|
this.isResult = false;
|
|
this.accumulator = double.Parse(value);
|
|
}
|
|
if (value == "0" && this.ResultText == "0")
|
|
return;
|
|
if (this.ResultText == "0")
|
|
this.ResultText = "";
|
|
else if (this.ResultText == "-0")
|
|
this.ResultText = "-";
|
|
this.ResultText += value;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void acClicked() {
|
|
this.accumulator = 0;
|
|
this.currentAction = (Action.None, 0);
|
|
this.isDecimal = false;
|
|
this.isNegative = false;
|
|
this.ResultText = "0";
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void invertClicked() {
|
|
if (this.isNegative) {
|
|
this.isNegative = false;
|
|
this.ResultText = this.ResultText.Substring(1);
|
|
} else {
|
|
this.isNegative = true;
|
|
this.ResultText = "-" + this.ResultText;
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void percentileClicked() {
|
|
this.accumulator = double.Parse(this.ResultText);
|
|
this.accumulator *= 0.01;
|
|
this.ResultText = this.accumulator.ToString();
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void divideClicked() {
|
|
var value = double.Parse(this.ResultText);
|
|
this.currentAction = (Action.Divide, value);
|
|
this.accumulator = value;
|
|
this.isResult = true;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void multiplyClicked() {
|
|
var value = double.Parse(this.ResultText);
|
|
this.currentAction = (Action.Multiply, value);
|
|
this.accumulator = value;
|
|
this.isResult = true;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void subtractClicked() {
|
|
var value = double.Parse(this.ResultText);
|
|
this.currentAction = (Action.Subtract, value);
|
|
this.accumulator = value;
|
|
this.isResult = true;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void addClicked() {
|
|
var value = double.Parse(this.ResultText);
|
|
this.currentAction = (Action.Add, value);
|
|
this.accumulator = value;
|
|
this.isResult = true;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void equalClicked() {
|
|
var (action, value) = this.currentAction;
|
|
switch (action) {
|
|
case Action.None:
|
|
break;
|
|
case Action.Add:
|
|
this.accumulator = value + this.accumulator;
|
|
break;
|
|
case Action.Subtract:
|
|
this.accumulator = value - this.accumulator;
|
|
break;
|
|
case Action.Multiply:
|
|
this.accumulator = value * this.accumulator;
|
|
break;
|
|
case Action.Divide:
|
|
this.accumulator = value / this.accumulator;
|
|
break;
|
|
}
|
|
this.ResultText = this.accumulator.ToString();
|
|
this.isResult = true;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void decimalPointClicked() {
|
|
this.isDecimal = true;
|
|
this.ResultText += ",";
|
|
}
|
|
|
|
}
|