This repository has been archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
258 lines (207 loc) · 7.22 KB
/
MainWindow.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace DentakuCore
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
private string Number;
private string Buffer;
private double Result;
private string Expression;
public MainWindow()
{
InitializeComponent();
Number = "0";
Buffer = "0";
_ = calculate();
UpdateDisplay();
UpdateSubDisplay();
}
private void ButtonCE_Click(object sender, RoutedEventArgs e)
{
Number = "0";
Buffer = "0";
UpdateDisplay();
UpdateSubDisplay();
}
private void ButtonC_Click(object sender, RoutedEventArgs e)
{
Number = "0";
UpdateDisplay();
}
private void ButtonBackspace_Click(object sender, RoutedEventArgs e)
{
var length = Number.Length;
if (length == 1 || (length == 2 && int.Parse(Number) < 0))
Number = "0";
else
Number = Number.Remove(length - 1, 1);
UpdateDisplay();
}
private void ButtonDivide_Click(object sender, RoutedEventArgs e)
{
if (Buffer == "0")
Buffer = "";
if (Number == "" && (Buffer[Buffer.Length - 2] == '+' || Buffer[Buffer.Length - 2] == '-' || Buffer[Buffer.Length - 2] == '*' || Buffer[Buffer.Length - 2] == '/'))
{
Buffer = Buffer.Remove(Buffer.Length - 2, 2) + "/ ";
UpdateSubDisplay();
return;
}
if (Number[Number.Length - 1] == '.')
Number = Number.Remove(Number.Length - 1, 1);
Buffer += $"{Number} / ";
UpdateDisplay();
Number = "";
UpdateSubDisplay();
}
private void ButtonMulti_Click(object sender, RoutedEventArgs e)
{
if (Buffer == "0")
Buffer = "";
if (Number == "" && (Buffer[Buffer.Length - 2] == '+' || Buffer[Buffer.Length - 2] == '-' || Buffer[Buffer.Length - 2] == '*' || Buffer[Buffer.Length - 2] == '/'))
{
Buffer = Buffer.Remove(Buffer.Length - 2, 2) + "* ";
UpdateSubDisplay();
return;
}
if (Number[Number.Length - 1] == '.')
Number = Number.Remove(Number.Length - 1, 1);
Buffer += $"{Number} * ";
UpdateDisplay();
Number = "";
UpdateSubDisplay();
}
private void ButtonSub_Click(object sender, RoutedEventArgs e)
{
if (Buffer == "0")
Buffer = "";
if (Number == "" && (Buffer[Buffer.Length - 2] == '+' || Buffer[Buffer.Length - 2] == '-' || Buffer[Buffer.Length - 2] == '*' || Buffer[Buffer.Length - 2] == '/'))
{
Buffer = Buffer.Remove(Buffer.Length - 2, 2) + "- ";
UpdateSubDisplay();
return;
}
if (Number[Number.Length - 1] == '.')
Number = Number.Remove(Number.Length - 1, 1);
Buffer += $"{Number} - ";
UpdateDisplay();
Number = "";
UpdateSubDisplay();
}
private void ButtonAdd_Click(object sender, RoutedEventArgs e)
{
if (Buffer == "0")
Buffer = "";
if (Number == "" && (Buffer[Buffer.Length - 2] == '+' || Buffer[Buffer.Length - 2] == '-' || Buffer[Buffer.Length - 2] == '*' || Buffer[Buffer.Length - 2] == '/'))
{
Buffer = Buffer.Remove(Buffer.Length - 2, 2) + "+ ";
UpdateSubDisplay();
return;
}
if (Number[Number.Length - 1] == '.')
Number = Number.Remove(Number.Length - 1, 1);
Buffer += $"{Number} + ";
UpdateDisplay();
Number = "";
UpdateSubDisplay();
}
private void ButtonInvert_Click(object sender, RoutedEventArgs e)
{
if (Number == "0")
return;
if (Number[0] == '-')
Number = Number.Remove(0, 1);
else
Number = $"-{Number}";
UpdateDisplay();
UpdateSubDisplay();
}
private void ButtonPoint_Click(object sender, RoutedEventArgs e)
{
if (!Number.Contains("."))
Number += ".";
UpdateDisplay();
UpdateSubDisplay();
}
private void ButtonEnter_Click(object sender, RoutedEventArgs e)
{
if (Number[Number.Length - 1] == '.')
Number = Number.Remove(Number.Length - 1, 1);
if (Buffer == "0")
Buffer = "";
Buffer += Number;
var length = Buffer.Length;
if (Buffer[length - 1] == ' ')
Buffer = Buffer.Remove(length - 1, 1);
length = Buffer.Length;
if (Buffer[length - 1] == '+' || Buffer[length - 1] == '-' || Buffer[length - 1] == '*' || Buffer[length - 1] == '/')
if (length != 1)
Buffer = Buffer.Remove(length - 2, 2);
if (Buffer.Contains("/ 0"))
{
UpdateSubDisplay();
Number = "0";
Buffer = "0";
Display.Content = "NaN";
return;
}
_ = calculate();
Number = Result.ToString();
UpdateSubDisplay();
Buffer = "0";
UpdateDisplay();
}
private void ButtonNumber_Click(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
if (Number == "0")
Number = "";
Number += button.Content;
UpdateDisplay();
UpdateSubDisplay();
}
private void UpdateDisplay()
{
Display.Content = Number;
}
private void UpdateSubDisplay()
{
if (Buffer == "0")
Expression = "";
else
Expression = Buffer.Replace("*", "×").Replace("/", "÷");
SubDisplay.Content = Expression;
}
private async Task calculate()
{
if (Number == "")
Number = "0";
if (Buffer == "")
Buffer = "0";
var code = $"double result = (double)({Buffer});";
try
{
var script = CSharpScript.Create(code);
var state = await script.RunAsync();
var calcResult = state.GetVariable("result");
Result = (double)calcResult.Value;
}
catch (CompilationErrorException ex)
{
MessageBox.Show(ex.Message, "構文エラー");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "不明なエラー");
}
}
}
}