C#計算器

效果圖如下
在這裏插入圖片描述
代碼如下

        double num1, num2;
        //定義第一次輸入的數字與第二次輸入的數字
        string ysf;
        //定義運算符
        //第一次輸入的數字都顯示在文本框中
        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text += button7.Text;
            textBox2.Text += button7.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text += button8.Text;
            textBox2.Text += button8.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text += button9.Text;
            textBox2.Text += button9.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text += button4.Text;
            textBox2.Text += button4.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text += button5.Text;
            textBox2.Text += button5.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text += button6.Text;
            textBox2.Text += button6.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text += button1.Text;
            textBox2.Text += button1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text += button2.Text;
            textBox2.Text += button2.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text += button3.Text;
            textBox2.Text += button3.Text;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            textBox1.Text += button11.Text;
            textBox2.Text += button11.Text;
        }
        //+    判斷運算符爲+
        private void button17_Click(object sender, EventArgs e)
        {
            textBox2.Text += button17.Text;
            num1 = double.Parse(textBox1.Text);
            textBox1.Text = "";
            ysf = "+";
        }
        //-    判斷運算符爲-
        private void button15_Click(object sender, EventArgs e)
        {
            textBox2.Text += button15.Text;
            num1 = double.Parse(textBox1.Text);
            textBox1.Text = "";
            ysf = "-";
        }
        //X    判斷運算符爲X
        private void button14_Click(object sender, EventArgs e)
        {
            textBox2.Text += button14.Text;
            num1 = double.Parse(textBox1.Text);
            textBox1.Text = "";
            ysf = "X";
        }
        //    判斷運算符爲/
        private void button16_Click(object sender, EventArgs e)
        {
            textBox2.Text += button16.Text;
            num1 = double.Parse(textBox1.Text);
            textBox1.Text = "";
            ysf = "/";
        }
        //=
        private void button13_Click(object sender, EventArgs e)
        {
            num2 = double.Parse(textBox1.Text);
            //判斷運算符爲+-X/時的算法
            switch (ysf)
            {
                case ("+"):
                    textBox1.Text = (num1 + num2).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                    break;
                case ("-"):
                    textBox1.Text = (num1 - num2).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                    break;
                case ("X"):
                    textBox1.Text = (num1 * num2).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                    break;
                case ("/"):
                    textBox1.Text = (num1 / num2).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                    break;

            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            textBox1.Text += button10.Text;
            textBox2.Text += button10.Text;
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章