-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
157 lines (142 loc) · 4.7 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Calculator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
double prevNum, result;
SelectedOperator selectedOperator;
public MainWindow()
{
InitializeComponent();
}
private void ButtonDecimal_Click(object sender, RoutedEventArgs e)
{
if (labelResult.Content.ToString().Contains("."))
{
//Do nothing
}
else
{
labelResult.Content = $"{labelResult.Content}.";
}
}
private void ButtonEquals_Click(object sender, RoutedEventArgs e)
{
double NewNum;
if (double.TryParse(labelResult.Content.ToString(), out NewNum))
{
switch (selectedOperator)
{
case SelectedOperator.Addition:
result = SimpleMaths.Add(prevNum,NewNum);
break;
case SelectedOperator.Subtraction:
result = SimpleMaths.Subtraction(prevNum,NewNum);
break;
case SelectedOperator.Multiplication:
result = SimpleMaths.Multiplication(prevNum,NewNum);
break;
case SelectedOperator.Division:
result = SimpleMaths.Division(prevNum,NewNum);
break;
case SelectedOperator.Mod:
result = SimpleMaths.Modulus(prevNum,NewNum);
break;
}
labelResult.Content = result.ToString();
}
}
private void OperationButton_Click(object sender, RoutedEventArgs e)
{
if (double.TryParse(labelResult.Content.ToString(), out prevNum))
{
labelResult.Content = "0";
}
if (sender == buttonMultiplication)
selectedOperator = SelectedOperator.Multiplication;
else if (sender == buttonDivision)
selectedOperator = SelectedOperator.Division;
else if (sender == buttonAddition)
selectedOperator = SelectedOperator.Addition;
else if (sender == buttonSubtraction)
selectedOperator = SelectedOperator.Subtraction;
else if (sender == buttonMod)
selectedOperator = SelectedOperator.Mod;
}
private void ButtonNegative_Click(object sender, RoutedEventArgs e)
{
if (double.TryParse(labelResult.Content.ToString(), out prevNum))
{
prevNum = prevNum * -1;
labelResult.Content = prevNum.ToString();
}
}
private void ButtonAC_Click(object sender, RoutedEventArgs e)
{
labelResult.Content= "0";
result = 0;
prevNum = 0;
}
private void ButtonNumber_Click(object sender, RoutedEventArgs e)
{
int selectedValue = int.Parse(((Button)sender).Content.ToString());
if (labelResult.Content.ToString() != "0")
labelResult.Content = $"{labelResult.Content}{selectedValue}";
else
labelResult.Content = $"{selectedValue}";
}
}
public enum SelectedOperator
{
Multiplication,
Division,
Addition,
Subtraction,
Mod
}
public class SimpleMaths
{
public static double Add(double n1, double n2)
{
return n1 + n2;
}
public static double Subtraction(double n1, double n2)
{
return n1 - n2;
}
public static double Multiplication(double n1, double n2)
{
return n1 * n2;
}
public static double Division(double n1, double n2)
{
if (n2 == 0)
{
MessageBox.Show("Cannot divide by 0!", "Wrong Operation", MessageBoxButton.OK, MessageBoxImage.Error);
return 0;
}
return n1 / n2;
}
public static double Modulus(double n1, double n2)
{
return n1 % n2;
}
}
}