Calculator

Developed a functional calculator application using Visual Studio, featuring a clean interface and reliable arithmetic operations.


Project type: Individual

Tools used

C#

C#

CSS

Visual Studio

Normal/Scientific Mode

Normal mode contains the 4 main operations: plus, minus, multiply, divide, while scientific mode contains more functions such as logarithms, square root, trigonometric functions etc.  

normal Normal
scientificScientific

Off/On button

To off the calculator, the shift button is pressed, followed by the AC. This will render all buttons inactive and nothing will happen if the user presses them.

To turn on the calculator, the user would simply just press the 'ON' button.

Character limit

Once the number fills all the available space in the window, a popup will alert the user that the character limit has been reached.

This prevents accidental key presses that the user may not notice from being included in the calculations.

character_limit

Sound effects

​To simulate as close to a real calculator, I have added sounds when the user presses any buttons, and a voice-over when the 'AC' button is pressed.

​ There is an option to disable the voiceover should it get disruptive in the long run.

(Click on the sound option below!)

The Backbone

Beyond the interface, this calculator relies on clear, structured logic. The following snippets show how I handled input processing, arithmetic operations, and display updates.

Character limit

This is essential for a calculator because, without an input limit, users could enter infinitely long numbers. I initially overlooked this issue, but correcting it ensures the calculator produces valid results. Through this, I've learnt to anticipate user actions, implement quality assurance measures and design more reliable applications.

if (txtResults.Text.Length == 15)
{
    MessageBox.Show("Character limit reached!");
    return;
}

Keyboard detection

The calculator can detect inputs from the keyboard, allowing users to type numbers and operations directly instead of clicking each button manually. This not only improves efficiency but also enhances the user experience by providing flexibility in how the calculator can be used. Through this feature, I strengthened my skills in event handling, user interface design, and enhancing usability, allowing user experience to be simpler and efficient.

private void Form1_Keys(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.D0:
            btn0.PerformClick();
            break;
        case Keys.D1:
            btn1.PerformClick();
            break;
        case Keys.D2:
            btn2.PerformClick();
            break;
        case Keys.D3:
            btn3.PerformClick();
            break;
        case Keys.D4:
            btn4.PerformClick();
            break;
        case Keys.D5 when !e.Shift:
            btn5.PerformClick();
            break;
        case Keys.D6:
            btn6.PerformClick();
      

Button toggle

To make the calculator toggle between scientific and radians using the same button, I made use of the modulus operator. The first time the button is pressed, the remainder will be 1, and the second time pressed, it will be 0. The remainder only toggles between 0 and 1 no matter how many time it is pressed.

private void degRadBtn_Click(object sender, EventArgs e)
{
    PlaySound();
    System.Windows.Forms.Button btn = (System.Windows.Forms.Button)sender;
    counterdegrad++;
    if (counterdegrad % 2 != 0)
    {
        degRadBtn.Text = "DEG";
        degraLbl.Text = "RAD";
    }
    else
    {
        degRadBtn.Text = "RAD";
        degraLbl.Text = "DEG";
    }
}

Full Equation Display

Through the use of the tag/text attached with the buttons, I made it possible to display the full equation so that the user is able to see what they have calculated from start to finish. This is implemented for all buttons, and below is an example of how it is coded for all buttons.

@txtEqu.Text += btn.Text;

Equation

Final Thoughts

Lessons Learned and Insights Gained: Challenges, Skills, and Growth from This Project

Lessons learnt

Skills demonstrated