-
Notifications
You must be signed in to change notification settings - Fork 3
/
Calculator.xaml.cs
209 lines (192 loc) · 6.49 KB
/
Calculator.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
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.Shapes;
namespace DesktopApp
{
/// <summary>
/// Logique d'interaction pour Calculator.xaml
/// </summary>
public partial class Calculator : Window
{
double firstNum = 0f;
double secondNum = 0f;
int operation = -1;
string operationString;
bool used;
public Calculator()
{
InitializeComponent();
}
private void ButtonClick(object sender, RoutedEventArgs e)
{
if (used)
{
used = false;
firstNum = secondNum = 0;
operation = -1;
operationString = "";
textBoxOperation.Text = "Operations will appear here...";
textBoxSolution.Text = "Solution";
}
if (this.textBoxOperation.Text.Equals("Operations will appear here..."))
{
this.textBoxOperation.Text = "";
}
if (((Button)sender).Content.ToString() == "Del")
{
if (this.textBoxOperation.Text.Length > 0)
this.textBoxOperation.Text = this.textBoxOperation.Text.Remove(this.textBoxOperation.Text.Length - 1);
}
else
{
this.textBoxOperation.Text += ((Button)sender).Content;
}
}
private void ButtonOp(object sender, RoutedEventArgs e)
{
if (this.textBoxOperation.Text.Equals("Operations will appear here..."))
{
MessageBox.Show("You can't start with an operation!");
}
else
{
String tempOp = ((Button)sender).Content.ToString();
if (tempOp == "=")
{
CalculateSolution();
}
else if (tempOp == "C")
{
used = false;
firstNum = secondNum = 0;
operation = -1;
operationString = "";
textBoxOperation.Text = "Operations will appear here...";
textBoxSolution.Text = "Solution";
}
else
{
operationString = tempOp;
if (operationString == "+")
{
operation = 0;
}
else if (operationString == "-")
{
operation = 1;
}
else if (operationString == "x")
{
operation = 2;
}
else if (operationString == "/")
{
operation = 3;
}
else if (operationString == "%")
{
operation = 4;
}
else
{
MessageBox.Show("Coming soon...");
}
firstNum = double.Parse(this.textBoxOperation.Text);
this.textBoxOperation.Text += ((Button)sender).Content;
}
}
}
void CalculateSolution()
{
secondNum = double.Parse(this.textBoxOperation.Text.Split(operationString[0])[1]);
if (secondNum == 0 && operation == 3)
{
MessageBox.Show("Error, devision by zero is not tolerated");
}
else
{
double result;
switch (operation)
{
case 0:
result = firstNum + secondNum;
break;
case 1:
result = firstNum - secondNum;
break;
case 2:
result = firstNum * secondNum;
break;
case 3:
result = firstNum / secondNum;
break;
case 4:
result = firstNum % secondNum;
break;
default:
result = 0;
break;
}
string[] preAnswers =
{
"I think it's ",
"You ll get ",
"Ehhh...",
"Well, it's ",
"Pfff, it's "
};
string[] postAnswers =
{
" Right?",
" I'm sure",
" I think..",
" Or is it?",
""
};
int randomIndex = new Random().Next(5);
this.textBoxSolution.Text = preAnswers[randomIndex] + result + postAnswers[randomIndex];
used = true;
if ((result % 1) == 0)
{
getFunnyFact("http://numbersapi.com/" + result);
}
}
}
private void funnyFact(object sender, RoutedEventArgs e)
{
if (int.TryParse(this.textBoxOperation.Text, out int a))
getFunnyFact("http://numbersapi.com/" + a);
else MessageBox.Show("Please enter a valid format (integer)");
}
public async Task<string> getFunnyFact(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
var content = string.Empty;
var response = await request.GetResponseAsync();
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
content = sr.ReadToEnd();
//textBoxSolution.Text = content;
new FunFact(content).Show();
return "";
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
}
}