2010年10月29日 星期五

移動數字

左上右下右下左左上右右下左左上
右右上左左下右右上左左

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];
        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 i = 0; i < 9; ++i)
            {
                TextBoxs[i] = new TextBox();
                this.Controls.Add(TextBoxs[i]);
                if (i <= 2)
                    TextBoxs[i].Location = new System.Drawing.Point(10 + i * 100, 10);
                else if (i > 2 && i <= 5)
                    TextBoxs[i].Location = new System.Drawing.Point(10 + (i - 3) * 100, 40);
                else if (i > 5 && i <= 9)
                    TextBoxs[i].Location = new System.Drawing.Point(10 + (i - 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());
            String tnum = sender.ToString();
            int tlen = tnum.Length;
            String no = tnum.Substring(tlen - 1);
            int n = int.Parse(no);
            for (int i = 0; i < 9; i++)
            {
                if (Buttons[i].Text == no)
                    n = i;
            }
            switch (n)
            {
                case 0:
                    if (Buttons[1].Text == "0")
                    {
                        string temp;
                        temp = Buttons[1].Text;
                        Buttons[1].Text = Buttons[0].Text;
                        Buttons[0].Text = temp;
                    }
                    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[0].Text == "0")
                    {
                        string temp;
                        temp = Buttons[0].Text;
                        Buttons[0].Text = Buttons[1].Text;
                        Buttons[1].Text = temp;
                    }
                    if (Buttons[4].Text == "0")
                    {
                        string temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[1].Text;
                        Buttons[1].Text = temp;
                    }
                    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;
                    }
                    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;
                    }
                    if (Buttons[4].Text == "0")
                    {
                        string temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[3].Text;
                        Buttons[3].Text = temp;
                    }
                    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;
                    }
                    if (Buttons[3].Text == "0")
                    {
                        string temp;
                        temp = Buttons[3].Text;
                        Buttons[3].Text = Buttons[4].Text;
                        Buttons[4].Text = temp;
                    }
                    if (Buttons[5].Text == "0")
                    {
                        string temp;
                        temp = Buttons[5].Text;
                        Buttons[5].Text = Buttons[4].Text;
                        Buttons[4].Text = temp;
                    }
                    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;
                    }
                    if (Buttons[4].Text == "0")
                    {
                        string temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[5].Text;
                        Buttons[5].Text = temp;
                    }
                    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;
                    }
                    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;
                    }
                    if (Buttons[6].Text == "0")
                    {
                        string temp;
                        temp = Buttons[6].Text;
                        Buttons[6].Text = Buttons[7].Text;
                        Buttons[7].Text = temp;
                    }
                    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;
                    }
                    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 - 3) + 1]);
                }
                else
                {
                    Buttons[i].Text = Convert.ToString(a[3, (i - 3 * 2) + 1]);
                }
            }
        }
    }
}

2010年10月22日 星期五

PUZZLE

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];
        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 i = 0; i < 9; ++i)
            {
                TextBoxs[i] = new TextBox();
                this.Controls.Add(TextBoxs[i]);
                if (i <= 2)
                    TextBoxs[i].Location = new System.Drawing.Point(10 + i * 100, 10);
                else if (i > 2 && i <= 5)
                    TextBoxs[i].Location = new System.Drawing.Point(10 + (i - 3) * 100, 40);
                else if (i > 5 && i <= 9)
                    TextBoxs[i].Location = new System.Drawing.Point(10 + (i - 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());
           MessageBox.Show("" + sender.ToString());
          
        }
        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 - 3) + 1]);
               }
               else
               {
                   Buttons[i].Text = Convert.ToString(a[3, (i - 3 * 2) + 1]);
               }
           }
        }
       
    }
}

2010年10月15日 星期五

3個矩陣

        System.Windows.Forms.TextBox[] TextBoxs;
        System.Windows.Forms.TextBox[] TextBoxs1;
        System.Windows.Forms.Button[] Buttons;




------------------------------------------------------------------------------


               TextBoxs = new System.Windows.Forms.TextBox[16];

               for (int i = 0; i < 16; ++i)
               {
                   TextBoxs[i] = new TextBox();
                   this.Controls.Add(TextBoxs[i]);
                   if (i <= 3)
                       TextBoxs[i].Location = new System.Drawing.Point(20 + i * 100, 30);

                   else if (i > 3 && i <= 7)
                       TextBoxs[i].Location = new System.Drawing.Point(20 + (i - 4) * 100, 60);
                   else if (i > 7 && i <= 11)
                       TextBoxs[i].Location = new System.Drawing.Point(20 + (i - 8) * 100, 90);
                   else if (i > 11 && i <= 15)
                       TextBoxs[i].Location = new System.Drawing.Point(20 + (i - 12) * 100, 120);
               }
                TextBoxs1 = new System.Windows.Forms.TextBox[16];

                for (int j = 0; j < 16; ++j)
                {
                    TextBoxs[j] = new TextBox();
                    this.Controls.Add(TextBoxs[j]);
                    if (j <= 3)
                        TextBoxs[j].Location = new System.Drawing.Point(500 + j * 100, 30);

                    else if (j > 3 && j <= 7)
                        TextBoxs[j].Location = new System.Drawing.Point(500 + (j - 4) * 100, 60);
                    else if (j > 7 && j <= 11)
                        TextBoxs[j].Location = new System.Drawing.Point(500 + (j - 8) * 100, 90);
                    else if (j > 11 && j <= 15)
                        TextBoxs[j].Location = new System.Drawing.Point(500 + (j - 12) * 100, 120);

                }

                Buttons = new System.Windows.Forms.Button[16];
       
                 for (int k = 0; k < 16; ++k)
              {
                Buttons[k] = new Button();
                this.Controls.Add(Buttons[k]);
                if (k <= 3)
                    Buttons[k].Location = new System.Drawing.Point(20 + k * 100, 200);
                else if (k > 3 && k <= 7)
                    Buttons[k].Location = new System.Drawing.Point(20 + (k - 4) * 100, 230);
                else if (k > 7 && k <= 11)
                    Buttons[k].Location = new System.Drawing.Point(20 + (k - 8) * 100, 260);
                else if (k > 11 && k <= 15)
                    Buttons[k].Location = new System.Drawing.Point(20 + (k - 12) * 100, 290);
            }

2010年10月8日 星期五

3*3矩陣運算

            int[,] a = new int[4, 4];
            int[,] b = new int[4, 4];
            int[,] c = new int[4, 4];
          
         
            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];
            textBox19.Text=""+c[1,1];
            textBox20.Text=""+c[1,2];
            textBox21.Text=""+c[1,3];
            textBox22.Text=""+c[2,1];
            textBox23.Text=""+c[2,2];
            textBox24.Text=""+c[2,3];
            textBox25.Text=""+c[3,1];
            textBox26.Text=""+c[3,2];
            textBox27.Text=""+c[3,3];

2010年10月1日 星期五

C# listbox 99乘法表

          int[,] array = new int[10, 10];
            for (int z = 1; z <= 9; z++)
            {
                for (int b = 1; b <= 9; b++)
                {
                    array[z, b] = z * b;
                }
            }
            for (int c = 1; c <= 9; c++)
            {
                listBox1.Items.Add("" + c + "*1=" + array[c, 1] + "" + c + "*2=" + array[c, 2] + "" + c + "*3=" +array[c, 3] + "" + c + "*4=" + array[c, 4] + "" + c + "*5=" + array[c, 5] + "" + c + "*6=" + array[c, 6] + "" + c + "*7=" + array[c, 7] + "" + c + "*8=" + array[c, 8] + "" + c + "*9=" + array[c, 9]);
            }