-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.xaml.cs
160 lines (128 loc) · 4.53 KB
/
MainPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System.Linq;
using Microsoft.Maui.Controls;
namespace NumiX
{
public partial class MainPage : ContentPage
{
//int count = 0;
private double? num1 = null;
private double? num2 = null;
private double? dangling = null;
private string? currentOperator = null;
private Boolean? operatorClicked = false;
private int? operatorCount = null;
private string? danglingText = null;
public MainPage()
{
InitializeComponent();
}
private void OnCounterClicked(object sender, EventArgs e)
{
QueryLabel.Text = "0";
}
private void OnNegateClicked(object sender, EventArgs e)
{
if (double.TryParse(QueryLabel.Text, out double result))
{
result = -result;
QueryLabel.Text = result.ToString();
}
}
private void OnPercentClicked(object sender, EventArgs e)
{
if (double.TryParse(QueryLabel.Text, out double result))
{
result = result / 100;
QueryLabel.Text = result.ToString();
}
}
private void OnOperatorClicked(object sender, EventArgs e)
{
if (sender is Button button)
{
currentOperator = button.Text;
if ( double.TryParse(QueryLabel.Text, out double parsedNumber) && operatorClicked == false )
{
num1 = parsedNumber;
ResultLabel.Text = "";
operatorClicked = true;
operatorCount = 1;
// Update query with the operator
QueryLabel.Text += $" {button.Text} ";
}
else if ( double.TryParse(ResultLabel.Text, out double parsedResult) && operatorClicked == true )
{
num1 = parsedResult;
ResultLabel.Text = "";
operatorCount += 1;
danglingText = "";
// Update query with the operator
QueryLabel.Text += $" {button.Text} ";
ResultLabel.Text = "";
}
else
{
ResultLabel.Text = "Error hapa";
}
}
}
private void OnNumberClicked(object sender, EventArgs e)
{
if (sender is Button button)
{
if (string.IsNullOrEmpty(QueryLabel.Text) || QueryLabel.Text == "0")
{
QueryLabel.Text = button.Text;
}
else if ( operatorClicked == true && num1 != null )
{
danglingText += button.Text;
double.TryParse(danglingText, out double parsedNumber);
dangling = parsedNumber;
double result = currentOperator switch
{
"÷" => dangling == 0 ? double.NaN : num1.Value / dangling.Value,
"×" => num1.Value * dangling.Value,
"-" => num1.Value - dangling.Value,
"+" => num1.Value + dangling.Value,
_ => double.NaN,
};
ResultLabel.Text = double.IsNaN(result) ? "Error Uwiii" : result.ToString();
QueryLabel.Text += button.Text;
}
else
{
QueryLabel.Text += button.Text;
}
// Update query as well
//ResultLabel.Text += button.Text;
}
}
private void OnDecimalClicked(object sender, EventArgs e)
{
if (!QueryLabel.Text.Contains("."))
{
QueryLabel.Text += ".";
}
}
private void OnClearClicked(object sender, EventArgs e)
{
ResultLabel.Text = "";
QueryLabel.Text = "";
dangling = null;
danglingText = null;
currentOperator = null;
operatorClicked = false;
}
private void OnEqualsClicked(object sender, EventArgs e)
{
QueryLabel.Text = ResultLabel.Text;
ResultLabel.Text = "";
num2 = null;
dangling = null;
danglingText = null;
currentOperator = null;
operatorClicked = false;
}
}
}