2014年11月20日 星期四

小算盤與副程式

public partial class Form1 : Form
    {
        int a = 0, b = 0,i=4;
        float a1 = 0, b1 = 0, c1 = 1;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text ="+";
            if (a == 0) {
                a = 1;
                textBox2.Visible = true;
                textBox3.Visible = true;
                b = 0;
                button17.Enabled = true;
                c1 = 1;
            }
            i = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Text ="-";
            if (a == 0)
            {
                a = 1;
                textBox2.Visible = true;
                textBox3.Visible = true;
                b = 0;
                button17.Enabled = true;
                c1 = 1;
            }
            i = 1;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Text ="*";
            if (a == 0)
            {
                a = 1;
                textBox2.Visible = true;
                textBox3.Visible = true;
                b = 0;
                button17.Enabled = true;
                c1 = 1;
            }
            i = 2;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox2.Text ="/";
            if (a == 0)
            {
                a = 1;
                textBox2.Visible = true;
                textBox3.Visible = true;
                b = 0;
                button17.Enabled = true;
                c1 = 1;
            }
            i = 3;
        }
        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            a = 0;
            b = 0;
            a1 = 0;
            b1 = 0;
            c1 = 1;
            i = 4;
            textBox2.Visible = false;
            textBox3.Visible = false;
            textBox4.Visible = false;
            button17.Enabled = true;
        }
        private void button6_Click(object sender, EventArgs e)
        {
            add(0);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox4.Visible = true;
            if (i == 0)
            {
                c1 = a1 + b1;
                textBox4.Text = "" + c1;
            }
            if (i == 1)
            {
                c1 = a1 - b1;
                textBox4.Text = "" + c1;
            }
            if (i == 2)
            {
                c1 = a1 * b1;
                textBox4.Text = "" + c1;
            }
            if (i == 3)
            {
                if (b1 != 0)
                {
                    c1 = a1 / b1;
                    textBox4.Text = "" + c1;
                }
                else if (b1 == 0) { textBox4.Text = "error!"; }
            }
        }
        private void button8_Click(object sender, EventArgs e)
        {
            add(1);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            add(2);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            add(3);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            add(4);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            add(5);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            add(6);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            add(7);
        }

        private void button15_Click(object sender, EventArgs e)
        {
            add(8);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            add(9);
        }

        private void button17_Click(object sender, EventArgs e)
        {
            b = 1;
            button17.Enabled = false;
            if (a == 0) { textBox1.Text = textBox1.Text + "."; }
            if (a == 1)
            {
                textBox3.Text = textBox3.Text + ".";
            }
        }
        private void add(int i)//這邊宣告了副程式add(int i)
        {
            if (a == 0)
            {
                textBox1.Text = textBox1.Text +i;
                if (b == 0)
                {
                    a1 = a1 * 10 + i;
                }
                if (b == 1)
                {
                    c1 = c1 / 10;
                    a1 = a1 + i * c1;
                }
            }
            if (a == 1)
            {
                textBox3.Text = textBox3.Text+i ;
                if (b == 0)
                {
                    b1 = b1 * 10 + i;
                }
                if (b == 1)
                {
                    c1 = c1 / 10;
                    b1 = b1 + i * c1;
                }
            }
        }
    }
--------------------------------------------------------------------------------------------

沒有留言:

張貼留言