Monday, 21 April 2014

My First C# Application - SimpleCalculator

Here i share with all, my first windows forms application.

A Simple Calculator

First step in created this application is , Creating a Winforms design.
Below find the snapshot of the design.

Forms Design

Done with the Design ???

     Next up , Register to all the button click events.
Once done with all these, let's dig into the functionality.

Key Modules/ Functions

 1. CallValidate

 2. ValidateKey

 3.  OperandTrack

 4.  Evaluate

 

CallValidate


  /// <summary>
        /// Detects the key or button input, and performs the appropriate operation
        /// </summary>
        /// <param name="ch">The character being inputted through keyboard or GUI button </param>
        /// <param name="flag">performs different Operations based on the Flag Value
        /// Flag=0 validates the keypressed from keyboard
        /// flag=1:aappends the numbers to the stringBuilder
        /// flag=2: Backspace Operation
        /// Flag=3:Dot operation
        /// flag=4:Xor OPeration for +/- button
        /// Flag=5:Clear Operation </param>
        /// <param name="t">Reference to the form TextBox </param>
        /// <param name="myString">reference to the string Builder </param>

        public void CallValidate(char ch, int flag, TextBox t)
        {
            switch (flag)
            {
                case 0: if (ValidateKey(ch, t))
                        {
                            if (eflag == 1)
                            {
                                oprCount = 0;
                            }
                            myString += ch;
                            t.Text =myString;
                            consecutiveCount = 0;
                        }
                        break;

                case 1: if (eflag == 1)
                            oprCount = 0;
                        myString += ch;
                        t.Text = myString;
                        consecutiveCount = 0;
                        break;

                case 2: if (myString.Length > 0)
                        {
                            myString=myString.Remove(myString.Length-1);
                            t.Text =myString;
                            if (myString.Length < x)
                            {
                                x = 0;
                            }
                            if (t.Text.Length == 0)
                                t.Text = "0";
                        }
                        else if (t.Text.Length == 0)
                            t.Text = "0";
                      
                        break;
                 
                  
                case 3: if (x == 0)
                        {
                            myString += ch;
                            t.Text = myString;
                            consecutiveCount = 0;
                            x=myString.Length;
                        }
                        break;

                case 4: res = float.Parse(t.Text) * (-1);
                        t.Text = res.ToString();
                        prev = res;
                        break;

                case 5: t.Text = "0";
                        myString = "";
                        consecutiveCount = 0;
                        next = 0;
                        oprCount = 0;
                        prev = 0;
                        res = 0;
                        x = 0;
                        break;

            }
        }


ValidateKey


 /* Checks the keypressed and acts according to the key
        diffrentiates btwn alphabets,numeric and  specaial keys(*,+,-,/) etc*/

        public bool ValidateKey(char ch, TextBox t)
        {
             switch (ch)
            {
               /* case '1': return true;
                case '2': return true;
                case '3': return true;
                case '4': return true;
                case '5': return true;
                case '6': return true;
                case '7': return true;
                case '8': return true;
                case '9': return true;
                case '0': return true;*/

                case '+': eflag = 0;
                    OperandTrack(ch, t);
                    return false;

                case '-': eflag = 0; OperandTrack(ch, t);
                    return false;

                case '*': eflag = 0; OperandTrack(ch, t); 
                    return false;

                case '/': eflag = 0; OperandTrack(ch, t);
                    return false;

                case '=':eflag = 1;
                    OperandTrack(ch, t);
                    if (consecutiveCount >= 2)
                    {
                        consecutiveCount++;
                        if (Evaluate(res, opr, next, t))
                        {
                            prev = res;
                            t.Text = res.ToString();
                        }
                    }
                        return false;
             }
            return false;
        }


OperandTrack



         /// <summary>

        /// This function keeps track of the prev and next operands,

        /// The operator value on which the operation has to be performed

        /// 

        /// if there is 2 Distinct operators then Evaluate the result

        /// Ex:2+3+ OR 2+3= 

        /// 

        /// if there there was only a single operator with a single
        /// operand, tracks the prev operand and the operator
        /// Ex: 2* or 0+ etc 
        /// 
        /// if operators are pressed multiple times , get the current
        /// operator neglecting the previous operator
        /// Ex: 2*+ , the * is neglected and + is considered
        /// 
        /// </summary>
        /// <param name="e"></param>
        /// <param name="textBox1"></param>
        /// <param name="myString"></param>

        public void OperandTrack(char ch, TextBox textBox1)
        {
            MessageBox.Show("");
            oprCount++;            
            consecutiveCount++;
            x = 0;
            if (oprCount == 2&&consecutiveCount<=1)
            {
                eflag = 0;
                next = float.Parse(textBox1.Text);   //Sets the second operand from the textbox
                if (Evaluate(prev, opr, next, textBox1))
                {
                   prev = res; // Sets the result as the prev operand value
                    textBox1.Text = res.ToString(); // Displays the result to the Form
                    oprCount = 1;
                    if (ch != '=')
                        opr = ch;
                    myString = "";
                    //textBox1.Text = "";
                }
            }           
            else if (oprCount == 1)
            {
                opr = ch;
                prev =float.Parse(textBox1.Text);
                myString="";
                //textBox1.Text = "";
            }

            else
            {
                --oprCount;
                if(ch!='=')
                    opr = ch;
                myString="";
               // textBox1.Text = "";
            }
        }



Evaluate



/* Evaluates the Expression and Returns the Result */

        public bool Evaluate(float x, float opr, float y,TextBox t)
        {
            
            if (opr == '+')
                 res = x + y;
            
            else if (opr == '-')
                  res = x - y;

            else if (opr == '*')
                res = x * y;

            else if (opr == '/')
                    {
                        if (y == 0)
                        {
                            t.Text = "divide by zero error";
                            return false;
                        }
                        else
                            res = x / y;
                    }
            else if(opr==61)
                res = float.Parse(t.Text);
            return true;
        }
    }

Finally Call the functions through Button click events.

Declare the ASCII constants for backspace and Delete buttons.

        public const char back = '\u0008';
        public const char del = '\u007F';

Function Calls.


         private void Form1_Load(object sender, EventArgs e)
        {
            this.Focus();
        }

        private void button19_Click(object sender, EventArgs e)
        {
            k.CallValidate(back, 2, textBox1);
        }

        private void button20_Click(object sender, EventArgs e)
        {
            k.CallValidate('`', 5, textBox1);
        }
         
        private void button18_Click(object sender, EventArgs e)
        {
           k.CallValidate('=', 0, textBox1);
        }
           
        private void button17_Click(object sender, EventArgs e)
        {
            k.CallValidate(char.Parse(button17.Text), 0, textBox1);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            k.CallValidate(char.Parse(button16.Text), 0, textBox1);
        }

        private void button15_Click(object sender, EventArgs e)
        {
            k.CallValidate(char.Parse(button15.Text), 0, textBox1);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            k.CallValidate(char.Parse(button14.Text), 0, textBox1);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            k.CallValidate('.', 3, textBox1);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            k.CallValidate('-', 4, textBox1);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            k.CallValidate('0', 1, textBox1);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            k.CallValidate('9', 1, textBox1);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            k.CallValidate('8', 1, textBox1);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            k.CallValidate('7', 1, textBox1);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            k.CallValidate('6', 1, textBox1);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            k.CallValidate('5', 1, textBox1);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            k.CallValidate('4', 1, textBox1);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            k.CallValidate('3', 1, textBox1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            k.CallValidate('2', 1, textBox1);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            k.CallValidate('1', 1,textBox1);
     
        }


/// To differntiate special char's ,numbers and alphabets.
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar))
                k.CallValidate(e.KeyChar, 1, textBox1);
            else if (e.KeyChar == '.')
                k.CallValidate(e.KeyChar, 3, textBox1);
            else if (e.KeyChar == back)
                k.CallValidate(e.KeyChar, 2, textBox1);
            else
                k.CallValidate(e.KeyChar, 0, textBox1);
 
        }

Find the link to the Application here

No comments:

Post a Comment