fixed calculator

This commit is contained in:
Mikkel Troels Kongsted 2023-03-10 09:59:40 +01:00
parent 4795d86148
commit 32fee8d57e

View File

@ -5,6 +5,13 @@ namespace SimpleCalculator;
public partial class CalculatorMV : ObservableObject { public partial class CalculatorMV : ObservableObject {
private enum State
{
Number,
Action,
Calculate,
}
private enum Action { private enum Action {
None, None,
Add, Add,
@ -13,11 +20,11 @@ public partial class CalculatorMV : ObservableObject {
Divide, Divide,
} }
private double accumulator = 0; private State currentState = State.Number;
private (Action, double) currentAction = (Action.None, 0); private string accumulator = "0";
private (Action, string) currentAction = (Action.None, "0");
private bool isDecimal = false; private bool isDecimal = false;
private bool isNegative = false; private bool isNegative = false;
private bool isResult = false;
[ObservableProperty] [ObservableProperty]
@ -26,10 +33,9 @@ public partial class CalculatorMV : ObservableObject {
[RelayCommand] [RelayCommand]
private void numberClicked(string value) { private void numberClicked(string value) {
if (this.isResult) { if (this.currentState == State.Action) {
this.ResultText = "0"; this.ResultText = "0";
this.isResult = false;
this.accumulator = double.Parse(value);
} }
if (value == "0" && this.ResultText == "0") if (value == "0" && this.ResultText == "0")
return; return;
@ -37,13 +43,15 @@ public partial class CalculatorMV : ObservableObject {
this.ResultText = ""; this.ResultText = "";
else if (this.ResultText == "-0") else if (this.ResultText == "-0")
this.ResultText = "-"; this.ResultText = "-";
this.currentState = State.Number;
this.ResultText += value; this.ResultText += value;
} }
[RelayCommand] [RelayCommand]
private void acClicked() { private void acClicked() {
this.accumulator = 0; this.accumulator = "";
this.currentAction = (Action.None, 0); this.currentState = State.Number;
this.currentAction = (Action.None, "");
this.isDecimal = false; this.isDecimal = false;
this.isNegative = false; this.isNegative = false;
this.ResultText = "0"; this.ResultText = "0";
@ -57,46 +65,47 @@ public partial class CalculatorMV : ObservableObject {
} else { } else {
this.isNegative = true; this.isNegative = true;
this.ResultText = "-" + this.ResultText; this.ResultText = "-" + this.ResultText;
} }
} }
[RelayCommand] [RelayCommand]
private void percentileClicked() { private void percentileClicked() {
this.accumulator = double.Parse(this.ResultText); this.accumulator = this.ResultText;
this.accumulator *= 0.01; this.accumulator = (double.Parse(this.accumulator) * 0.01).ToString();
this.ResultText = this.accumulator.ToString(); this.ResultText = this.accumulator;
} }
[RelayCommand] [RelayCommand]
private void divideClicked() { private void divideClicked() {
var value = double.Parse(this.ResultText); var value = this.ResultText;
this.currentAction = (Action.Divide, value); this.currentAction = (Action.Divide, value);
this.accumulator = value; this.accumulator = "0";
this.isResult = true; this.currentState = State.Action;
} }
[RelayCommand] [RelayCommand]
private void multiplyClicked() { private void multiplyClicked() {
var value = double.Parse(this.ResultText); var value =this.ResultText;
this.currentAction = (Action.Multiply, value); this.currentAction = (Action.Multiply, value);
this.accumulator = value; this.accumulator = "0";
this.isResult = true; this.currentState = State.Action;
} }
[RelayCommand] [RelayCommand]
private void subtractClicked() { private void subtractClicked() {
var value = double.Parse(this.ResultText); var value = this.ResultText;
this.currentAction = (Action.Subtract, value); this.currentAction = (Action.Subtract, value);
this.accumulator = value; this.accumulator = "0";
this.isResult = true; this.currentState = State.Action;
} }
[RelayCommand] [RelayCommand]
private void addClicked() { private void addClicked() {
var value = double.Parse(this.ResultText); var value = this.ResultText;
this.currentAction = (Action.Add, value); this.currentAction = (Action.Add, value);
this.accumulator = value; this.accumulator = "0";
this.isResult = true; this.currentState = State.Action;
} }
[RelayCommand] [RelayCommand]
@ -106,20 +115,20 @@ public partial class CalculatorMV : ObservableObject {
case Action.None: case Action.None:
break; break;
case Action.Add: case Action.Add:
this.accumulator = value + this.accumulator; this.accumulator = (double.Parse(value) + double.Parse(this.ResultText)).ToString();
break; break;
case Action.Subtract: case Action.Subtract:
this.accumulator = value - this.accumulator; this.accumulator = (double.Parse(value) - double.Parse(this.ResultText)).ToString();
break; break;
case Action.Multiply: case Action.Multiply:
this.accumulator = value * this.accumulator; this.accumulator = (double.Parse(value) * double.Parse(this.ResultText)).ToString();
break; break;
case Action.Divide: case Action.Divide:
this.accumulator = value / this.accumulator; this.accumulator = (double.Parse(value) / double.Parse(this.ResultText)).ToString();
break; break;
} }
this.ResultText = this.accumulator.ToString(); this.ResultText = this.accumulator.ToString();
this.isResult = true; this.currentState = State.Calculate;
} }
[RelayCommand] [RelayCommand]