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