-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
438 lines (360 loc) · 11.3 KB
/
Form1.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
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
/* Calculator app with basic mathemtical functions
* with an interactive interface and features included
* to prevent unaccepted user input from crashing the app
* George C.
* Start Date: 01/12/2023
* Finish Date: 01/13/2023,
*/
//i made a change
using System;
using System.Windows.Forms;
namespace Calculator_App
{
public partial class Calculator : Form
{
public Calculator()
{
InitializeComponent();
}
//variable declaration
float number = 0, num2;
public static float answer = 0;
double roundedNum, root;
int operation = 0, indexNum = 0, indexOp = 0, preop = 0;
float[] numArr = new float[100];
int[] opArr = new int[100];
//Loads the calculator app window
private void Form1_Load(object sender, EventArgs e)
{
}
//Changes the Display text
private void Display_TextChanged(object sender, EventArgs e)
{
}
//buttons that adds the number to the display
private void one_Click(object sender, EventArgs e)
{
showNum("1");
}
private void two_Click(object sender, EventArgs e)
{
showNum("2");
}
private void three_Click(object sender, EventArgs e)
{
showNum("3");
}
private void four_Click(object sender, EventArgs e)
{
showNum("4");
}
private void five_Click(object sender, EventArgs e)
{
showNum("5");
}
private void six_Click(object sender, EventArgs e)
{
showNum("6");
}
private void seven_Click(object sender, EventArgs e)
{
showNum("7");
}
private void eight_Click(object sender, EventArgs e)
{
showNum("8");
}
private void nine_Click(object sender, EventArgs e)
{
showNum("9");
}
private void zero_Click(object sender, EventArgs e)
{
showNum("0");
}
private void doublezero_Click(object sender, EventArgs e)
{
showNum("00");
}
//button to display a decimal
private void dot_Click(object sender, EventArgs e)
{
if (Display.Text == "" || int.TryParse(Display.Text, out int j))
{
showNum(".");
}
}
//addition operation
private void plus_Click(object sender, EventArgs e)
{
removeEqual();
check(" + ", 1);
}
//subtraction operation
private void minus_Click(object sender, EventArgs e)
{
removeEqual();
check(" - ", 2);
if (display2.Text == "")
{
Display.Text = Display.Text + "-";
display2.Text = display2.Text + "-";
}
}
//multiplication operation
private void multiply_Click(object sender, EventArgs e)
{
removeEqual();
check(" x ", 3);
}
//division operation
private void divide_Click(object sender, EventArgs e)
{
removeEqual();
check(" ÷ ", 4);
}
//previous answer
private void pre_Click(object sender, EventArgs e)
{
display2.Text = display2.Text + answer.ToString();
if (Display.Text == "")
{
Display.Text = answer.ToString();
}
}
public void showNum(string num)
{
Display.Text = Display.Text + num;
display2.Text = display2.Text + num;
}
public void storeNum(float numStored)
{
numArr[indexNum] = numStored;
indexNum++;
}
public void storeOp(int opStored)
{
opArr[indexOp] = opStored;
indexOp++;
}
public void removeEqual()
{
if (display2.Text.Contains("="))
{
string displayTwo = display2.Text;
display2.Text = displayTwo.Remove(displayTwo.Length - 2);
}
}
public void check(string sign, int op)
{
storeNum(float.Parse(Display.Text));
if (int.TryParse(Display.Text, out int j) || float.TryParse(Display.Text, out float k))
{
number = float.Parse(Display.Text);
Display.Clear();
Display.Focus();
display2.Text = display2.Text + sign;
storeOp(op);
}
}
public void compute()
{
storeNum(float.Parse(Display.Text));
switch (opArr[0])
{
case 1:
answer = numArr[0] + numArr[1];
display2.Text = display2.Text + " =";
preop = 1;
break;
case 2:
answer = numArr[0] - numArr[1];
display2.Text = display2.Text + " =";
preop = 2;
break;
case 3:
answer = numArr[0] * numArr[1];
display2.Text = display2.Text + " =";
preop = 3;
break;
case 4:
answer = numArr[0] / numArr[1];
display2.Text = display2.Text + " =";
preop = 4;
break;
default:
break;
}
if (indexOp > 1)
{
for (int i = 1; i < indexOp; i++)
{
removeEqual();
switch (opArr[i])
{
case 1:
answer = answer + numArr[i + 1];
display2.Text = display2.Text + " =";
preop = 1;
break;
case 2:
answer = answer - numArr[i + 1];
display2.Text = display2.Text + " =";
preop = 2;
break;
case 3:
answer = answer * numArr[i + 1];
display2.Text = display2.Text + " =";
preop = 3;
break;
case 4:
answer = answer / numArr[i + 1];
display2.Text = display2.Text + " =";
preop = 4;
break;
default:
break;
}
}
}
return;
}
//computing the answer (equal sign button)
private void equal_Click(object sender, EventArgs e)
{
compute();
Display.Text = answer.ToString();
}
private void button1_Click(object sender, EventArgs e) //log
{
}
//delete function
private void delete_Click(object sender, EventArgs e)
{
Display.Text = Display.Text.Remove(Display.Text.Length - 1);
display2.Text = display2.Text.Remove(display2.Text.Length - 1);
}
private void button10_Click(object sender, EventArgs e) //cos
{
}
private void sqrt_Click(object sender, EventArgs e)
{
if (Display.Text != "")
{
display2.Text = "";
display2.Text = "√" + Display.Text;
root = double.Parse(Display.Text);
Display.Text = Math.Sqrt(root).ToString();
}
else
{
operation = 6;
display2.Text = display2.Text + "√";
}
}
private void absolute_Click(object sender, EventArgs e)
{
}
//rounding function
private void round_Click(object sender, EventArgs e)
{
roundedNum = double.Parse(Display.Text);
operation = 5;
Display.Clear();
Display.Focus();
}
//square a number
private void squared_Click(object sender, EventArgs e)
{
float num2;
if (int.TryParse(Display.Text, out int j))
{
num2 = float.Parse(Display.Text);
num2 = num2 * num2;
Display.Text = num2.ToString();
display2.Text = display2.Text + "² ";
}
else if (float.TryParse(Display.Text, out float k))
{
num2 = float.Parse(Display.Text);
num2 = num2 * num2;
Display.Text = num2.ToString();
display2.Text = display2.Text + "² ";
}
operation = preop;
preop = 99;
}
//cube a number
private void cube_Click(object sender, EventArgs e)
{
float num2;
if (int.TryParse(Display.Text, out int j))
{
num2 = float.Parse(Display.Text);
num2 = num2 * num2 * num2;
Display.Text = num2.ToString();
display2.Text = display2.Text + "² ";
}
else if (float.TryParse(Display.Text, out float k))
{
num2 = float.Parse(Display.Text);
num2 = num2 * num2 * num2;
Display.Text = num2.ToString();
display2.Text = display2.Text + "² ";
}
operation = preop;
preop = 99;
}
private void exponent_Click(object sender, EventArgs e)
{
}
private void sin_Click(object sender, EventArgs e)
{
}
private void Asin_Click(object sender, EventArgs e)
{
}
private void button8_Click(object sender, EventArgs e) //sinh
{
}
private void Acos_Click(object sender, EventArgs e)
{
}
private void button12_Click(object sender, EventArgs e) //cosh
{
}
private void tan_Click(object sender, EventArgs e)
{
}
private void Atan_Click(object sender, EventArgs e)
{
}
private void button14_Click(object sender, EventArgs e) //tanh
{
}
private void pi_Click(object sender, EventArgs e)
{
if (Display.Text == "")
{
Display.Text = Display.Text + Math.PI;
display2.Text = display2.Text + "π";
}
}
private void display2_TextChanged(object sender, EventArgs e)
{
}
//clear entires and values of variables and ArrayLists (C button)
private void clear_Click(object sender, EventArgs e)
{
Display.Clear();
display2.Clear();
num2 = number = answer = 0;
roundedNum = root = 0;
operation = preop = 0;
indexNum = 0;
indexOp = 0;
Array.Clear(numArr, 0, 100);
Array.Clear(opArr, 0, 100);
}
}
}