Tuesday, September 11, 2018

Simple Calculator - C#

Today I'll be showing the first of three little C# projects I've worked on while waiting for the necessary parts to finish the airplane. This one in particular is a scientific calculator I made using the Windows Forms App in .NET. Here's a picture of the calculator in all its glory:
As can be seen in the image, the calculator can perform the basic arithmetic operations along with evaluating trig functions, logs, etc. The first step I took in building this calculator was adding a bunch of buttons onto the Windows Forms App (and adjusting the font to something beautiful, like Times New Roman). After that I began coding all the numerical buttons to add a number into the text box when pressed, using the following if else statement:
private void zero_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "0";
            }
            else textBox1.Text += "0";
        }
The example above is for the number zero, so I replaced the "0's" with the other buttons' corresponding numbers. Next, I added the following code for the decimal button to ensure that it cannot be used more than once in a single number:
private void dot_Click(object sender, EventArgs e)
        {
            if (!textBox1.Text.Contains("."))
            {
                textBox1.Text += ".";
            }
        }
Once that was out of the way, it was time to begin working on the operations and functions. For the main operators I first added some code whereby the operator would be applied to the first inputted number and wait for a second number to be written before evaluating the expression. As an example, here is what the addition operator looks like:
 private void addition_Click(object sender, EventArgs e)
        {
            num1 = decimal.Parse(textBox1.Text);
            operation = "+";
            textBox1.Text = "0";
        }
These operations will later be evaluated once the Evaluate, or "=" button is pressed. Before that, I implemented the logic that would solve the different trig functions, log functions, and other good stuff like that. The code for most of these was really simple since C# already supports the evaluation of many of these functions. As an example, here is the sine function:

private void sin_Click(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(Math.Sin(double.Parse(textBox1.Text)));
        }
I also added a few constants like 𝛑 and e, along with a factorial function for integer values (basically just a for loop until the factorial is calculated):

private void factorial_Click(object sender, EventArgs e)
        {
            long f = 1;

                for (long i = 1; i <= long.Parse(textBox1.Text); i++)
                {
                    f = f * i;
                }
                textBox1.Text = Convert.ToString(f);
        }
Finally, I coded a clear button to clear away all inputs and got the Evaluate button to calculate all basic operations. This mostly involved adding, subtracting, multiplying, or dividing the two input numbers before converting the result to a string and printing it in the text box. I also had to account for some possible errors that could arise from dividing by 0 or raising 0 to the 0th power, along with converting the inputs from decimals to integers so that exponentiation could take place:

private void evaluate_Click(object sender, EventArgs e)
        {
            num2 = decimal.Parse(textBox1.Text);

            switch (operation)
            {
                case "+":
                    textBox1.Text = Convert.ToString(num1 + num2);
                    break;

                case "-":
                    textBox1.Text = Convert.ToString(num1 - num2);
                    break;

                case "*":
                    textBox1.Text = Convert.ToString(num1 * num2);
                    break;

                case "/":
                    if (num2 != 0)
                    {
                        textBox1.Text = Convert.ToString(num1 / num2);
                    }
                    else
                    {
                        textBox1.Text = "NaN";
                    }
                    break;

                case "^":
                    textBox1.Text = Convert.ToString(int.Parse(Convert.ToString(num1)) ^ int.Parse(Convert.ToString(num2)));
                    if(num1 == 0 & num2 == 0)
                    {
                        textBox1.Text = "1";
                    }
                    break;
            }
And that's all there is to it! There are still some errors messages that pop up if I try to use the factorial on a non-integer value, but I will try and fix it in the future by implementing the gamma function in place of my current factorial code so that those inputs can still be utilized. Tomorrow I will show the next of my C# projects, and below I will be all the code for this calculator in case you are interested:
using System;
using System.Windows.Forms;

namespace calculator
{
    public partial class Form1 : Form
    {
        private decimal num1;
        private decimal num2;
        string operation;

        public Form1()
        {
            InitializeComponent();
        }

        private void zero_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "0";
            }
            else textBox1.Text += "0";
        }

        private void one_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "1";
            }
            else textBox1.Text += "1";
        }

        private void two_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "2";
            }
            else textBox1.Text += "2";
        }

        private void three_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "3";
            }
            else textBox1.Text += "3";
        }

        private void four_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "4";
            }
            else textBox1.Text += "4";
        }

        private void five_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "5";
            }
            else textBox1.Text += "5";
        }

        private void six_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "6";
            }
            else textBox1.Text += "6";
        }

        private void seven_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "7";
            }
            else textBox1.Text += "7";
        }

        private void eight_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "8";
            }
            else textBox1.Text += "8";
        }

        private void nine_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "9";
            }
            else textBox1.Text += "9";
        }

        private void dot_Click(object sender, EventArgs e)
        {
            if (!textBox1.Text.Contains("."))
            {
                textBox1.Text += ".";
            }
        }

        private void addition_Click(object sender, EventArgs e)
        {
            num1 = decimal.Parse(textBox1.Text);
            operation = "+";
            textBox1.Text = "0";
        }

        private void subtraction_Click(object sender, EventArgs e)
        {
            num1 = decimal.Parse(textBox1.Text);
            operation = "-";
            textBox1.Text = "0";
        }

        private void multiplication_Click(object sender, EventArgs e)
        {
            num1 = decimal.Parse(textBox1.Text);
            operation = "*";
            textBox1.Text = "0";
        }

        private void division_Click(object sender, EventArgs e)
        {
            num1 = decimal.Parse(textBox1.Text);
            operation = "/";
            textBox1.Text = "0";
        }

        private void sin_Click(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(Math.Sin(double.Parse(textBox1.Text)));
        }

        private void cos_Click(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(Math.Cos(double.Parse(textBox1.Text)));
        }

        private void tan_Click(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(Math.Tan(double.Parse(textBox1.Text)));
        }

        private void arcsin_Click(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(Math.Asin(double.Parse(textBox1.Text)));
        }

        private void arccos_Click(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(Math.Acos(double.Parse(textBox1.Text)));
        }

        private void arctan_Click(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(Math.Atan(double.Parse(textBox1.Text)));
        }

        private void tenPower_Click(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(Math.Pow(10, double.Parse(textBox1.Text)));
        }

        private void log_Click(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(Math.Log10(double.Parse(textBox1.Text)));
        }

        private void pi_Click(object sender, EventArgs e)
        {
            double pi = Math.PI;
            textBox1.Text = Convert.ToString(pi);
        }

        private void ePower_Click(object sender, EventArgs e)
        {
            double E = Math.E;
            textBox1.Text = Convert.ToString(Math.Pow(E, double.Parse(textBox1.Text)));
        }

        private void lnx_Click(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(Math.Log(double.Parse(textBox1.Text)));
        }

        private void factorial_Click(object sender, EventArgs e)
        {
            long f = 1;

                for (long i = 1; i <= long.Parse(textBox1.Text); i++)
                {
                    f = f * i;
                }
                textBox1.Text = Convert.ToString(f);
        }
        
        private void xSquared_Click(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(Math.Pow(double.Parse(textBox1.Text), 2));
        }

        private void xPowerY_Click(object sender, EventArgs e)
        {
            num1 = decimal.Parse(textBox1.Text);
            operation = "^";
            textBox1.Text = "0";
        }

        private void sqrt_Click(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(Math.Sqrt(double.Parse(textBox1.Text)));
        }

        private void clear_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            operation = string.Empty;
        }

        private void evaluate_Click(object sender, EventArgs e)
        {
            num2 = decimal.Parse(textBox1.Text);

            switch (operation)
            {
                case "+":
                    textBox1.Text = Convert.ToString(num1 + num2);
                    break;

                case "-":
                    textBox1.Text = Convert.ToString(num1 - num2);
                    break;

                case "*":
                    textBox1.Text = Convert.ToString(num1 * num2);
                    break;

                case "/":
                    if (num2 != 0)
                    {
                        textBox1.Text = Convert.ToString(num1 / num2);
                    }
                    else
                    {
                        textBox1.Text = "NaN";
                    }
                    break;

                case "^":
                    textBox1.Text = Convert.ToString(int.Parse(Convert.ToString(num1)) ^ int.Parse(Convert.ToString(num2)));
                    if(num1 == 0 & num2 == 0)
                    {
                        textBox1.Text = "1";
                    }
                    break;
            }
        }
    }
}

No comments:

Post a Comment