2010年10月22日 星期五

九宮格

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        System.Windows.Forms.Button[] Buttons;
        System.Windows.Forms.TextBox[] Textboxs;
        int[,] a = new int[4, 4];
        int bno;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Buttons = new System.Windows.Forms.Button[9];
            Textboxs = new System.Windows.Forms.TextBox[9];
            for (int i = 0; i < 9; ++i)
            {
                Buttons[i] = new Button();
                Buttons[i].Click += new System.EventHandler(Buttons_Click);
                this.Controls.Add(Buttons[i]);
                if (i <= 2)
                    Buttons[i].Location = new System.Drawing.Point(10 + i * 80, 10 + 100);
                else if (i > 2 && i <= 5)
                    Buttons[i].Location = new System.Drawing.Point(10 + (i - 3) * 80, 40 + 100);
                else if (i > 5 && i <= 9)
                    Buttons[i].Location = new System.Drawing.Point(10 + (i - 6) * 80, 70 + 100);
            }
            for (int j = 0; j < 9; ++j)
            {
                Textboxs[j] = new TextBox();
                this.Controls.Add(Textboxs[j]);
                if (j <= 2)
                    Textboxs[j].Location = new System.Drawing.Point(10 + j * 100, 10);
                else if (j > 2 && j <= 5)
                    Textboxs[j].Location = new System.Drawing.Point(10 + (j - 3) * 100, 40);
                else if (j > 5 && j <= 9)
                    Textboxs[j].Location = new System.Drawing.Point(10 + (j - 6) * 100, 70);
            }
            Textboxs[0].Text = "7";
            Textboxs[1].Text = "2";
            Textboxs[2].Text = "4";
            Textboxs[3].Text = "5";
            Textboxs[4].Text = "0";
            Textboxs[5].Text = "6";
            Textboxs[6].Text = "8";
            Textboxs[7].Text = "3";
            Textboxs[8].Text = "1";
        }
        public void Buttons_Click(object sender, EventArgs e)
        {
            // System.Windows.Forms.MessageBox.Show("You have clicked button " +
            //   ((System.Windows.Forms.Button)sender).Tag.ToString());
            int tlen = sender.ToString().Length;
            String no = sender.ToString().Substring(tlen - 1);
            for (int i = 0; i < 9; i++)
            {
                if (Buttons[i].Text == no)
                    bno = i;
            }
            switch(bno)
            {
            case 0:
                    if (Buttons[1].Text == "0")
                    {
                        String temp;
                        temp = Buttons[1].Text;
                        Buttons[1].Text = Buttons[0].Text;
                        Buttons[0].Text = temp;
                    }
                    else if (Buttons[3].Text == "0")
                    {
                        String temp;
                        temp = Buttons[3].Text;
                        Buttons[3].Text = Buttons[0].Text;
                        Buttons[0].Text = temp;
                    }
            break;
            case 1:
                    if (Buttons[4].Text == "0")       
                    {
                        String temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[1].Text;
                        Buttons[1].Text = temp;
                    }
                    else if (Buttons[0].Text == "0")
                    {
                        String temp;
                        temp = Buttons[0].Text;
                        Buttons[0].Text = Buttons[1].Text;
                        Buttons[1].Text = temp;
                    }
                    else if (Buttons[2].Text == "0")
                    {
                        String temp;
                        temp = Buttons[2].Text;
                        Buttons[2].Text = Buttons[1].Text;
                        Buttons[1].Text = temp;
                    }
            break;
            case 2:
                    if (Buttons[1].Text == "0")
                    {
                        String temp;
                        temp = Buttons[1].Text;
                        Buttons[1].Text = Buttons[2].Text;
                        Buttons[2].Text = temp;
                    }
                    else if (Buttons[5].Text == "0")
                    {
                        String temp;
                        temp = Buttons[5].Text;
                        Buttons[5].Text = Buttons[2].Text;
                        Buttons[2].Text = temp;
                    }
            break;
            case 3:
                    if (Buttons[0].Text == "0")
                    {
                        String temp;
                        temp = Buttons[0].Text;
                        Buttons[0].Text = Buttons[3].Text;
                        Buttons[3].Text = temp;
                    }
                    else if (Buttons[4].Text == "0")
                    {
                        String temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[3].Text;
                        Buttons[3].Text = temp;
                    }
                    else if (Buttons[6].Text == "0")
                    {
                        String temp;
                        temp = Buttons[6].Text;
                        Buttons[6].Text = Buttons[3].Text;
                        Buttons[3].Text = temp;
                    }
            break;
            case 4:
                    if (Buttons[1].Text == "0")
                    {
                        String temp;
                        temp = Buttons[1].Text;
                        Buttons[1].Text = Buttons[4].Text;
                        Buttons[4].Text = temp;
                    }
                    else if (Buttons[3].Text == "0")
                    {
                        String temp;
                        temp = Buttons[3].Text;
                        Buttons[3].Text = Buttons[4].Text;
                        Buttons[4].Text = temp;
                    }
                    else if (Buttons[5].Text == "0")
                    {
                        String temp;
                        temp = Buttons[5].Text;
                        Buttons[5].Text = Buttons[4].Text;
                        Buttons[4].Text = temp;
                    }
                    else if (Buttons[7].Text == "0")
                    {
                        String temp;
                        temp = Buttons[7].Text;
                        Buttons[7].Text = Buttons[4].Text;
                        Buttons[4].Text = temp;
                    }
           
            break;
            case 5:
                    if (Buttons[2].Text == "0")
                    {
                        String temp;
                        temp = Buttons[2].Text;
                        Buttons[2].Text = Buttons[5].Text;
                        Buttons[5].Text = temp;
                    }
                    else if (Buttons[4].Text == "0")
                    {
                        String temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[5].Text;
                        Buttons[5].Text = temp;
                    }
                    else if (Buttons[8].Text == "0")
                    {
                        String temp;
                        temp = Buttons[8].Text;
                        Buttons[8].Text = Buttons[5].Text;
                        Buttons[5].Text = temp;
                    }
            break;
            case 6:
                    if (Buttons[3].Text == "0")
                    {
                        String temp;
                        temp = Buttons[3].Text;
                        Buttons[3].Text = Buttons[6].Text;
                        Buttons[6].Text = temp;
                    }
                    else if (Buttons[7].Text == "0")
                    {
                        String temp;
                        temp = Buttons[7].Text;
                        Buttons[7].Text = Buttons[6].Text;
                        Buttons[6].Text = temp;
                    }
            break;
            case 7:
                    if (Buttons[4].Text == "0")
                    {
                        String temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[7].Text;
                        Buttons[7].Text = temp;
                    }
                    else if (Buttons[6].Text == "0")
                    {
                        String temp;
                        temp = Buttons[6].Text;
                        Buttons[6].Text = Buttons[7].Text;
                        Buttons[7].Text = temp;
                    }
                    else if (Buttons[8].Text == "0")
                    {
                        String temp;
                        temp = Buttons[8].Text;
                        Buttons[8].Text = Buttons[7].Text;
                        Buttons[7].Text = temp;
                    }
            break;
            case 8:
                    if (Buttons[5].Text == "0")
                    {
                        String temp;
                        temp = Buttons[5].Text;
                        Buttons[5].Text = Buttons[8].Text;
                        Buttons[8].Text = temp;
                    }
                    else if (Buttons[7].Text == "0")
                    {
                        String temp;
                        temp = Buttons[7].Text;
                        Buttons[7].Text = Buttons[8].Text;
                        Buttons[8].Text = temp;
                    }
            break;
          
            }
        

        }
        private void button1_Click(object sender, EventArgs e)
        {
            //輸入 input
            a[1, 1] = Convert.ToInt16(Textboxs[0].Text);
            a[1, 2] = Convert.ToInt16(Textboxs[1].Text);
            a[1, 3] = Convert.ToInt16(Textboxs[2].Text);
            a[2, 1] = Convert.ToInt16(Textboxs[3].Text);
            a[2, 2] = Convert.ToInt16(Textboxs[4].Text);
            a[2, 3] = Convert.ToInt16(Textboxs[5].Text);
            a[3, 1] = Convert.ToInt16(Textboxs[6].Text);
            a[3, 2] = Convert.ToInt16(Textboxs[7].Text);
            a[3, 3] = Convert.ToInt16(Textboxs[8].Text);
            //輸出 output
            for (int i = 0; i < 9; i++)
            {
                if (i < 3)
                {
                    Buttons[i].Text = Convert.ToString(a[1, i + 1]);
                }
                else if (i >= 3 && i < 6)
                {
                    Buttons[i].Text = Convert.ToString(a[2, i - 2]);
                }
                else
                {
                    Buttons[i].Text = Convert.ToString(a[3, i - 5]);
                }
            }
        }
    }
}

2010年10月8日 星期五

3*3矩陣

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 WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int[,] a = new int[4, 4];
            int[,] b = new int[4, 4];
            int[,] c = new int[4, 4];
            String c1, c2, c3, c4, c5,c6, c7, c8, c9;
            a[1, 1] = int.Parse(textBox1.Text);
            a[1, 2] = int.Parse(textBox2.Text);
            a[1, 3] = int.Parse(textBox3.Text);
            a[2, 1] = int.Parse(textBox4.Text);
            a[2, 2] = int.Parse(textBox5.Text);
            a[2, 3] = int.Parse(textBox6.Text);
            a[3, 1] = int.Parse(textBox7.Text);
            a[3, 2] = int.Parse(textBox8.Text);
            a[3, 3] = int.Parse(textBox9.Text);
            b[1, 1] = int.Parse(textBox10.Text);
            b[1, 2] = int.Parse(textBox11.Text);
            b[1, 3] = int.Parse(textBox12.Text);
            b[2, 1] = int.Parse(textBox13.Text);
            b[2, 2] = int.Parse(textBox14.Text);
            b[2, 3] = int.Parse(textBox15.Text);
            b[3, 1] = int.Parse(textBox16.Text);
            b[3, 2] = int.Parse(textBox17.Text);
            b[3, 3] = int.Parse(textBox18.Text);
           
            c[1, 1] = a[1, 1] * b[1, 1] + a[1, 2] * b[2, 1] + a[1, 3] * b[3, 1];
            c[1, 2] = a[1, 1] * b[1, 2] + a[1, 2] * b[2, 2] + a[1, 3] * b[3, 2];
            c[1, 3] = a[1, 1] * b[1, 3] + a[1, 2] * b[2, 3] + a[1, 3] * b[3, 3];
            c[2, 1] = a[2, 1] * b[1, 1] + a[2, 2] * b[2, 1] + a[2, 3] * b[3, 1];
            c[2, 2] = a[2, 1] * b[1, 2] + a[2, 2] * b[2, 2] + a[2, 3] * b[3, 2];
            c[2, 3] = a[2, 1] * b[1, 3] + a[2, 2] * b[2, 3] + a[2, 3] * b[3, 3];
            c[3, 1] = a[3, 1] * b[1, 1] + a[3, 2] * b[2, 1] + a[3, 3] * b[3, 1];
            c[3, 2] = a[3, 1] * b[1, 2] + a[3, 2] * b[2, 2] + a[3, 3] * b[3, 2];
            c[3, 3] = a[3, 1] * b[1, 3] + a[3, 2] * b[2, 3] + a[3, 3] * b[3, 3];
            c1 = Convert.ToString(c[1, 1]);
            c2 = Convert.ToString(c[1, 2]);
            c3 = Convert.ToString(c[1, 3]);
            c4 = Convert.ToString(c[2, 1]);
            c5 = Convert.ToString(c[2, 2]);
            c6 = Convert.ToString(c[2, 3]);
            c7 = Convert.ToString(c[3, 1]);
            c8 = Convert.ToString(c[3, 2]);
            c9 = Convert.ToString(c[3, 3]);
            textBox19.Text = c1;
            textBox20.Text = c2;
            textBox21.Text = c3;
            textBox22.Text = c4;
            textBox23.Text = c5;
            textBox24.Text = c6;
            textBox25.Text = c7;
            textBox26.Text = c8;
            textBox27.Text = c9;
        }
        private void label1_Click(object sender, EventArgs e)
        {
        }
    }
}

2010年10月1日 星期五

等差數列

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Form1t newform;
        public Form1()
        {
            InitializeComponent();
        }
        private void label3_Click(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //宣告
            int a1, an, n, d,sn;
            //輸入
            a1 = int.Parse(textBox1.Text);
            d = int.Parse(textBox2.Text);
            n=int.Parse(textBox3.Text);
            //處理
            an = a1 + (n-1) * d;
            sn = (a1 + an) * n / 2;
            //顯示
            textBox4.Text = "" + an;
            textBox5.Text = "" + sn;
        }
    
        private void button4_Click(object sender, EventArgs e)
        {
            newform = new Form1t();
            newform.Show();
        }
    }
}

九九乘法表listBox1

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 WindowsFormsApplication1
{
    public partial class Form1t : Form
    {
        public Form1t()
        {
            InitializeComponent();
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        private void button2_Click(object sender, EventArgs e)
        {
            {
            int sum = 0;
            int[,] arr2 = new int[10, 10];
            for (int i = 1; i <= 9; i++)
                for (int j = 1; j <= 9; j++)
                {
                    sum = i * j;
                    arr2[i, j] = sum;
                }
            for (int i = 1; i <= 9; i++)
                {
                    listBox1.Items.Add(+i + "*" + 1 + "=" + arr2[i, 1]+"," +i + "*" + 2 + "=" + arr2[i, 2]
                        + "," + i + "*" + 3 + "=" + arr2[i, 3] + "," + i + "*" + 4 + "=" + arr2[i, 4]
                        + "," + i + "*" + 5 + "=" + arr2[i, 5] + "," + i + "*" + 6 + "=" + arr2[i, 6]
                        + "," + i + "*" + 7 + "=" + arr2[i, 7] + "," + i + "*" + 8 + "=" + arr2[i, 8]
                        + "," + i + "*" + 9 + "=" + arr2[i, 9]);
                }
        }
       
        }
    }
}