using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class frmmain : Form
    {
        public frmmain()
        {
            InitializeComponent();
        }
        double num1 = 0, num2 = 0, num;

        private void btn9_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (txtmain.Text == "0" || lblamalyat.Text != "")
                {
                    txtmain.Text = "9";
                    lblamalyat.Text = "";
                }

                else
                    txtmain.Text += "9";
            }
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (txtmain.Text == "0" || lblamalyat.Text != "")
                {
                    txtmain.Text = "8";
                    lblamalyat.Text = "";
                }

                else
                    txtmain.Text += "8";
            }
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (txtmain.Text == "0" || lblamalyat.Text != "")
                {
                    txtmain.Text = "7";
                    lblamalyat.Text = "";
                }

                else
                    txtmain.Text += "7";
            }
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (txtmain.Text == "0" || lblamalyat.Text != "")
                {
                    txtmain.Text = "6";
                    lblamalyat.Text = "";
                }

                else
                    txtmain.Text += "6";
            }
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (txtmain.Text == "0" || lblamalyat.Text != "")
                {
                    txtmain.Text = "5";
                    lblamalyat.Text = "";
                }

                else
                    txtmain.Text += "5";
            }
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (txtmain.Text == "0" || lblamalyat.Text != "")
                {
                    txtmain.Text = "4";
                    lblamalyat.Text = "";
                }

                else
                    txtmain.Text += "4";
            }
        }



        private void btn2_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (txtmain.Text == "0" || lblamalyat.Text != "")
                {
                    txtmain.Text = "2";
                    lblamalyat.Text = "";
                }

                else
                    txtmain.Text += "2";
            }
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (txtmain.Text == "0"  || lblamalyat.Text!="") 
                {
                    txtmain.Text = "1";
                    lblamalyat.Text = "";
                }

                else
                    txtmain.Text += "1";
            }
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (txtmain.Text == "0" || lblamalyat.Text != "")
                {
                    txtmain.Text = "0";
                    lblamalyat.Text = "";
                }

                else
                    txtmain.Text += "0";
            }
            
        }
        private void Mosavi(string sign)
        {
            switch (sign)
            {
                case "+":

                    if (num1 == 0)
                    {
                        num1 = double.Parse(txtmain.Text);
                        txtmain.Text = "0";
                    }
                      else
                    {
                        num2 = double.Parse(txtmain.Text);
                        num = num1 + num2;
                        txtmain.Text = Convert.ToString(num);
                        num1 = 0; num2 = 0;
                        lblamalyat.Text = "+";
                        num1 = num;
                    }
                    break;
                case "-":
                    if (num1 == 0)
                    {
                        num1 = double.Parse(txtmain.Text);
                        txtmain.Text = "0";
                    }
                    else
                    {
                        num2 = double.Parse(txtmain.Text);
                        num = num1 - num2;
                        txtmain.Text = Convert.ToString(num);
                        num1 = 0; num2 = 0;
                        lblamalyat.Text = "-";
                        num1 = num;
                    }
                    break;
                case "*":
                    if (num1 == 0)
                    {
                        num1 = double.Parse(txtmain.Text);
                        txtmain.Text = "0";
                    }
                    else
                    {
                        num2 = double.Parse(txtmain.Text);
                        num = num1 * num2 ;
                        txtmain.Text = Convert.ToString(num);
                        num1 = 0; num2 = 0;
                        lblamalyat.Text = "*";
                        num1 = num;
                    }
                    break;
                case "/":
                    if (num1 == 0)
                    {
                        num1 = double.Parse(txtmain.Text);
                        txtmain.Text = "0";
                    }
                    else
                    {
                        num2 = double.Parse(txtmain.Text);
                        num = num1 / num2;
                        txtmain.Text = Convert.ToString(num);
                        num1 = 0; num2 = 0;
                        lblamalyat.Text = "/";
                         num1 = num;
                    }
                    break;
                                    
            }

        }
        private void btnsum_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (lbltashkhisalamat.Text != "")

                    Mosavi(lbltashkhisalamat.Text);
                else
                    Mosavi("+");

                lbltashkhisalamat.Text = "+";
            }
        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            num1 = 0; num2 = 0;
            txtmain.Text = "0";
            lbltashkhisalamat.Text = "";
        }

        private void btntafrigh_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (lbltashkhisalamat.Text != "")
                    Mosavi(lbltashkhisalamat.Text);
                else
                {
                    Mosavi("-");
                }
                lbltashkhisalamat.Text = "-";
            }
        }

        private void btntaghsim_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (lbltashkhisalamat.Text != "")
                    Mosavi(lbltashkhisalamat.Text);
                else
                {
                    Mosavi("/");
                }
                lbltashkhisalamat.Text = "/";
            }
        }

        private void btnzarb_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (lbltashkhisalamat.Text != "")
                    Mosavi(lbltashkhisalamat.Text);
                else
                {
                    Mosavi("*");
                }
                lbltashkhisalamat.Text = "*";
            }
        }
        private void btnmosavi_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                switch (lbltashkhisalamat.Text)
                {
                    case ("+"):
                        Mosavi("+");
                        break;
                    case ("-"):
                        Mosavi("-");
                        break;
                    case ("/"):
                        Mosavi("/");
                        break;
                    case ("*"):
                        Mosavi("*");
                        break;

                }
                lbltashkhisalamat.Text = "";
                num = 0; num1 = 0; num2 = 0;
            }
        }

        private void btn3_Click_1(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (txtmain.Text == "0" || lblamalyat.Text != "")
                {
                    txtmain.Text = "3";
                    lblamalyat.Text = "";
                }

                else
                    txtmain.Text += "3";
            }
        }

        private void btnback_Click(object sender, EventArgs e)
        {
            string str;
            int i;
            str = txtmain.Text;
            i = str.Length;
            if (!(i==1))
            {
            str=str.Substring(0, i-1);
            txtmain.Text = str;
            }
            else
                txtmain.Text="0";
        }

        private void btnon_Click(object sender, EventArgs e)
        {
            txtmain.Enabled = true;
        }
        private void btnoff_Click_1(object sender, EventArgs e)
        {
            txtmain.Enabled = false;
            lblamalyat.Text = "";
            lbltashkhisalamat.Text = "";
            num = 0; num1 = 0; num2 = 0;
            txtmain.Text = "0";
        }

        private void btnmomayez_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                
                
                    string str, s3;
                    int t = -1, i;
                    str = txtmain.Text;
                    t = Convert.ToInt16(str.IndexOf('.'));

                    if (t == -1)
                        txtmain.Text = "." + txtmain.Text;
                    else
                    {
                        str = txtmain.Text;
                        i = str.Length;
                        s3 = str.Substring(1, i - 1);
                        txtmain.Text = s3;
                        t = -1;
                    }
                

            }
        }

        private void btnalamat_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (txtmain.Text!="0")
                {
                    string str,s3;
                    int t=-1,i;
                    str=txtmain.Text;
                    t=Convert.ToInt16( str.IndexOf('-'));

                    if (t == -1)
                        txtmain.Text = "-"+txtmain.Text ;
                    else
                    {
                        str = txtmain.Text;
                        i = str.Length;
                        s3 = str.Substring(1,i-1);
                        txtmain.Text = s3;
                        t = -1;
                    }
                }

            }
        }
        int ms=0;
        private void button23_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                if (txtmain.Text != "0")
                {
                    ms = Convert.ToInt16 (txtmain.Text);
                    txtmemory.Text = "M";
                }

            }
        }

        private void btnmc_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                               
                    ms = 0;
                    txtmemory.Text = "";
                
            }
        }

        private void btnmr_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                 txtmain.Text = Convert.ToString(ms); ;
                                    
            }
        }

        private void btnm_Click(object sender, EventArgs e)
        {
            if (txtmain.Enabled == true)
            {
                               
                    ms +=ms;
                                  

            }
        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Thank you! Programmer:Sam Rahmati" , "Calculator");
            this.Close();
        }
    }
}