Share via


Visual Studio 2015 : Advanced Calculator

Preface

In this chapter we want  to write advanced Calculator program .

Process

The following steps show how to write a advanced Calculator:

1. First, Click New Project in Start Page Or On File Menu .

2. In New Project Dialog , Click Windows On Left Pane And Windows Forms Application On Middle Pane .

3. Change forms layout to this Mode :

4. Now Declare This Public Variables :

 decimal num1;

        decimal num2;

        string operation;

5. Then Add This Method for Number Buttons :

        private void input(string a)

        {

            if (textBox1.Text == "0")

                textBox1.Text = a;

            else

                textBox1.Text += a;

        }

6. Add This Code In Number Buttons(Click Event) :

     input("(Number Of Button)");

7. Now Add This Code For Operation Buttons(+,-,/,*,^ And Mod(%)) :

     num1 = decimal.Parse(textBox1.Text);

     operation = "(Operation For Example "+")";

     textBox1.Text = "0";

8.Then Add  This Code For Calculation Button(=) :

            num2 = decimal.Parse(textBox1.Text);

            ////////////////////////////////

            switch (operation)

            {

                case "+":

                    textBox1.Text = (num1 + num2).ToString();

                    break;

                case "-":

                    textBox1.Text = (num1 - num2).ToString();

                    break;

                case "*":

                    textBox1.Text = (num1 * num2).ToString();

                    break;

                case "/":

                    textBox1.Text = (num1 / num2).ToString();

                    break;

                case "^":

                    textBox1.Text = (int.Parse(num1.ToString()) ^ int.Parse(num2.ToString())).ToString();

                    break;

                case "%":

                    textBox1.Text = (num1 % num2).ToString();

                    break;

            }

9. Now Add This Code For Cosine Button :

            textBox1.Text = (Math.Cos(double.Parse(textBox1.Text))).ToString();

10. Then Add This Code For Sinus Button :

            textBox1.Text = (Math.Sin(double.Parse(textBox1.Text))).ToString();

11. Now Add This Code For Tangent Button :

            textBox1.Text = (Math.Tan(double.Parse(textBox1.Text))).ToString();

12. Then Add This Code For Logarithm Button :

            textBox1.Text = (Math.Log(double.Parse(textBox1.Text))).ToString();

13. Now Add This Code For Factorial Button :

            long f = 1;

            for (long i = 1; i <= long.Parse(textBox1.Text); i++)

            {

                f = f * i;

            }

            textBox1.Text = f.ToString();

14. Then Add This Code For Radical Button :

            textBox1.Text = (Math.Sqrt(double.Parse(textBox1.Text))).ToString();

good luck   !