Pages

Tuesday, June 9, 2015

Visual Calculator in C# Using Classes




System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 
namespace visual_calculator
{
    class Methods
    {
        public  int Factorial(int input)
        {
            int answer = 0;
            int count = 0;
            if (input > 0)
            {
                count = 1;
                while (count <= input)
                {
                    if (count == 1)
                    {
                        answer = 1;
                        count++;
                    }
                    else
                    {
                        answer = count * answer;
                        count++;
                    }
                }
            }
            else
            {
                MessageBox.Show("Please enter only a positive integer.");
            }

            return answer;
        }
        public  void toBin(int dec)
        {
            int count = 0;
            int[] bin = new int[100];

            while (dec > 0)
            {
                bin[count] = dec % 2;    //store Binary value in array bin
                count++;
                dec = dec / 2;  //for moving from unit to ten's or next
            }
            for (int j = count - 1; j >= 0; j--)//for loop for printing the array elements in reverse

                Console.Write(bin[j]);
        }
        //--------------------------------------------------------------------------------------------------------
        public  void toOct(int dec)
        {
            int count = 0;
            int[] oct = new int[100];

            while (dec > 0)
            {
                oct[count] = dec % 8;    //store octal in oct array
                count++;
                dec = dec / 8;           //for moving from unit to ten's or next
            }
            for (int j = count - 1; j >= 0; j--)//for loop for printing the array elements in reverse
            {
                Console.Write(oct[j]);
            }
        }

        //--------------------------------------------------------------------------------------------------------
        public  int toHex(int dec)
        {
            int count = 0;
            int[] hex = new int[100];
            while (dec > 0)
            {
                hex[count] = dec % 16; //store stuff in array hex
                count++;
                dec = dec / 16;
            }
            for (int j = count - 1; j >= 0; j--)    //for loop for printing the array elements in reverse
                if (hex[j] < 10)                //if array element is less than 10 then printing the array element
                {
                    Console.Write(hex[j]);
                }
                else
                    switch (hex[j])   //if array element is greater than 10 then replace 10 with A uptill 15 which is F
                    {
                        case 10:
                            Console.Write('A');
                            break;
                        case 11:
                            Console.Write('B');
                            break;
                        case 12:
                            Console.Write('C');
                            break;
                        case 13:
                            Console.Write('D');
                            break;
                        case 14:
                            Console.Write('E');
                            break;
                        case 15:
                            Console.Write('F');
                            break;
                    }
            return hex[count];
        }
        //------------------------------------------------------------------------------
        public  int BintoDecimal(int des)
        {

            int d;
            double num = 0;
            for (int i = 0; des != 0; i++)
            {
                d = des % 10;
                num += (d) * (Math.Pow(2, i));  //Multiplying number with power of 2 increasing from left to right
                des = des / 10;
            }
            return Convert.ToInt32(num);
        }

        public  void BintoOct(int oct)
        {
            toOct(oct);
        }
        public  void BintoHexa(int oct)
        {
            toHex(oct);
        }
        //-------------------------------------------------------------------------------
        public  int OcttoDes(int des)
        {
            int d;
            double num = 0;
            for (int i = 0; des != 0; i++)
            {
                d = des % 10;
                num += d * (Math.Pow(8, i)); //Multiplying number with power of 8 increasing from left to right
                des = des / 10;
            }
            return Convert.ToInt32(num);
        }
       
        //-------------------------------------------------------------------------------
    }
}

Source code:form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace visual_calculator
{
    public partial class CalculateForm : Form
    {
        int input1,sum=0,sub=0,pro=0,div=0,fact=0,sin=0,cos=0,tan=0,cosec=0,sec=0,cot=0,des,log,oct,pow,tenpow,bin,squ,ans;
        int destohex,mod;
        string hexValue;
        double answer,trig = 0;
        Methods m = new Methods();
        public CalculateForm()
        {
            InitializeComponent();
        }      
      
        private void SqrtButton_Click(object sender, EventArgs e)
        {
            double a=double.Parse(textBox3.Text);
            textBox3.Text = Math.Sqrt(a).ToString();
        }

        private void ReciprocalButton_Click(object sender, EventArgs e)
        {
            float a = 0;
            a = float.Parse(textBox3.Text);
            a = 1 / a;
            textBox3.Text = a.ToString();
        }

        private void PowerButton_Click(object sender, EventArgs e)
        {
            pow = 1;
            input1 = int.Parse(textBox3.Text);
            textBox3.Text = "";           
        }
           private void TenPowButton_Click(object sender, EventArgs e)
        {
                tenpow = 1;
            input1 = int.Parse(textBox3.Text);
            textBox3.Text = "";              
        } 
        private void DivideButton_Click(object sender, EventArgs e)
        {
            div = 1;
            input1 = Convert.ToInt32(textBox3.Text);
            textBox3.Text = "";
        }

        private void MultiplyButton_Click(object sender, EventArgs e)
        {
            pro = 1;
            input1 = Convert.ToInt32(textBox3.Text);
        }

        private void SubtractButton_Click(object sender, EventArgs e)
        {
            sub = 1;
            input1 = Convert.ToInt32(textBox3.Text);
            textBox3.Text = "";
       
            }

        private void AddButton_Click(object sender, EventArgs e)
        {
           
            sum = 1;
           input1= Convert.ToInt32( textBox3.Text); 
            textBox3.Text="";
        }
        private void FactButton_Click(object sender, EventArgs e)
        {
            fact = 1;
            input1 = Convert.ToInt32(textBox3.Text);
        }
        private void ModButton_Click(object sender, EventArgs e)
        {
            mod = 1;
            input1 = Convert.ToInt32(textBox3.Text);
            textBox3.Text = "";

        }
        private void ClearButton_Click(object sender, EventArgs e)
        {
            textBox3.Text = "";
            textBox3.Clear();
        }

        private void ModulasButton_Click(object sender, EventArgs e)
        {
            double a = 0;
            a = double.Parse(textBox3.Text) % double.Parse(textBox3.Text);
           textBox3.Text= a.ToString();
        }
        private void SinButton_Click(object sender, EventArgs e)
        {
            sin = 1;
            trig = Convert.ToDouble(textBox3.Text);
        }     

       private void OneButton_Click(object sender, EventArgs e)
        {
            textBox3.Text +="1";
        }

        private void TwoButton_Click(object sender, EventArgs e)
        {
            textBox3.Text += "2";
        }

        private void ThreeButton_Click(object sender, EventArgs e)
        {
            textBox3.Text += "3";

        }

        private void button21_Click(object sender, EventArgs e)
        {
            textBox3.Text += "4";

        }

        private void button22_Click(object sender, EventArgs e)
        {
            textBox3.Text += "5";

        }

        private void button23_Click(object sender, EventArgs e)
        {
            textBox3.Text += "6";

        }

        private void button12_Click(object sender, EventArgs e)
        {
            textBox3.Text += "7"; 
        }

        private void button13_Click(object sender, EventArgs e)
        {
            textBox3.Text +="8";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            textBox3.Text += "9";
        }

        private void ZeroButton_Click(object sender, EventArgs e)
        {
            textBox3.Text += "0";
        }

        private void AnsButton_Click(object sender, EventArgs e)
        {
        if (sum==1)
            {
                ans = input1 + Convert.ToInt32(textBox3.Text);
                textBox3.Text = ans.ToString();
               
            }
        else if(sub==1)
        {
             ans = input1 - Convert.ToInt32(textBox3.Text);
             textBox3.Text = ans.ToString();

        }
        else if (div == 1)
        {
            ans = input1 / Convert.ToInt32(textBox3.Text);
            textBox3.Text = ans.ToString();
        }
        else if (pro == 1)
        {
            ans = input1 * Convert.ToInt32(textBox3.Text);
            textBox3.Text = ans.ToString();
        }
        else if(mod==1)
        {
            ans = input1 % Convert.ToInt32(textBox3.Text);
            textBox3.Text = ans.ToString();
        }
        else if (fact == 1)
        {
            ans = m.Factorial(int.Parse(textBox3.Text));
            textBox3.Text = ans.ToString();
        }
        else if (pow == 1)
        {
            double a = Convert.ToDouble(input1);
            double b = Convert.ToDouble(textBox3.Text);
            answer= Math.Pow(a,b);
            textBox3.Text = answer.ToString();
        }
        else if(tenpow==1)
        {
            double a=Math.Pow(10,Convert.ToDouble(input1));
            textBox3.Text=a.ToString();
        }
        else if (sin == 1)
        {
            answer = Math.Sin(trig);
            textBox3.Text = answer.ToString();
        }
        else if (cos == 1)
        {
            answer = Math.Cos(trig);
            textBox3.Text = answer.ToString();
        }
        else if (tan == 1)
        {
            answer = Math.Tan(trig);
            textBox3.Text = answer.ToString();
        }
        else if (sec == 1)
        {
            answer = 1/Math.Cos(trig);
            textBox3.Text = answer.ToString();
        }
        else if (cosec == 1)
        {
            answer = 1 / Math.Sin(trig);
            textBox3.Text = answer.ToString();
        }
        else if (cot == 1)
        {
            answer = 1 / Math.Tan(trig);
            textBox3.Text = answer.ToString();
        }
        else if (des == 1)
        {
            textBox3.Text = ans.ToString();
        }
     
        else if (destohex == 1)
        {
            textBox3.Text = hexValue;
        }
        else if (oct == 1)
        {
            textBox3.Text = ans.ToString();
        }
        else if (bin == 1)
        {
            textBox3.Text = hexValue;
        }
        else if (squ == 1)
        {
            double a = Math.Pow(Convert.ToDouble(input1), 2);
            textBox3.Text = a.ToString();
        }
        else if (log == 1)
        {
            double a = Math.Log(trig);
            textBox3.Text = a.ToString();
        }
        }

        private void HexaRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            destohex = 1;
            input1 = int.Parse(textBox3.Text);
            hexValue = input1.ToString("X");
          

          }

        private void OctalRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            oct = 1;
            ans = Convert.ToInt32(textBox3.Text, 8);          
        }

        private void BinaryRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            bin = 1;
            input1 = int.Parse(textBox3.Text);
            hexValue=Convert.ToString(input1,2);
        }
        private void DecimalRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            des = 1;
            input1 = int.Parse(textBox3.Text);
            ans = m.BintoDecimal(input1);

        }

        private void AButton_Click(object sender, EventArgs e)
        {
            textBox3.Text += "A";
        }

        private void BButton_Click(object sender, EventArgs e)
        {
            textBox3.Text += "B";
        }

        private void CButton_Click(object sender, EventArgs e)
        {
            textBox3.Text += "C";
        }

        private void DButton_Click(object sender, EventArgs e)
        {
            textBox3.Text += "D";
        }

        private void EButton_Click(object sender, EventArgs e)
        {
            textBox3.Text += "E";
        }

        private void FButton_Click(object sender, EventArgs e)
        {
            textBox3.Text += "F";
        }

        private void BackspaceButton_Click(object sender, EventArgs e)
        {
          textBox3.Text = textBox3.Text.Remove(textBox3.Text.Length - 1, 1);
        }

        private void SquareButton_Click(object sender, EventArgs e)
        {
            squ = 1;
            input1 = Convert.ToInt32(textBox3.Text);
            textBox3.Text = "";
        }

        private void CosButton_Click(object sender, EventArgs e)
        {
            cos = 1;
            trig = Convert.ToDouble(textBox3.Text);
        }

        private void TanButton_Click(object sender, EventArgs e)
        {
            tan = 1;
            trig = Convert.ToDouble(textBox3.Text);
        }

        private void SecButton_Click(object sender, EventArgs e)
        {
            sec = 1;
            trig = Convert.ToDouble(textBox3.Text);
        }

        private void CotanButton_Click(object sender, EventArgs e)
        {
            cot = 1;
            trig = Convert.ToDouble(textBox3.Text);
        }

        private void CosecButton_Click(object sender, EventArgs e)
        {
            cosec = 1;
            trig = Convert.ToDouble(textBox3.Text);
        }

        private void LogButton_Click(object sender, EventArgs e)
        {
            log = 1;
            trig = Convert.ToDouble(textBox3.Text);
        }
    }

}

No comments:

Post a Comment