// This prgm creates a calculator

// Author: Ahmad Mobasseri

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace CalculousSharp

{

    public partial class Form1 : Form

    {

        private TextBox Operand; //Note how this variable is used to switch between

        //the display text boxes

        public Form1()

        {

            InitializeComponent();

            Operand = Operand0; //initialize it to the first text box

        }

 

        //These are the handlers for the number buttons including the decimal point..

 

        private void Zero_Click(object sender, EventArgs e)

        {

            Operand.Text += "0";

        }

 

        private void One_Click(object sender, EventArgs e)

        {

            Operand.Text += "1";

        }

 

        private void Two_Click(object sender, EventArgs e)

        {

            Operand.Text += "2";

        }

 

        private void Three_Click(object sender, EventArgs e)

        {

            Operand.Text += "3";

        }

 

        private void Four_Click(object sender, EventArgs e)

        {

            Operand.Text += "4";

        }

 

        private void Five_Click(object sender, EventArgs e)

        {

            Operand.Text += "5";

        }

 

        private void Six_Click(object sender, EventArgs e)

        {

            Operand.Text += "6";

        }

 

        private void Seven_Click(object sender, EventArgs e)

        {

            Operand.Text += "7";

        }

 

        private void Eight_Click(object sender, EventArgs e)

        {

            Operand.Text += "8";

        }

 

        private void Nine_Click(object sender, EventArgs e)

        {

            Operand.Text += "9";

        }

 

        private void DecimalPoint_Click(object sender, EventArgs e)

        {

            Operand.Text += ".";

        }

 

        //These member functions are handlers for the operator group..

 

        private void Add_Click(object sender, EventArgs e)

        {

            Operator.Text = "+";

            Operand = Operand1; //Once an operator is entered, we have to switch to the

        }                       //other text box...

 

        private void Subtract_Click(object sender, EventArgs e)

        {

            Operator.Text = "-";

            Operand = Operand1;

        }

 

        //Eval starts the process that interprets what you entered...

 

        private void Evaluate_Click(object sender, EventArgs e)

        {

            Double Opd0 = Double.Parse(Operand0.Text);  //Convert the Operand values to numbers..

            Double Opd1 = Double.Parse(Operand1.Text);

            Double Rslt = 0;

 

            if (Operator.Text == "+")   //Figure what operation is being performed..

                Rslt = Opd0 + Opd1;   //...and perform it...

            else if (Operator.Text == "-")

                Rslt = Opd0 - Opd1;

            else if (Operator.Text == "*")

                Rslt = Opd0 * Opd1;

            else if (Operator.Text == "/")

                Rslt = Opd0 / Opd1;

 

            Result.Text = Rslt.ToString(); //Convert the value back to a string and put it in

            //the appropriate text box...

 

            Operand0.Text = ""; //Clear the display group text boxes...

            Operand1.Text = "";

            Operator.Text = "";

 

            Operand = Operand0; //Reinitialize the variable for the next calculation...

 

        }

 

        private void Multiply_Click(object source, EventArgs e)

        {

            Operator.Text = "*";

            Operand = Operand1;

        }

 

        private void Divide_Click(object sender, EventArgs e)

        {

            Operator.Text = "/";

            Operand = Operand1;

        }

    }

}