Wednesday, July 27, 2011

Standard Calculator [www.ScriptHood.com]

Hello guys,
This is c# code for Standard Calculator. I hope you will find it useful.






Standard Calculator :

Download this project from: www.ScriptHood.com


Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Calculator
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        bool plus = false;
        bool minus = false;
        bool multiply = false;
        bool divide = false;
        bool equal = false;
        bool flag = false;
        bool sqrt = false;
        bool sin = false;
        bool cos = false;
        bool log = false;
        bool power = false;
        bool factorial = false;
        bool mod = false;
        bool repic = false;
        bool tan = false;
        bool square = false;

        string latestOperation;
        
        

        public MainWindow()
        {
            InitializeComponent();
        }
        private void CheckOperator() 
        {
            if (flag == true)
            {
                
                textBox1.Text = "";
                
                flag = false;
            }
        }
        private void CheckIfEqual()
        {
            
            
            if (equal == true)
            {
                
                textBox1.Text = "";
                equal = false;
            }
        }

        private void CheckFactorial()
        {
            if (factorial)
            {

                textBox1.Text = "";

            }
            factorial = false;
        }
        private void CheckLog()
        {
            if (log)
            {
                
                textBox1.Text = "";

            }
            log = false;
        }
        private void CheckSqrt()
        {
            if (sqrt)
            {

                textBox1.Text = "";
                
            }
            sqrt = false;
        }
        private void CheckSquare()
        {
            if (square)
            {

                textBox1.Text = "";

            }
            square = false;
        }
        private void CheckRepic()
        {
            if (repic)
            {

                textBox1.Text = "";

            }
            repic = false;
        }

        private void CheckSin()
        {
            if (sin)
            {

                textBox1.Text = "";

            }
            sin = false;
        }

        private void CheckCos()
        {
            if (cos)
            {

                textBox1.Text = "";

            }
            cos = false;
        }
        private void CheckTan()
        {
            if (tan)
            {

                textBox1.Text = "";

            }
            tan = false;
        }

        private void operatorCheck()
        {
            CheckSquare();
            CheckTan();
            CheckRepic();
            CheckFactorial();
            CheckLog();
            CheckCos();
            CheckSin();
            CheckSqrt();
            CheckOperator();
            CheckIfEqual();
 
        }
        private void button9_Click(object sender, RoutedEventArgs e)
        {
            operatorCheck();
            textBox1.Text += "1";
        }

        

        private void button11_Click(object sender, RoutedEventArgs e)
        {
            operatorCheck();
            textBox1.Text += "2";
        }

        private void button13_Click(object sender, RoutedEventArgs e)
        {
            operatorCheck();
            textBox1.Text += "3";
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            operatorCheck();
            textBox1.Text += "4";
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            operatorCheck();
            textBox1.Text += "5";
        }

        
        private void button7_Click(object sender, RoutedEventArgs e)
        {
            operatorCheck();
            textBox1.Text += "6";
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            operatorCheck();
            textBox1.Text += "7";
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            operatorCheck();
            textBox1.Text += "8";
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            operatorCheck();
            textBox1.Text += "9";
        }

        private void button10_Click(object sender, RoutedEventArgs e)
        {
            operatorCheck();
            textBox1.Text += "0";
        }

        private void button27_Click(object sender, RoutedEventArgs e)// pi
        {
            operatorCheck();
            textBox1.Text += Math.PI.ToString();
        }

        private void button14_Click(object sender, RoutedEventArgs e)
        {
            CheckIfEqual();

            if (textBox1.Text.Contains(","))
            {
                return;
            }
            else
            {
                textBox1.Text += ",";
            }
        }

        private void button12_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text.Contains("-"))
            {
                textBox1.Text = textBox1.Text.Remove(0, 1);
            }
            else
            {
                textBox1.Text = "-" + textBox1.Text;
            }
        }

       

        private void CheckActiveOperators()
        {
            if (mod)
            {
                if (textBox1.Text == "")
                {
                    return;
                }
                double dob = (Convert.ToDouble(textBox1.Tag)%Convert.ToDouble(textBox1.Text));
                textBox1.Text = "";
                flag = true;
                textBox1.Text = dob.ToString();
            }
            if (power)
            {
                if (textBox1.Text == "")
                {
                    return;
                }
                double dob = Math.Pow(Convert.ToDouble(textBox1.Tag), Convert.ToDouble(textBox1.Text));
                textBox1.Text = "";
                flag = true;
                textBox1.Text = dob.ToString();
            }
            
            if (plus)
            {
                if (textBox1.Text == "")
                {
                    return;
                }
                decimal dec = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text);
                textBox1.Text = "";
                flag = true;
                textBox1.Text = dec.ToString();
            }
            if (minus)
            {
                if (textBox1.Text == "")
                {
                    return;
                }
                decimal dec = Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text);
                textBox1.Text = "";
                flag = true;
                textBox1.Text = dec.ToString();
            }
            if (multiply)
            {
                if (textBox1.Text == "")
                {
                    return;
                }
                decimal dec = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text);
                textBox1.Text = "";
                flag = true;
                textBox1.Text = dec.ToString();
            }
            if (divide)
            {
                if (textBox1.Text == "")
                {
                    return;
                }
                decimal dec = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text);
                textBox1.Text = "";
                flag = true;
                textBox1.Text = dec.ToString();
            }
        }

        
        private void button18_Click(object sender, RoutedEventArgs e) //EQUALS
        {
            equal = true;
            flag = false;

            if (mod && latestOperation == "mod")
            {
                double dob = (Convert.ToDouble(textBox1.Tag)%Convert.ToDouble(textBox1.Text));
                textBox1.Text = "";
                textBox1.Text = dob.ToString();
                mod = false;
            }
            if (power && latestOperation == "power")
            {
                double dob = Math.Pow(Convert.ToDouble(textBox1.Tag),Convert.ToDouble(textBox1.Text));
                textBox1.Text = "";
                textBox1.Text = dob.ToString();
                power = false;
            }
            if (plus && latestOperation=="plus" )
            {
                decimal dec = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text);
                textBox1.Text = "";
                textBox1.Text= dec.ToString();
                plus = false;
            }
            if (minus && latestOperation=="minus")
            {
                decimal dec =Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text);
                textBox1.Text = "";
                textBox1.Text = dec.ToString();
                minus = false;
            }
            if (multiply && latestOperation=="multiply")
            {
                decimal dec = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text);
                textBox1.Text = "";
                textBox1.Text = dec.ToString();
                multiply = false;
            }
            if (divide && latestOperation=="divide")
            {
                decimal dec = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text);
                textBox1.Text = "";
                textBox1.Text =dec.ToString();
                divide = false;
            }

            latestOperation = "";

        }

        private void button26_Click(object sender, RoutedEventArgs e) // MOD
        {
            CheckActiveOperators();


            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                mod = true;
                latestOperation = "mod";

                textBox1.Tag = textBox1.Text;

                if (!flag)
                {
                    textBox1.Text = "";
                }
            }

        }
        private void button21_Click(object sender, RoutedEventArgs e)// POWER
        {
            CheckActiveOperators();


            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                power = true;
                latestOperation = "power";

                textBox1.Tag = textBox1.Text;

                if (!flag)
                {
                    textBox1.Text = "";
                }
            }

        }
        private void button16_Click(object sender, RoutedEventArgs e) //PLUS
        {

            CheckActiveOperators();


            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                plus = true;
                latestOperation = "plus";


                textBox1.Tag = textBox1.Text;

                if (!flag)
                {
                    textBox1.Text = "";
                }

            }
        }

        private void button15_Click(object sender, RoutedEventArgs e) //MINUS
        {
            CheckActiveOperators();

            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                minus = true;
                latestOperation = "minus";
                textBox1.Tag = textBox1.Text;

                if (!flag)
                {
                    textBox1.Text = "";
                }
            }

        }

        private void button8_Click(object sender, RoutedEventArgs e) //MULTIPLY
        {
            CheckActiveOperators();

            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                multiply = true;
                latestOperation = "multiply";
                textBox1.Tag = textBox1.Text;

                if (!flag)
                {
                    textBox1.Text = "";
                }
            }
        }

        private void button24_Click(object sender, RoutedEventArgs e)//FACTORIAL
        {
            CheckActiveOperators();

            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                latestOperation = "factorial";

            }

            int result=1;
            int a = Convert.ToInt32(textBox1.Text);
            for (int i = 1; i <= a; i++)
            {
                result *= i;
 
            }
            textBox1.Text = result.ToString();

                factorial = true;

        }
        private void button25_Click(object sender, RoutedEventArgs e) // LOG
        {
            CheckActiveOperators();

            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                latestOperation = "log";

            }

            textBox1.Text = (Math.Log10(Convert.ToDouble(textBox1.Text))).ToString();
            log = true;
        }

        private void button23_Click(object sender, RoutedEventArgs e)//COS
        {
            CheckActiveOperators();

            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                latestOperation = "cos";

            }

            textBox1.Text = (Math.Cos(Convert.ToDouble(textBox1.Text))).ToString();
            cos = true;

        }
        private void button29_Click(object sender, RoutedEventArgs e) //tan
        {
            CheckActiveOperators();

            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                latestOperation = "tan";

            }

            textBox1.Text = (Math.Tan(Convert.ToDouble(textBox1.Text))).ToString();
            tan = true;

        }
        private void button22_Click(object sender, RoutedEventArgs e) //SIN
        {
            CheckActiveOperators();

            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                latestOperation = "sin";

            }

            textBox1.Text = (Math.Sin(Convert.ToDouble(textBox1.Text))).ToString();
            sin = true;

        }
        private void button28_Click(object sender, RoutedEventArgs e) // 1/x
        {
            CheckActiveOperators();

            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                latestOperation = "repic";

            }

            textBox1.Text = ((1/Convert.ToDouble(textBox1.Text))).ToString();
            repic = true;
        }

        private void button30_Click(object sender, RoutedEventArgs e) // x^2
        {
            CheckActiveOperators();

            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                latestOperation = "square";

            }

            textBox1.Text = ((Convert.ToDouble(textBox1.Text)) * (Convert.ToDouble(textBox1.Text))).ToString();
            square = true;

        }
        private void button20_Click(object sender, RoutedEventArgs e) // SQRT
        {
            CheckActiveOperators();

            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                 latestOperation = "sqrt";
                               
            }

            textBox1.Text = (Math.Sqrt(Convert.ToDouble(textBox1.Text))).ToString();
            sqrt = true;
        }

        private void button5_Click(object sender, RoutedEventArgs e) //DIVIDE
        {
            CheckActiveOperators();

            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                divide = true;
                latestOperation = "divide";
                textBox1.Tag = textBox1.Text;

                if (!flag)
                {
                    textBox1.Text = "";
                }
            }
        }

        private void button17_Click(object sender, RoutedEventArgs e)
        {
            plus = minus = multiply = divide = equal=power=false;
            textBox1.Text = "";
            textBox1.Tag = "";
        }

        private void button19_Click(object sender, RoutedEventArgs e)
        {
            
            if (textBox1.Text.Length > 0)
            {
                textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1, 1);
            }
        }   
       
    }
}


No comments:

Post a Comment