An Advanced Calculator in C# 5



The .Net Framework provides a Math class with a loads of mathematical functions like Sin, Cos, Tan, ASin, ACos, ATan, Floor, Ceiling, Power, Log, Ln etc. We can easily use them by simply referring to the Math functions. These Math class functions take suitable parameters and return appropriate datatypes which can be easily changed to Strings or Doubles. The conversion here done is with the help of Convert class.


This is just another basic piece of code which illustrates a different approach to the simple calculator which we use daily. The calculator is designed to do 7 basic calculations like +,-,*,/,sin,cos,tan. Of course there is no limit to the ways in which we can combine these and create even more complex formulas. The calci stores different calculations in different textboxes , also showing the operand/s . This is simply achieved by concatenating the operands retrieved from textboxes with the appropriate operation sign in between and then appending the calculated result in the end after an = sign. A word of caution : C# asks programmers to be strict with the datatypes so we have to convert and match existing datatypes to perform operations especially mathematical. In other words we cannot apply the * operation on two strings , instead we have to convert the strings to integers and then apply the operation. This is simply achieved by using the function Convert.ToInt32() which converts its parameter into a 32 bit integer value(using 16 instead of 32 would convert it to a 16 bit integer value which also has a smaller range as compared to 32 bit).

using System;

namespace Advanced_Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Addition_Click(object sender, EventArgs e)
        {
            txtAddition.Text = txtX.Text + " + " + txtY.Text + " = " + Convert.ToString(Convert.ToInt32((Convert.ToInt32(txtX.Text))+(Convert.ToInt32(txtY.Text))));
        }

        private void Subtraction_Click(object sender, EventArgs e)
        {
            txtSubtraction.Text = txtX.Text + " - " + txtY.Text + " = " + Convert.ToString(Convert.ToInt32((Convert.ToInt32(txtX.Text)) - (Convert.ToInt32(txtY.Text))));

        }

        private void Multiplication_Click(object sender, EventArgs e)
        {
            txtMultiplication.Text = txtX.Text + " * " + txtY.Text + " = " + Convert.ToString(Convert.ToInt32((Convert.ToInt32(txtX.Text)) * (Convert.ToInt32(txtY.Text))));

        }

        private void Division_Click(object sender, EventArgs e)
        {
            txtDivision.Text = txtX.Text + " / " + txtY.Text + " = " + Convert.ToString(Convert.ToInt32((Convert.ToInt32(txtX.Text)) / (Convert.ToInt32(txtY.Text))));

        }

        private void SinX_Click(object sender, EventArgs e)
        {
            txtSinX.Text = " Sin " +txtX.Text +  " = " + Convert.ToString(Math.Sin(Convert.ToDouble(txtX.Text)));
        }

        private void CosX_Click(object sender, EventArgs e)
        {
            txtCosX.Text = " Cos " + txtX.Text + " = " + Convert.ToString(Math.Cos(Convert.ToDouble(txtX.Text)));
        }

        private void TanX_Click(object sender, EventArgs e)
        {
            txtTanX.Text = " Tan " + txtX.Text + " = " + Convert.ToString(Math.Tan(Convert.ToDouble(txtX.Text)));
        }
    }
}
Please Note :
** Do not Copy & Paste code written here ; instead type it in your Development Environment
** Testing done in .Net Framework 4.5 but code should be very similar for previous versions of .Net
** All Program Codes here are  100%  tested & running.