-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalculatorForm.cs
167 lines (141 loc) · 4.24 KB
/
CalculatorForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculator
{
public partial class CalculatorForm : Form
{
public CalculatorForm()
{
InitializeComponent();
}
private double EvaluateString(string expression)
{
DataTable table = new DataTable();
table.Columns.Add("expression", typeof(string), expression);
DataRow row = table.NewRow();
table.Rows.Add(row);
return double.Parse((string)row["expression"]);
}
private void Calculate()
{
ClearOutput();
try
{
double rawAnswer = EvaluateString(tbCalcInput.Text);
if (rawAnswer == Double.PositiveInfinity)
{
lblErrorText.Text = "Error: Dividing by Zero is Invalid";
}
else
{
lblResultOutput.Text = rawAnswer.ToString();
}
}
catch (InvalidCastException icEx)
{
lblErrorText.Text = "Error: Invalid Syntax";
}
catch (SyntaxErrorException seEx)
{
lblErrorText.Text = "Error: Invalid Syntax";
}
catch (EvaluateException eEx)
{
lblErrorText.Text = "Error: Invalid Syntax";
}
}
private void ClearInput()
{
tbCalcInput.Text = "";
}
private void ClearOutput()
{
lblResultOutput.Text = "";
lblErrorText.Text = "";
}
private void btnZero_Click(object sender, EventArgs e)
{
tbCalcInput.Text += "0";
}
private void btnOne_Click(object sender, EventArgs e)
{
tbCalcInput.Text += "1";
}
private void btnTwo_Click(object sender, EventArgs e)
{
tbCalcInput.Text += "2";
}
private void btnThree_Click(object sender, EventArgs e)
{
tbCalcInput.Text += "3";
}
private void btnFour_Click(object sender, EventArgs e)
{
tbCalcInput.Text += "4";
}
private void btnFive_Click(object sender, EventArgs e)
{
tbCalcInput.Text += "5";
}
private void btnSix_Click(object sender, EventArgs e)
{
tbCalcInput.Text += "6";
}
private void btnSeven_Click(object sender, EventArgs e)
{
tbCalcInput.Text += "7";
}
private void btnEight_Click(object sender, EventArgs e)
{
tbCalcInput.Text += "8";
}
private void btnNine_Click(object sender, EventArgs e)
{
tbCalcInput.Text += "9";
}
private void btnDot_Click(object sender, EventArgs e)
{
tbCalcInput.Text += ".";
}
private void btnAdd_Click(object sender, EventArgs e)
{
tbCalcInput.Text += "+";
}
private void btnSubtract_Click(object sender, EventArgs e)
{
tbCalcInput.Text += "-";
}
private void btnMultiply_Click(object sender, EventArgs e)
{
tbCalcInput.Text += "*";
}
private void btnDivide_Click(object sender, EventArgs e)
{
tbCalcInput.Text += "/";
}
private void btnClearResult_Click(object sender, EventArgs e)
{
ClearInput();
ClearOutput();
}
private void btnEvaluate_Click(object sender, EventArgs e)
{
Calculate();
}
private void tbCalcInput_HandleKeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Calculate();
e.Handled = true;
}
}
}
}