Blog entry by 97213529 郭益銘

Gambar dari 97213529 郭益銘
oleh 97213529 郭益銘 - Senin, 22 Desember 2008, 01:50
Anyone in the world

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication7
{
    class matrix
    {
        public int[,] a;
        public matrix(int dog1,int dog2)//見夠子
        {
            a = new int[dog1, dog2];
        }
        public void insert()//書入資廖
        {
            Console.WriteLine("開始輸入矩陣的資料");
            for (int i = 0; i < a.GetLength(0); i++)
            {
                for (int j = 0; j < a.GetLength(1); j++)
                {
                    Console.Write("輸入第[{0},{1}] 的資料: ", i, j);
                    a[i, j] = Int32.Parse(Console.ReadLine());
                }
            }
        }
        public static matrix operator *(matrix dog1, matrix dog2) //****
        {
            matrix c = new matrix(dog1.a.GetLength(0),dog2.a.GetLength(1));
         
            for (int i = 0; i < c.a.GetLength(0); i++)
            {
                for (int j = 0; j < c.a.GetLength(1); j++)
                {
                    for (int k = 0; k < dog1.a.GetLength(1); k++)
                    {
                        c.a[i, j] += dog1.a[i, k] * dog2.a[k, j];
                    }
                }
            }
            return c;
        }
 
        public void Print(matrix dog)
        {
            Console.WriteLine("a 陣列是 ");
            for (int i = 0; i < dog.a.GetLength(0); i++)
            {
                for (int j = 0; j < dog.a.GetLength(1); j++)
                {
                    Console.Write("{0} ", dog.a[i, j]);
                }
                Console.WriteLine();
            }
        }
      

    }

    class Program
    {
       
        static void Main(string[] args)
        {
            int v, x, y, z;
            Console.WriteLine("請輸入第一個矩陣第一維度的大小");
            v = Int32.Parse(Console.ReadLine());
            Console.WriteLine("請輸入第一個矩陣第二維度的大小");
            x = Int32.Parse(Console.ReadLine());

            matrix m1 = new matrix(v, x);
            m1.insert();

            Console.WriteLine("請輸入第二個矩陣第一維度的大小");
            y = Int32.Parse(Console.ReadLine());
            Console.WriteLine("請輸入第二個矩陣第二維度的大小");
            z = Int32.Parse(Console.ReadLine());

            matrix m2 = new matrix(y, z);
            m2.insert();

            if (x == y)
            {
                m1.Print(m1 * m2);
            }
            else
            {
                Console.WriteLine("第一矩陣的第二維度不等於第二矩陣的第一維度,故不能相乘");
            }

 

        }
    }
}

 

 

  
RSS