wpf-calculator/SimpleCalculator/CalculatorMV.cs

141 lines
3.8 KiB
C#
Raw Normal View History

2023-03-09 13:35:50 +00:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace SimpleCalculator;
public partial class CalculatorMV : ObservableObject {
2023-03-10 08:59:40 +00:00
private enum State
{
Number,
Action,
Calculate,
}
2023-03-09 13:35:50 +00:00
private enum Action {
None,
Add,
Subtract,
Multiply,
Divide,
}
2023-03-10 08:59:40 +00:00
private State currentState = State.Number;
private string accumulator = "0";
private (Action, string) currentAction = (Action.None, "0");
2023-03-09 13:35:50 +00:00
private bool isDecimal = false;
private bool isNegative = false;
[ObservableProperty]
private string resultText = "0";
[RelayCommand]
private void numberClicked(string value) {
2023-03-10 08:59:40 +00:00
if (this.currentState == State.Action) {
2023-03-09 13:35:50 +00:00
this.ResultText = "0";
2023-03-10 08:59:40 +00:00
2023-03-09 13:35:50 +00:00
}
if (value == "0" && this.ResultText == "0")
return;
if (this.ResultText == "0")
this.ResultText = "";
else if (this.ResultText == "-0")
this.ResultText = "-";
2023-03-10 08:59:40 +00:00
this.currentState = State.Number;
2023-03-09 13:35:50 +00:00
this.ResultText += value;
}
[RelayCommand]
private void acClicked() {
2023-03-10 08:59:40 +00:00
this.accumulator = "";
this.currentState = State.Number;
this.currentAction = (Action.None, "");
2023-03-09 13:35:50 +00:00
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;
2023-03-10 08:59:40 +00:00
2023-03-09 13:35:50 +00:00
}
}
[RelayCommand]
private void percentileClicked() {
2023-03-10 08:59:40 +00:00
this.accumulator = this.ResultText;
this.accumulator = (double.Parse(this.accumulator) * 0.01).ToString();
this.ResultText = this.accumulator;
2023-03-09 13:35:50 +00:00
}
[RelayCommand]
private void divideClicked() {
2023-03-10 08:59:40 +00:00
var value = this.ResultText;
2023-03-09 13:35:50 +00:00
this.currentAction = (Action.Divide, value);
2023-03-10 08:59:40 +00:00
this.accumulator = "0";
this.currentState = State.Action;
2023-03-09 13:35:50 +00:00
}
[RelayCommand]
private void multiplyClicked() {
2023-03-10 08:59:40 +00:00
var value =this.ResultText;
2023-03-09 13:35:50 +00:00
this.currentAction = (Action.Multiply, value);
2023-03-10 08:59:40 +00:00
this.accumulator = "0";
this.currentState = State.Action;
2023-03-09 13:35:50 +00:00
}
[RelayCommand]
private void subtractClicked() {
2023-03-10 08:59:40 +00:00
var value = this.ResultText;
2023-03-09 13:35:50 +00:00
this.currentAction = (Action.Subtract, value);
2023-03-10 08:59:40 +00:00
this.accumulator = "0";
this.currentState = State.Action;
2023-03-09 13:35:50 +00:00
}
[RelayCommand]
private void addClicked() {
2023-03-10 08:59:40 +00:00
var value = this.ResultText;
2023-03-09 13:35:50 +00:00
this.currentAction = (Action.Add, value);
2023-03-10 08:59:40 +00:00
this.accumulator = "0";
this.currentState = State.Action;
2023-03-09 13:35:50 +00:00
}
[RelayCommand]
private void equalClicked() {
var (action, value) = this.currentAction;
switch (action) {
case Action.None:
break;
case Action.Add:
2023-03-10 08:59:40 +00:00
this.accumulator = (double.Parse(value) + double.Parse(this.ResultText)).ToString();
2023-03-09 13:35:50 +00:00
break;
case Action.Subtract:
2023-03-10 08:59:40 +00:00
this.accumulator = (double.Parse(value) - double.Parse(this.ResultText)).ToString();
2023-03-09 13:35:50 +00:00
break;
2023-03-10 08:59:40 +00:00
case Action.Multiply:
this.accumulator = (double.Parse(value) * double.Parse(this.ResultText)).ToString();
2023-03-09 13:35:50 +00:00
break;
case Action.Divide:
2023-03-10 08:59:40 +00:00
this.accumulator = (double.Parse(value) / double.Parse(this.ResultText)).ToString();
2023-03-09 13:35:50 +00:00
break;
}
this.ResultText = this.accumulator.ToString();
2023-03-10 08:59:40 +00:00
this.currentState = State.Calculate;
2023-03-09 13:35:50 +00:00
}
[RelayCommand]
private void decimalPointClicked() {
this.isDecimal = true;
this.ResultText += ",";
}
}