2010年10月15日 星期五

未完成的4X4矩陣(Use For loop)

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;
        public Form1()
        {
            InitializeComponent();
        }
        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] = 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];
                    }
                    Console.WriteLine(" c[" + i + ", " + j + "]=" + c[i, j]);
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Buttons = new System.Windows.Forms.Button[9];

            for (int i = 0; i < 9; ++i)
            {
                Buttons[i] = new Button();
                this.Controls.Add(Buttons[i]);
                if (i <= 2)
                    Buttons[i].Location = new System.Drawing.Point(10 + i * 80, 10);
                else if (i > 2 && i <= 5)
                    Buttons[i].Location = new System.Drawing.Point(10 + (i - 3) * 80, 40);
                else if (i > 5 && i <= 9)
                    Buttons[i].Location = new System.Drawing.Point(10 + (i - 6) * 80, 90);
            }
        }
    }
}

沒有留言:

張貼留言