2010年10月15日 星期五

4x4矩陣完成版(Use for迴圈and物件產生)

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
    {
        public Form1()
        {
            InitializeComponent();
        }

        System.Windows.Forms.TextBox[] TextBoxes;
        //先宣告TextBox的矩陣

        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] = 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] = 4;
         
            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迴圈計算矩陣乘法

            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之中
            }
        }
    }
}

沒有留言:

張貼留言