-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
165 lines (160 loc) · 5.36 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
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_.NET_Core
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private double lastNumber, result;
SelectedOperator selectedoperator;
public MainWindow()
{
InitializeComponent();
resultButton.Click += ResultButton_Click;
percentButton.Click += PercentButton_Click;
minPlusButton.Click += MinPlusButton_Click;
acButton.Click += AcButton_Click;
}
private void AcButton_Click(object sender, RoutedEventArgs e)
{
resultLabel.Content = "0";
}
private void MinPlusButton_Click(object sender, RoutedEventArgs e)
{
if(double.TryParse(resultLabel.Content.ToString(), out lastNumber))
{
lastNumber *= -1;
resultLabel.Content = lastNumber.ToString();
}
}
private void PercentButton_Click(object sender, RoutedEventArgs e)
{
if (double.TryParse(resultLabel.Content.ToString(), out lastNumber))
{
lastNumber /= 100;
operatorLabel.Content = "%";
resultLabel.Content = lastNumber.ToString();
}
}
private void OperationButton_Click(object sender, RoutedEventArgs e)
{
if (double.TryParse(resultLabel.Content.ToString(), out lastNumber))
{
resultLabel.Content = "0";
}
if (sender == plusButton)
{
selectedoperator = SelectedOperator.Pertambahan;
operatorLabel.Content = "+";
}
if (sender == minusButton)
{
selectedoperator = SelectedOperator.Pengurangan;
operatorLabel.Content = "-";
}
if (sender == multiplyButton)
{
selectedoperator = SelectedOperator.Perkalian;
operatorLabel.Content = "*";
}
if (sender == divideButton)
{
selectedoperator = SelectedOperator.Pembagian;
operatorLabel.Content = "/";
}
}
private void NumberButton_Click(object sender, RoutedEventArgs e)
{
int selectedValue = int.Parse((sender as Button).Content.ToString());
if (resultLabel.Content.ToString() is "0")
{
resultLabel.Content = $"{selectedValue}";
}
else
{
resultLabel.Content = $"{resultLabel.Content}{selectedValue}";
}
}
private void ComaButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString().Contains(","))
{
// Kosong
}
else
{
resultLabel.Content = $"{resultLabel.Content},";
}
}
private void ResultButton_Click(object sender, RoutedEventArgs e)
{
double newNumber;
Console.WriteLine(lastNumber);
if(double.TryParse(resultLabel.Content.ToString(), out newNumber))
{
switch (selectedoperator)
{
case SelectedOperator.Pertambahan:
result = Hitung.Pertambahan(lastNumber, newNumber);
break;
case SelectedOperator.Pengurangan:
result = Hitung.Pengurangan(lastNumber, newNumber);
break;
case SelectedOperator.Perkalian:
result = Hitung.Perkalian(lastNumber, newNumber);
break;
case SelectedOperator.Pembagian:
result = Hitung.Pembagian(lastNumber, newNumber);
break;
}
operatorLabel.Content = "";
resultLabel.Content = result.ToString();
}
}
}
public enum SelectedOperator
{
Pertambahan,
Pengurangan,
Perkalian,
Pembagian
}
public class Hitung
{
public static double Pertambahan(double n1, double n2)
{
return n1 + n2;
}
public static double Pengurangan(double n1, double n2)
{
return n1 - n2;
}
public static double Perkalian(double n1, double n2)
{
return n1 * n2;
}
public static double Pembagian(double n1, double n2)
{
if(n2 is 0)
{
MessageBox.Show("You can't divide by 0", "Number is Wrong", MessageBoxButton.OK, MessageBoxImage.Hand);
return 0;
}
return n1 / n2;
}
}
}