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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace NickGeorge.Cis300.Calculator
{
       /// <summary>
       /// Main class for calculator
       /// </summary>
    public partial class Calculator : Form
    {
              /// <summary>
              /// Stores total of calculator input.
              /// </summary>
        private Double _total = 0.0;
 
              /// <summary>
              /// Boolean to tell if calculator is in display or input modes.
              /// </summary>
        private Boolean _input = false;
 
              /// <summary>
              /// Stack of all used operators.
              /// </summary>
              private Stack<char> _operators = new Stack<char>();
 
              /// <summary>
              /// Stack of all used operands.
              /// </summary>
              private Stack<double> _operands = new Stack<double>();
 
              /// <summary>
              /// Method which intializes GUI
              /// </summary>
        public Calculator()
        {
            InitializeComponent();
        }
 
              /// <summary>
              /// Method which appends given text/integer to display and stores the current value if necessary
              /// </summary>
              /// <param name="str">Input string to append to the display</param>
        private void AppendTo(String str)
        {
                     if(this._input)
                            this.uxSum.Text = this.uxSum.Text + str;
                     else
                     {
                            this._input = true;
                            this.uxSum.Text = str;
                     }
        }
 
              /// <summary>
              /// Returns the precedence of given operator
              /// </summary>
              /// <param name="c">Operator</param>
              /// <returns>Int value of precedence 0-2</returns>
              private int GetPrecedence(char c)
              {
                     switch(c)
                     {
                            case '*':
                            case '/':
                                   return 2;
 
                            case '+':
                            case '-':
                                   return 1;
 
                            default:
                                   return 0;
                     }
              }
 
              /// <summary>
              /// Will record an arithmatic sequence.
              /// </summary>
              /// <param name="Operator">Operator of the sequence</param>
              /// <param name="Operand">First value of the sequence</param>
              private void StartSequence(char Operator, double Operand)
              {
                     this._input = false;
                     this._operands.Push(Operand);
                     this._operators.Push(Operator);
                     this.uxSum.Text = Operand.ToString();
              }
 
              /// <summary>
              /// Performs all of the pending operations up to, but not including, the most recent pending operation having precedence less than or equal to a given precedence
              /// </summary>
              /// <param name="precedence">value of precedence at which to stop calculating</param>
              /// <param name="operand">Last pending operand to use in calculation</param>
              /// <returns>Value of calcuation</returns>
              private double Calculate(int precedence, double operand)
              {
                     double total = operand;
 
                     while(this._operators.Count > 0 && this.GetPrecedence(this._operators.Peek()) > (precedence - 1))
                     {                                   
                            switch (this._operators.Pop())
                            {
                                   case '+':
                                          total += this._operands.Pop();
                                          break;
 
                                   case '-':
                                          total = this._operands.Pop() - total;
                                          break;
 
                                   case '*':
                                          total *= this._operands.Pop();
                                          break;
 
                                   case '/':
                                          total = this._operands.Pop() / total;
                                          break;
                            }
                     }
 
                     return total;
              }
 
              /// <summary>
              /// Clears totals and sets display to "0"
              /// </summary>
        private void uxClear_Click(object sender, EventArgs e)
        {
            this._total = 0.0;
            this.uxSum.Text = "0";
                     this._operands.Clear();
                     this._operators.Clear();
            this._input = false;
        }
 
              /// <summary>
              /// Adds 0 to display
              /// </summary>
        private void uxZero_Click(object sender, EventArgs e)
        {
                     AppendTo("0");
        }
 
              /// <summary>
              /// Adds 1 to display
              /// </summary>
              private void uxOne_Click(object sender, EventArgs e)
              {
                     AppendTo("1");
              }
 
              /// <summary>
              /// Adds 2 to display
              /// </summary>
              private void uxTwo_Click(object sender, EventArgs e)
              {
                     AppendTo("2");
              }
 
              /// <summary>
              /// Adds 3 to display
              /// </summary>
              private void uxThree_Click(object sender, EventArgs e)
              {
                     AppendTo("3");
              }
 
              /// <summary>
              /// Adds 4 to display
              /// </summary>
              private void uxFour_Click(object sender, EventArgs e)
              {
                     AppendTo("4");
              }
 
              /// <summary>
              /// Adds 5 to display
              /// </summary>
              private void uxFive_Click(object sender, EventArgs e)
              {
                     AppendTo("5");
              }
 
              /// <summary>
              /// Adds 6 to display
              /// </summary>
              private void uxSix_Click(object sender, EventArgs e)
              {
                     AppendTo("6");
              }
 
              /// <summary>
              /// Adds 7 to display
              /// </summary>
              private void uxSeven_Click(object sender, EventArgs e)
              {
                     AppendTo("7");
              }
 
              /// <summary>
              /// Adds 8 to display
              /// </summary>
              private void uxEight_Click(object sender, EventArgs e)
              {
                     AppendTo("8");
              }
 
              /// <summary>
              /// Adds 9 to display
              /// </summary>
              private void uxNine_Click(object sender, EventArgs e)
              {
                     AppendTo("9");
              }
 
              /// <summary>
              /// If in input mode, will invert the sign associated with the current value
              /// </summary>
              private void uxInvert_Click(object sender, EventArgs e)
              {
                     if(this._input)
                     {
                            if(this.uxSum.Text.CompareTo("0") != 0)
                            {
                                   if(this.uxSum.Text.Contains("-"))
                                          this.uxSum.Text = this.uxSum.Text.Substring(1);
                                   else
                                          this.uxSum.Text = "-" + this.uxSum.Text;
                            }
                     }
              }
 
              /// <summary>
              /// If in input mode, will add a decimal to the current value
              /// </summary>
              private void uxDecimal_Click(object sender, EventArgs e)
              {
                     if (!this._input)
                     {
                            this._input = true;
                            this._total = Double.Parse(this.uxSum.Text);
                            this.uxSum.Text = "0.";
                     }
                     else
                     {
                            if (!this.uxSum.Text.Contains('.'))
                                   this.uxSum.Text = this.uxSum.Text + ".";
                     }
              }
 
              /// <summary>
              /// If two values are given, will operate the two values together to a new value
              /// </summary>
              private void uxEqual_Click(object sender, EventArgs e)
              {
                     this._input = false;
                     double output = Double.Parse(this.uxSum.Text);
                     while(this._operands.Count > 0)
                            output = this.CloseParenth(output);
                     this.uxSum.Text = output.ToString();
              }
 
              /// <summary>
              /// Creates an addition sequence
              /// </summary>
              private void uxAdd_Click(object sender, EventArgs e)
              {
                     this.StartSequence('+', this.Calculate(this.GetPrecedence('+'), Double.Parse(this.uxSum.Text)));
              }
 
              /// <summary>
              /// Creates a subtration sequence
              /// </summary>
              private void uxSubtract_Click(object sender, EventArgs e)
              {
                     this.StartSequence('-', this.Calculate(this.GetPrecedence('-'), Double.Parse(this.uxSum.Text)));
              }
 
              /// <summary>
              /// Creates a multiplication sequence
              /// </summary>
              private void uxMultiply_Click(object sender, EventArgs e)
              {
                     this.StartSequence('*', this.Calculate(this.GetPrecedence('*'), Double.Parse(this.uxSum.Text)));
              }
 
              /// <summary>
              /// Creates a division sequence
              /// </summary>
              private void uxDivide_Click(object sender, EventArgs e)
              {
                     this.StartSequence('/', this.Calculate(this.GetPrecedence('/'), Double.Parse(this.uxSum.Text)));
              }
 
              /// <summary>
              /// Creates a open parenthesis to the order of operations
              /// </summary>
              private void uxOpenParenth_Click(object sender, EventArgs e)
              {
                     this._input = false;
                     this._operators.Push('(');
              }
 
              /// <summary>
              /// Creates a close parenthesis to the order of operations
              /// </summary>
              private void uxCloseParenth_Click(object sender, EventArgs e)
              {
                     this._input = false;
                     this.uxSum.Text = this.CloseParenth(Double.Parse(this.uxSum.Text)).ToString();
              }
 
              /// <summary>
              /// Will close a set of parenthesis
              /// </summary>
              /// <param name="op">Current input</param>
              /// <returns>Calculated number</returns>
              private double CloseParenth(double op)
              {
                     double number = this.Calculate(this.GetPrecedence('('), op);
                     if (this._operators.Count > 0)
                            this._operators.Pop();
                     return number;
              }
    }
}