Project 1 — Calculator

Requirements (100 points)

Your task is to develop a basic calculator that operates on real numbers.

User Interface

The GUI should contain:

The buttons should be custom controls that 1) become a different color when hovered over, and 2) become yet another color while being pressed.

Functionality

Your calculator's behavior should pretty much behave like the Windows Calculator in Standard mode. Note that in Scientific mode the order of buttons pressed and evaluation is a little different. In Standard mode, the order that operations are evaluated is that in which they are entered. For example, if the users presses "3", "+", "2", and "*", the display gets the value of the first operation (3+2). Subsequently pressing "4" and "=" results in the running total (4) getting multiplied by 4, yielding 20. If multiple operators are input in succession, use the last one for computation.

Whereas the Windows Calculator will repeatedly perform the last operation if "=" is pressed consecutively, you are not required to; you can simply ignore subseqent "=" operations. Other behaviors you can ignore are that of pressing "+/-" or "=" immediatiately after an operator; simply ignore these.

For simplicity, don't worry about the length of the number in the display; you can allow the total to be become arbitrarily large and you can ignore arithmetic overflow issues.

You are required to deal with the leading 0 appropriately and have exactly one decimal point in the display at all times. These seemingly simple requirements, in addition to the need to support running computations (multiple binary operators applied in succession), lead to a fair number of possible states the program can be in. To assist with this design, here is a finite state machine that you may want to model in your implementation. It is good practice to think through a state diagram like this for all GUI application development. Once you have thought through all possible state transitions, it becomes much easier to implement, understand, and debug the logic.

Extra Credit (10 points)

Keyboard Input (5 points)

Keyboard input should be recognized for each button with the exception of negation (+/-) since their is no obvious keyboard equivalent. In good style, these key event handlers (as well as the button handlers) should all be very short, since the main logic should be separated in other methods. If you've attached a KeyDown event to the form but it doesn't seem to be working, the likely problem is that the event is getting sent to whichever button is in focus. To get around this, set the KeyPreview property of the form to true, which alert's the form control of the event before any other control. Since you will likely take care of all necessary functionality directly from the form's key event handler, it is good practice to prevent the event from getting sent to any other control; you can accomplish this in the handler with the statement: e.Handled = true;

Backspace Button (5 points)

Add a backspace button that erases the last digit in the display. Deal with "0" and "." sensically. If a binary operator or "=" has just been pressed, backspace should have no effect.

Sample

Here is a sample calculator. This is provided only to provide an example of approximately what you are expected to implement. You have freedom to design the UI as you see fit. Furthermore, this is not necessarily bug-free. You should implement the specification, not mimic the sample.