Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
663 lines (581 loc) · 21.8 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
663 lines (581 loc) · 21.8 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
/// <todo>
/// TODO - Fix Factorial - shows infinity for high values
/// TODO - Type into text box
/// TODO - Add exponential notation
/// </todo>
namespace Calculator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
// Max digits to be shown in the memory label, does not affect the actual number stored in memory
const int maxMemoryLabelLength = 6;
// Default font result box font size
const int defaultFontSize = 48;
// True if there is an on going math operation
bool operationCheck;
// True if a function (sin, tan, ln, log etc) was called on the number during another mathematical operation
bool functionCheck;
// True if the result box is to be cleared when a number is entered
bool clearNext;
// True if the text in the result box is the result of some computation
bool isResult;
// True if the text in the result box has not been changed after clicking an operator
bool isOldText;
// Stores the number in memory accessed via MR
double memory = 0;
// Stores the text in the text box after a new math operation is selected
string previousText;
// Trigonometric modes
enum trigModes
{
STANDARD, // Default mode
HYPERBOLIC,
ARC
}
// Stores the current trigonometric mode
trigModes currentTrigMode;
// Symbols to show on the button for different trig modes
Dictionary<trigModes, string> trigModeSymbols = new Dictionary<trigModes, string>()
{
{ trigModes.STANDARD, "STD" },
{ trigModes.ARC, "ARC" },
{ trigModes.HYPERBOLIC, "HYP" }
};
// Stores the current angle unit, default is radians
Angles.units angleUnit;
// Symbols to show on the button for different angle units
Dictionary<Angles.units, string> angleUnitSymbols = new Dictionary<Angles.units, string>()
{
{ Angles.units.RADIANS, "RAD" },
{ Angles.units.DEGREES, "DEG" },
{ Angles.units.GRADIANS, "GRAD" }
};
// Errors that may occur
static string OVERFLOW = "Overflow";
static string INVALID_INPUT = "Invalid input";
static string NOT_A_NUMBER = "NaN";
string[] errors = { OVERFLOW, INVALID_INPUT, NOT_A_NUMBER };
// Holds the current operation
operations currentOperation = operations.NULL;
// Math operations that take two operands
enum operations
{
ADDITION,
SUBTRACTION,
DIVISION,
MULTIPLICATION,
POWER,
NULL // Represents no operation (used to reset the status)
}
public MainWindow()
{
InitializeComponent();
angle_unit_button.Content = angleUnitSymbols[angleUnit];
trig_mode_button.Content = trigModeSymbols[currentTrigMode];
}
/// <summary>
/// Displays the given text to the result box and sets the value of clearNext to true by default (false if specified).
/// </summary>
private void showText(string text, bool clear=true)
{
try
{
if (double.Parse(text) == 0)
text = "0";
}
catch (Exception)
{
showError(INVALID_INPUT);
return;
}
if (text.Length > 30)
return;
if (text.Length > 12)
resultBox.FontSize = 25;
if (text.Length > 24)
resultBox.FontSize = 20;
clearNext = clear;
resultBox.Text = text;
}
/// <summary>
/// Displays the given text in the result box.
/// </summary>
private void showError(string text)
{
resultBox.Text = text;
previousText = null;
operationCheck = false;
clearNext = true;
updateEquationBox("");
currentOperation = operations.NULL;
resetFontSize();
}
/// <summary>
/// Updates the equation box with the given equation string.
/// If append is true then the given text is appended to the existing text in the equation box.
/// </summary>
private void updateEquationBox(string equation, bool append=false)
{
// Removes pointless decimals from the numbers in the equation
equation = Regex.Replace(equation, @"(\d+)\.\s", "$1 ");
if (equation.Length > 10)
equationBox.FontSize = 18;
if (!append)
equationBox.Text = equation;
else
equationBox.Text += equation;
}
/// <summary>
/// Updates the memory label text with the value in memory variable.
/// </summary>
private void updateMemoryLabel()
{
memoryLabel.Content = memory.ToString();
if (memoryLabel.Content.ToString().Length > maxMemoryLabelLength)
memoryLabel.Content = memoryLabel.Content.ToString().Substring(0, 5) + "...";
}
/// <summary>
/// Parses the text in the text box into a double datatype and returns it.
/// </summary>
private double getNumber()
{
double number = double.Parse(resultBox.Text);
return number;
}
/// <summary>
/// Resets the result box font size to defaultSize
/// </summary>
private void resetFontSize()
{
resultBox.FontSize = defaultFontSize;
}
/// <summary>
/// Calculates the result by solving the previousText and current text in the result
/// box with the operand in currentOperation.
/// </summary>
private void calculateResult()
{
if (currentOperation == operations.NULL)
return;
double a = double.Parse(previousText); // first operand
double b = double.Parse(resultBox.Text); // second operand
double result;
switch(currentOperation)
{
case operations.DIVISION:
result = a / b;
break;
case operations.MULTIPLICATION:
result = a * b;
break;
case operations.ADDITION:
result = a + b;
break;
case operations.SUBTRACTION:
result = a - b;
break;
case operations.POWER:
result = Math.Pow(a, b);
break;
default:
return;
}
if (errors.Contains(resultBox.Text))
return;
operationCheck = false;
previousText = null;
string equation;
// If a function button was not clicked during a mathematical operation then the equation box will have the text with the
// format <operand a> <operation> <operand b as a number> else <operand a> <operation> <func>(<operand b>)
if (!functionCheck)
equation = equationBox.Text + b.ToString();
else
{
equation = equationBox.Text;
functionCheck = false;
}
updateEquationBox(equation);
showText(result.ToString());
currentOperation = operations.NULL;
isResult = true;
}
/// <summary>
/// Appends the digit clicked to the text in the text box.
/// If an ongoing operation has been selected then the text box value is first assigned to previousText variable and then new text
/// is appended to the text box after truncating the previous text.
/// </summary>
private void numberClick(object sender, RoutedEventArgs e)
{
isResult = false;
Button button = (Button)sender;
if (resultBox.Text == "0" || errors.Contains(resultBox.Text))
resultBox.Clear();
string text;
if (clearNext)
{
resetFontSize();
text = button.Content.ToString();
isOldText = false;
}
else
text = resultBox.Text + button.Content.ToString();
if (!operationCheck && equationBox.Text != "")
updateEquationBox("");
showText(text, false);
}
/// <summary>
/// Changes the current angle unit.
/// </summary>
private void angle_unit_button_Click(object sender, RoutedEventArgs e)
{
List<Angles.units> units = new List<Angles.units>()
{
Angles.units.RADIANS,
Angles.units.DEGREES,
Angles.units.GRADIANS
};
Button button = (Button)sender;
angleUnit = units.ElementAtOrDefault(units.IndexOf(angleUnit) + 1);
button.Content = angleUnitSymbols[angleUnit];
}
/// <summary>
/// Changes the trigonometric functions mode to Normal, Hyperbolic or Arc
/// </summary>
private void trig_mode_button_Click(object sender, RoutedEventArgs e)
{
List<trigModes> modes = new List<trigModes>()
{
trigModes.STANDARD,
trigModes.ARC,
trigModes.HYPERBOLIC
};
Button button = (Button)sender;
currentTrigMode = modes.ElementAtOrDefault(modes.IndexOf(currentTrigMode) + 1);
button.Content = trigModeSymbols[currentTrigMode];
if (currentTrigMode == trigModes.STANDARD)
{
sin_button.Content = "sin";
cos_button.Content = "cos";
tan_button.Content = "tan";
}
if (currentTrigMode == trigModes.HYPERBOLIC)
{
sin_button.Content = "sinh";
cos_button.Content = "cosh";
tan_button.Content = "tanh";
}
if (currentTrigMode == trigModes.ARC)
{
sin_button.Content = "asin";
cos_button.Content = "acos";
tan_button.Content = "atan";
}
}
/// <summary>
/// Deals with function button clicks.
/// </summary>
private void function(object sender, RoutedEventArgs e)
{
if (errors.Contains(resultBox.Text))
return;
Button button = (Button)sender;
string buttonText = button.Content.ToString();
double number = getNumber();
string equation = "";
string result = "";
switch (buttonText)
{
case "!":
if (number < 0 || number.ToString().Contains("."))
{
showError(INVALID_INPUT);
return;
}
if (number > 3248) // chose this number because the default windows calculator doesn't go beyond this number
{
showError(OVERFLOW);
return;
}
double res = 1;
if (number == 1 || number == 0)
result = res.ToString();
else
{
for (int i = 2; i <= number; i++)
{
res *= i;
}
}
equation = "fact(" + number.ToString() + ")";
result = res.ToString();
break;
case "ln":
equation = "ln(" + number + ")";
result = Math.Log(number).ToString();
break;
case "log":
equation = "log(" + number + ")";
result = Math.Log10(number).ToString();
break;
case "√":
equation = "√(" + number + ")";
result = Math.Sqrt(number).ToString();
break;
case "-n":
equation = "negate(" + number + ")";
result = decimal.Negate((decimal)number).ToString();
break;
}
if (operationCheck)
{
equation = equationBox.Text + equation;
functionCheck = true;
}
updateEquationBox(equation);
showText(result);
}
/// <summary>
/// Deals with trigonometric function button clicks.
/// </summary>
private void trigFunction(object sender, RoutedEventArgs e)
{
if (errors.Contains(resultBox.Text))
return;
Button button = (Button)sender;
string buttonText = button.Content.ToString();
string equation = "";
string result = "";
double number = getNumber();
switch (currentTrigMode)
{
// Standard trig functions
case trigModes.STANDARD:
double radianAngle = Angles.Converter.radians(number, angleUnit);
switch (buttonText)
{
case "sin":
equation = "sin(" + number.ToString() + ")";
result = Math.Sin(radianAngle).ToString();
break;
case "cos":
equation = "cos(" + number.ToString() + ")";
result = Math.Cos(radianAngle).ToString();
break;
case "tan":
equation = "tan(" + number.ToString() + ")";
result = Math.Tan(radianAngle).ToString();
break;
}
break;
// Hyperbolic trig functions
case trigModes.HYPERBOLIC:
switch(buttonText)
{
case "sinh":
equation = "sinh(" + number + ")";
result = Math.Sinh(number).ToString();
break;
case "cosh":
equation = "cosh(" + number + ")";
result = Math.Cosh(number).ToString();
break;
case "tanh":
equation = "tanh(" + number + ")";
result = Math.Tanh(number).ToString();
break;
}
break;
// Arc trig functions
case trigModes.ARC:
switch (buttonText)
{
case "asin":
equation = "asin(" + number + ")";
result = Math.Asin(number).ToString();
break;
case "acos":
equation = "acos(" + number + ")";
result = Math.Acos(number).ToString();
break;
case "atan":
equation = "atan(" + number + ")";
result = Math.Atan(number).ToString();
break;
}
break;
}
// We need to convert the result to the given angle unit if arc trig functions are used
if (currentTrigMode == trigModes.ARC)
{
switch(angleUnit)
{
case Angles.units.DEGREES:
result = Angles.Converter.degrees(double.Parse(result), Angles.units.RADIANS).ToString();
break;
case Angles.units.GRADIANS:
result = Angles.Converter.gradians(double.Parse(result), Angles.units.RADIANS).ToString();
break;
default: // 'result' is in radians by default
break;
}
}
if (operationCheck)
{
equation = equationBox.Text + equation;
functionCheck = true;
}
updateEquationBox(equation);
showText(result);
}
/// <summary>
/// Deals with double operand function clicks.
/// </summary>
private void doubleOperandFunction(object sender, RoutedEventArgs e)
{
if (errors.Contains(resultBox.Text))
return;
if (operationCheck && !isOldText)
calculateResult();
Button button = (Button)sender;
operationCheck = true;
previousText = resultBox.Text;
string buttonText = button.Content.ToString();
string equation = previousText + " " + buttonText + " ";
switch(buttonText)
{
case "/":
currentOperation = operations.DIVISION;
break;
case "x":
currentOperation = operations.MULTIPLICATION;
break;
case "-":
currentOperation = operations.SUBTRACTION;
break;
case "+":
currentOperation = operations.ADDITION;
break;
case "^":
currentOperation = operations.POWER;
break;
}
updateEquationBox(equation);
resetFontSize();
showText(resultBox.Text);
isOldText = true;
}
/// <summary>
/// Appends a decimal point to the number in the result box on click,
/// if the number already has a decimal point then no action is taken
/// </summary>
private void decimal_button_Click(object sender, RoutedEventArgs e)
{
if (!resultBox.Text.Contains("."))
{
string text = resultBox.Text += ".";
showText(text, false);
}
}
private void pi_button_Click(object sender, RoutedEventArgs e)
{
if (!operationCheck)
updateEquationBox("");
showText(Math.PI.ToString());
isResult = true; // Constants cannot be changed
}
private void e_button_Click(object sender, RoutedEventArgs e)
{
if (!operationCheck)
updateEquationBox("");
showText(Math.E.ToString());
isResult = true; // Constants cannot be changed
}
private void madd_button_Click(object sender, RoutedEventArgs e)
{
if (errors.Contains(resultBox.Text))
return;
memory += getNumber();
updateMemoryLabel();
}
private void msub_button_Click(object sender, RoutedEventArgs e)
{
if (errors.Contains(resultBox.Text))
return;
memory -= getNumber();
updateMemoryLabel();
}
private void mc_button_Click(object sender, RoutedEventArgs e)
{
memory = 0;
updateMemoryLabel();
}
private void mr_button_Click(object sender, RoutedEventArgs e)
{
showText(memory.ToString());
if (!operationCheck)
updateEquationBox("");
}
private void clear_button_Click(object sender, RoutedEventArgs e)
{
resultBox.Text = "0";
operationCheck = false;
previousText = null;
updateEquationBox("");
resetFontSize();
}
private void clr_entry_button_Click(object sender, RoutedEventArgs e)
{
resultBox.Text = "0";
resetFontSize();
}
private void equals_button_Click(object sender, RoutedEventArgs e)
{
calculateResult();
}
private void about_button_Click(object sender, RoutedEventArgs e)
{
AboutBox aboutForm = new AboutBox();
aboutForm.ShowDialog();
}
// Copy
private void copy_button_Click(object sender, RoutedEventArgs e)
{
if (errors.Contains(resultBox.Text))
return;
Clipboard.SetData(DataFormats.UnicodeText, resultBox.Text);
}
// Paste
private void paste_button_Click(object sender, RoutedEventArgs e)
{
object clipboardData = Clipboard.GetData(DataFormats.UnicodeText);
if (clipboardData != null)
{
string data = clipboardData.ToString();
showText(data.ToString());
}
else
return;
}
private void back_button_Click(object sender, RoutedEventArgs e)
{
if (isResult)
return;
string text;
if (resultBox.Text.Length == 1)
text = "0";
else
text = resultBox.Text.Substring(0, resultBox.Text.Length - 1);
showText(text, false);
}
}
}
You can’t perform that action at this time.
