Design a Calculator Using C# code.

Design a Calculator 




What you will need:
  • A computer running Windows 7 or higher
  • Visual Studio: Any version
              Download: http://www.visualstudio.com
               
I will be using the 2010 version of Visual Studio in this tutorial, but every version is pretty similar.

Step 1: Creating a New Project





Step 2: Designing the Application


 
 
 
 

Step 3: Creating Event Handlers


3.1. Double click a button.  This will bring you to the "Form1.cs" tab.  This is where all of your code will be written.  Notice a section has been pre-written for you based off of the "Click" event.  This section of code will be executed when that specific button is clicked.  For the sake of this project, we will call these Event Handlers.

3.2. Double click all of your buttons so you have an event handler for each button (use the tabs to switch back and forth).

3.3. Click Save All and move on to step 4.


Step 4: Declaring Variables

Variables are declared in the following form: <Data Type> <Variable Name> = <Initial Value>;
Ex: To declare a new char initialized to contain 'c', the code would look like this: char mycharacter = 'c';

4.1. Declare 5 new variables above the Form1() method as shown in the image above.  This will create the needed variables to store and process the input from the user.

4.2. Click the Save All button and move on to the next step when you are ready.


Step 5: Recording User Input

The shell is set up and ready to go.  But in order for a user to interact with the program, we need to store the data given from the user clicking the buttons.

5.1. Type the line input += "0"; between the curly braces for the zero_Click event handler.  This will add a zero to the input character string.

*Note: Each time the user clicks the zero button, another zero will be added to the input string.  One click will have the string contain one zero like so: "0".  Clicking the zero button again will add another making the string value "00", and so on.

**Note: += is a shortcut for writing input = input + "0";  Essentially, it is adding a zero to the end of what is already existing in the string.

5.2. Repeat step 5.1 for all the numerical input event handlers, as well as the decimal button event handler.  Change the "0" value to match which button handler you are in.  (Don't want to add a 0 to the string when a 1 is pressed).

5.3. Next add the following lines of code for each of your operand button event handlers (+,-,*,/).
            operand1 = input;
            operation = '+';
            input = string.Empty;

This code will store the contents of the input string into the operand1 string, and set the operation character accordingly.  The input string gets cleared at the end so it can be ready to record the next value from the user.

5.4. Repeat step 5.4 for all operand input event handlers.

5.3. Click the Save All button and move on to step 6.

Step 6: Viewing Input


6.1. Add the line this.textBox1.Text += input; underneath the input line as shown.  This will add the input to the TextBox that was dragged to the screen earlier.  

6.2. Repeat this step for every numerical input handler.

6.3. Next, add the line this.textBox1.Text = ""; before the input line.  This is essential because it clears the TextBox before the input string is added to it.  Forgetting this step will leave the user with a mess on the display (feel free to try it out by running the code without this step).

6.4. Run the program and see what happens!

6.5. Click Save All and move on to step 7.

Refer this code:

private void eight_Click(object sender, EventArgs e)
        {
            this.textbox1.Text = "";
            input += "8";
            this.textbox1.Text += input;
        }


Step 7: Clearing Input

From the last step, you have probably noticed that we can't  clear the input off the screen.  This step will fix that issue!

7.1.  Add the following lines of code in the clear button event handler.
            this.textBox1.Text = "";
            this.input = string.Empty;
            this.operand1 = string.Empty;
            this.operand2 = string.Empty;

Step 8: Calculating Output

8.1. Insert the following code into the equals button event handler.  (Try not to just paste it, try and understand what is happening).

            operand2 = input;
            double num1, num2;
            double.TryParse(operand1, out num1);
            double.TryParse(operand2, out num2);

            if (operation == '+')
            {
                result = num1 + num2;
                textBox1.Text = result.ToString();
            }
            else if (operation == '-')
            {
                result = num1 - num2;
                textBox1.Text = result.ToString();
            }
            else if (operation == '*')
            {
                result = num1 * num2;
                textBox1.Text = result.ToString();
            }
            else if (operation == '/')
            {
                if (num2 != 0)
                {
                    result = num1 / num2;
                    textBox1.Text = result.ToString();
                }
                else
                {
                    textBox1.Text = "DIV/Zero!";
                }

            }


STEP 9: FORM View



HERE Full CODE:::: 

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 Calculator
{
    public partial class Form1 : Form
    {
        string input = "";
        string operand1 = string.Empty;
        string operand2 = string.Empty;
        char operation;
        double result = 0.0;
        public Form1()
        {
            InitializeComponent();
        }


        private void zero_Click(object sender, EventArgs e)
        {
            this.textbox1.Text = "";
            input += "0";
            this.textbox1.Text += input;


        }

        private void one_Click(object sender, EventArgs e)
        {
            this.textbox1.Text = "";
            input += "1";
            this.textbox1.Text += input;
        }

        private void two_Click(object sender, EventArgs e)
        {
            this.textbox1.Text = "";
            input += "2";
            this.textbox1.Text += input;
        }

        private void three_Click(object sender, EventArgs e)
        {
            this.textbox1.Text = "";
            input += "3";
            this.textbox1.Text += input;
        }

        private void four_Click(object sender, EventArgs e)
        {
            this.textbox1.Text = "";
            input += "4";
            this.textbox1.Text += input;
        }

        private void five_Click(object sender, EventArgs e)
        {
            this.textbox1.Text = "";
            input += "5";
            this.textbox1.Text += input;
        }

        private void six_Click(object sender, EventArgs e)
        {
            this.textbox1.Text = "";
            input += "6";
            this.textbox1.Text += input;
        }

        private void seven_Click(object sender, EventArgs e)
        {
            this.textbox1.Text = "";
            input += "7";
            this.textbox1.Text += input;
        }

        private void eight_Click(object sender, EventArgs e)
        {
            this.textbox1.Text = "";
            input += "8";
            this.textbox1.Text += input;
        }

        private void nine_Click(object sender, EventArgs e)
        {
            this.textbox1.Text = "";
            input += "9";
            this.textbox1.Text += input;
        }

        private void add_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = '+';
            input = string.Empty;
        }

        private void sub_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = '-';
            input = string.Empty;
        }

        private void mux_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = '*';
            input = string.Empty;
        }

        private void div_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = '/';
            input = string.Empty;
        }

        private void equal_Click(object sender, EventArgs e)
        {
            operand2 = input;
            double num1, num2;
            double.TryParse(operand1, out num1);
            double.TryParse(operand2, out num2);

            if (operation == '+')
            {
                result = num1 + num2;
                textbox1.Text = result.ToString();
            }
            else if (operation == '-')
            {
                result = num1 - num2;
                textbox1.Text = result.ToString();
            }
            else if (operation == '*')
            {
                result = num1 * num2;
                textbox1.Text = result.ToString();
            }
            else if (operation == '/')
            {
                if (num2 != 0)
                {
                    result = num1 / num2;
                    textbox1.Text = result.ToString();
                }
                else
                {
                    textbox1.Text = "DIV/Zero!";
                }

            }
        }

        private void clear_Click(object sender, EventArgs e)
        {
            this.textbox1.Text = "";
            this.input = string.Empty;
            this.operand1 = string.Empty;
            this.operand2 = string.Empty;
        }

        private void textbox1_TextChanged(object sender, EventArgs e)
        {

        }      
    }

}


Any Help: Comment Below

Thanks for VISIT

Comments

Popular posts from this blog

CONVERSION INTO BINARY FROM DECIMAL, HEXA, OCTAL NUMBERS

Insertion Sorting in C Language.