2010年10月22日 星期五

複合式4X4矩陣(內建單位矩陣X單位矩陣)

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 WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        System.Windows.Forms.TextBox[] TextBoxes;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            TextBoxes = new System.Windows.Forms.TextBox[17];
            for (int i = 1; i <= 16; ++i)
            {
                TextBoxes[i] = new TextBox();
                this.Controls.Add(TextBoxes[i]);
                if (i <= 4)
                    TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 1) * 106, 12);
                else if (i > 4 && i <= 8)
                    TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 5) * 106, 40);
                else if (i > 8 && i <= 12)
                    TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 9) * 106, 68);
                else if (i > 12 && i <= 16)
                    TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 13) * 106, 96);
                //產生出16個TextBox,並排列整齊成為4x4的空格方陣
            }
            //在此環境下製作的TextBox會直接出現在form上
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int[,] a = new int[5, 5];
            int[,] b = new int[5, 5];
            int[,] c = new int[5, 5];
       
            int m = 5, n = 5;
  
            a[1, 1] = int.Parse(textBox1.Text);
            a[1, 2] = int.Parse(textBox5.Text);
            a[1, 3] = int.Parse(textBox9.Text);
            a[1, 4] = int.Parse(textBox13.Text);
            a[2, 1] = int.Parse(textBox2.Text);
            a[2, 2] = int.Parse(textBox6.Text);
            a[2, 3] = int.Parse(textBox10.Text);
            a[2, 4] = int.Parse(textBox14.Text);
            a[3, 1] = int.Parse(textBox3.Text);
            a[3, 2] = int.Parse(textBox7.Text);
            a[3, 3] = int.Parse(textBox11.Text);
            a[3, 4] = int.Parse(textBox15.Text);
            a[4, 1] = int.Parse(textBox4.Text);
            a[4, 2] = int.Parse(textBox8.Text);
            a[4, 3] = int.Parse(textBox12.Text);
            a[4, 4] = int.Parse(textBox16.Text);
            b[1, 1] = int.Parse(textBox17.Text);
            b[1, 2] = int.Parse(textBox21.Text);
            b[1, 3] = int.Parse(textBox25.Text);
            b[1, 4] = int.Parse(textBox29.Text);
            b[2, 1] = int.Parse(textBox18.Text);
            b[2, 2] = int.Parse(textBox22.Text);
            b[2, 3] = int.Parse(textBox26.Text);
            b[2, 4] = int.Parse(textBox30.Text);
            b[3, 1] = int.Parse(textBox19.Text);
            b[3, 2] = int.Parse(textBox23.Text);
            b[3, 3] = int.Parse(textBox27.Text);
            b[3, 4] = int.Parse(textBox31.Text);
            b[4, 1] = int.Parse(textBox20.Text);
            b[4, 2] = int.Parse(textBox24.Text);
            b[4, 3] = int.Parse(textBox28.Text);
            b[4, 4] = int.Parse(textBox32.Text);
           
            for (int i = 1; i < m; i++)
            {
                for (int j = 1; j < n; j++)
                {
                    for (int k = 1; k < m; k++)
                    {
                        c[i, j] = c[i, j] + a[i, k] * b[k, j];
                    }
                }
            }
            for (int i = 1; i <= 4; i++)
            {
                TextBoxes[i].Text = Convert.ToString(c[i, 1]);
                TextBoxes[i + 4].Text = Convert.ToString(c[i, 2]);
                TextBoxes[i + 8].Text = Convert.ToString(c[i, 3]);
                TextBoxes[i + 12].Text = Convert.ToString(c[i, 4]);
                //使用for迴圈呈現答案於16個Textbox之中
            }

        }
        private void textBox23_TextChanged(object sender, EventArgs e)
        {
        }
        private void button2_Click(object sender, EventArgs e)
        {
            int[,] a = new int[5, 5];
            int[,] b = new int[5, 5];
            int[,] c = new int[5, 5];
            int m = 5, n = 5;

            textBox1.Text = "1";
            textBox2.Text = "0";
            textBox3.Text = "0";
            textBox4.Text = "0";

            textBox5.Text = "0";
            textBox6.Text = "1";
            textBox7.Text = "0";
            textBox8.Text = "0";
            textBox9.Text = "0";
            textBox10.Text = "0";
            textBox11.Text = "1";
            textBox12.Text = "0";
            textBox13.Text = "0";
            textBox14.Text = "0";
            textBox15.Text = "0";
            textBox16.Text = "1";

            textBox17.Text = "1";
            textBox18.Text = "0";
            textBox19.Text = "0";
            textBox20.Text = "0";

            textBox21.Text = "0";
            textBox22.Text = "1";
            textBox23.Text = "0";
            textBox24.Text = "0";
            textBox25.Text = "0";
            textBox26.Text = "0";
            textBox27.Text = "1";
            textBox28.Text = "0";
            textBox29.Text = "0";
            textBox30.Text = "0";
            textBox31.Text = "0";
            textBox32.Text = "1";
            a[1, 1] = 1;
            a[1, 2] = 0;
            a[1, 3] = 0;
            a[1, 4] = 0;
            a[2, 1] = 0;
            a[2, 2] = 1;
            a[2, 3] = 0;
            a[2, 4] = 0;
            a[3, 1] = 0;
            a[3, 2] = 0;
            a[3, 3] = 1;
            a[3, 4] = 0;
            a[4, 1] = 0;
            a[4, 2] = 0;
            a[4, 3] = 0;
            a[4, 4] = 1;
            b[1, 1] = 1;
            b[1, 2] = 0;
            b[1, 3] = 0;
            b[1, 4] = 0;
            b[2, 1] = 0;
            b[2, 2] = 1;
            b[2, 3] = 0;
            b[2, 4] = 0;
            b[3, 1] = 0;
            b[3, 2] = 0;
            b[3, 3] = 1;
            b[3, 4] = 0;
            b[4, 1] = 0;
            b[4, 2] = 0;
            b[4, 3] = 0;
            b[4, 4] = 1;
           
          
            for (int i = 1; i < m; i++)
            {
                for (int j = 1; j < n; j++)
                {
                    for (int k = 1; k < m; k++)
                    {
                        c[i, j] = c[i, j] + a[i, k] * b[k, j];
                    }
                }
            }
            for (int i = 1; i <= 4; i++)
            {
                TextBoxes[i].Text = Convert.ToString(c[i, 1]);
                TextBoxes[i + 4].Text = Convert.ToString(c[i, 2]);
                TextBoxes[i + 8].Text = Convert.ToString(c[i, 3]);
                TextBoxes[i + 12].Text = Convert.ToString(c[i, 4]);
                //使用for迴圈呈現答案於16個Textbox之中
            }
        }
        }
    }

沒有留言:

張貼留言