2010年10月22日 星期五

2010/10/15 按鈕陣列 乘法矩陣 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.TextBox[] TextBox1;
        System.Windows.Forms.TextBox[] TextBox2;
        System.Windows.Forms.Button[] Button3;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            TextBox1 = new System.Windows.Forms.TextBox[16];
            TextBox2 = new System.Windows.Forms.TextBox[16];
            Button3 = new System.Windows.Forms.Button[16];

            for (int d = 0; d < 16; d++)
            {
                TextBox1[d] = new TextBox();
                this.Controls.Add(TextBox1[d]);
                if (d <= 3)
                    TextBox1[d].Location = new System.Drawing.Point(10 + d * 100, 10);
                else if (d > 3 && d <= 7)
                    TextBox1[d].Location = new System.Drawing.Point(10 + (d - 4) * 100, 40);
                else if (d > 7 && d <= 11)
                    TextBox1[d].Location = new System.Drawing.Point(10 + (d - 8) * 100, 70);
                else if (d > 11 && d <= 16)
                    TextBox1[d].Location = new System.Drawing.Point(10 + (d - 12) * 100, 100);

            }
            for (int f = 0; f < 16; f++)
            {
                TextBox2[f] = new TextBox();
                this.Controls.Add(TextBox2[f]);
                if (f <= 3)
                    TextBox2[f].Location = new System.Drawing.Point(10 + f * 100, 10 + 130);
                else if (f > 3 && f <= 7)
                    TextBox2[f].Location = new System.Drawing.Point(10 + (f - 4) * 100, 40 + 130);
                else if (f > 7 && f <= 11)
                    TextBox2[f].Location = new System.Drawing.Point(10 + (f - 8) * 100, 70 + 130);
                else if (f > 11 && f <= 16)
                    TextBox2[f].Location = new System.Drawing.Point(10 + (f - 12) * 100, 100 + 130);

            }
            for (int g = 0; g < 16; g++)
            {
                Button3[g] = new Button();
                this.Controls.Add(Button3[g]);
                if (g <= 3)
                    Button3[g].Location = new System.Drawing.Point(10 + g * 80, 10 + 130 + 130);
                else if (g > 3 && g <= 7)
                    Button3[g].Location = new System.Drawing.Point(10 + (g - 4) * 80, 40 + 130 + 130);
                else if (g > 7 && g <= 11)
                    Button3[g].Location = new System.Drawing.Point(10 + (g - 8) * 80, 70 + 130 + 130);
                else if (g > 11 && g <= 16)
                    Button3[g].Location = new System.Drawing.Point(10 + (g - 12) * 80, 100 + 130 + 130);
            }
        }
        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, n;
            m = 5;
            n = 5;

            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    for (int k = 0; k < m; k++)
                    {
                        c[i, j] = c[i, j] + a[i, k] * b[k, j];
                    }
                    a[1, 1] = Convert.ToInt16(TextBox1[0].Text);
                    a[1, 2] = Convert.ToInt16(TextBox1[1].Text);
                    a[1, 3] = Convert.ToInt16(TextBox1[2].Text);
                    a[1, 4] = Convert.ToInt16(TextBox1[3].Text);
                    a[2, 1] = Convert.ToInt16(TextBox1[4].Text);
                    a[2, 2] = Convert.ToInt16(TextBox1[5].Text);
                    a[2, 3] = Convert.ToInt16(TextBox1[6].Text);
                    a[2, 4] = Convert.ToInt16(TextBox1[7].Text);
                    a[3, 1] = Convert.ToInt16(TextBox1[8].Text);
                    a[3, 2] = Convert.ToInt16(TextBox1[9].Text);
                    a[3, 3] = Convert.ToInt16(TextBox1[10].Text);
                    a[3, 4] = Convert.ToInt16(TextBox1[11].Text);
                    a[4, 1] = Convert.ToInt16(TextBox1[12].Text);
                    a[4, 2] = Convert.ToInt16(TextBox1[13].Text);
                    a[4, 3] = Convert.ToInt16(TextBox1[14].Text);
                    a[4, 4] = Convert.ToInt16(TextBox1[15].Text);
                    b[1, 1] = Convert.ToInt16(TextBox2[0].Text);
                    b[1, 2] = Convert.ToInt16(TextBox2[1].Text);
                    b[1, 3] = Convert.ToInt16(TextBox2[2].Text);
                    b[1, 4] = Convert.ToInt16(TextBox2[3].Text);
                    b[2, 1] = Convert.ToInt16(TextBox2[4].Text);
                    b[2, 2] = Convert.ToInt16(TextBox2[5].Text);
                    b[2, 3] = Convert.ToInt16(TextBox2[6].Text);
                    b[2, 4] = Convert.ToInt16(TextBox2[7].Text);
                    b[3, 1] = Convert.ToInt16(TextBox2[8].Text);
                    b[3, 2] = Convert.ToInt16(TextBox2[9].Text);
                    b[3, 3] = Convert.ToInt16(TextBox2[10].Text);
                    b[3, 4] = Convert.ToInt16(TextBox2[11].Text);
                    b[4, 1] = Convert.ToInt16(TextBox2[12].Text);
                    b[4, 2] = Convert.ToInt16(TextBox2[13].Text);
                    b[4, 3] = Convert.ToInt16(TextBox2[14].Text);
                    b[4, 4] = Convert.ToInt16(TextBox2[15].Text);


                    Button3[0].Text = Convert.ToString(c[1, 1]);
                    Button3[1].Text = Convert.ToString(c[1, 2]);
                    Button3[2].Text = Convert.ToString(c[1, 3]);
                    Button3[3].Text = Convert.ToString(c[1, 4]);
                    Button3[4].Text = Convert.ToString(c[2, 1]);
                    Button3[5].Text = Convert.ToString(c[2, 2]);
                    Button3[6].Text = Convert.ToString(c[2, 3]);
                    Button3[7].Text = Convert.ToString(c[2, 4]);
                    Button3[8].Text = Convert.ToString(c[3, 1]);
                    Button3[9].Text = Convert.ToString(c[3, 2]);
                    Button3[10].Text = Convert.ToString(c[3, 3]);
                    Button3[11].Text = Convert.ToString(c[3, 4]);
                    Button3[12].Text = Convert.ToString(c[4, 1]);
                    Button3[13].Text = Convert.ToString(c[4, 2]);
                    Button3[14].Text = Convert.ToString(c[4, 3]);
                    Button3[15].Text = Convert.ToString(c[4, 4]);
                }
            }
        }
        }
    }

沒有留言:

張貼留言