Пример #1
0
            public static Matran operator *(Matran a, Matran b)
            {
                Matran m;
                int    hangC = a.h;
                int    cotC  = a.c;

                m = new Matran(hangC, cotC);
                if (a.c != b.h)
                {
                    Console.WriteLine("\n\t Khong nhan duoc 2 ma tran!!! \n");
                }
                else
                {
                    for (int i = 0; i < hangC; i++)
                    {
                        for (int j = 0; j < cotC; j++)
                        {
                            m.M[i, j] = 0;
                            for (int k = 0; k < b.h; k++)
                            {
                                m.M[i, j] += a.M[i, k] * b.M[k, j];
                            }
                        }
                    }
                }
                return(m);
            }
Пример #2
0
            public static Matran operator *(Matran a, Matran b)
            {
                Matran c = new Matran(a.hang, a.cot);

                for (int i = 0; i < a.hang; i++)
                {
                    for (int j = 0; j < b.cot; j++)
                    {
                        c.a[i, j] = 0;
                    }
                }
                for (int i = 0; i < a.hang; i++)    //hang cua ma tran thu nhat
                {
                    for (int j = 0; j < b.cot; j++) //cot cua ma tran thu hai
                    {
                        int sum = 0;
                        for (int k = 0; k < a.cot; k++)
                        {
                            sum = sum + a.a[i, k] * b.a[k, j];
                        }
                        c.a[i, j] = sum;
                    }
                }
                return(c);
            }
Пример #3
0
    public Matran Tong()
    {
        Matran t = new Matran
                       ();

        for (int i = 0; i < somt; ++i)
        {
            t = t.Tong(ds[i]);
        }
        return(t);
    }
Пример #4
0
 public Matran(Matran t2)
 {
     this.sh = t2.sh;
     this.sc = t2.sc;
     this.a  = new int[sh, sc];
     for (int i = 0; i < sh; i++)
     {
         for (int j = 0; j < sc; j++)
         {
             this.a[i, j] = t2.a[i, j];
         }
     }
 }
Пример #5
0
    public Matran Tong(Matran t2)
    {
        Matran t = new Matran();

        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                t.a[i, j] = this.a[i, j] + t2.a[i, j];
            }
        }
        return(t);
    }
Пример #6
0
 public void Nhap()
 {
     Console.Write("Nhap cap cho ma tran:");
     Matran.N = int.Parse(Console.ReadLine());
     Console.Write("Nhap so ma tran:");
     somt = int.Parse(Console.ReadLine());
     ds   = new Matran[somt];
     for (int i = 0; i < somt; ++i)
     {
         Console.WriteLine("Nhap ma tran thu " + i);
         ds[i] = new Matran();
         ds[i].Nhap();
     }
 }
Пример #7
0
            public Matran NghichDaoMaTran()
            {
                Matran c = new Matran(cot, hang);

                for (int i = 0; i < cot; i++)

                {
                    for (int j = 0; j < hang; j++)

                    {
                        c.a[i, j] = a[j, i];
                    }
                }
                return(c);
            }
Пример #8
0
            public static Matran operator +(Matran a, Matran b)
            {
                Matran c = new Matran(a.hang, a.cot);

                for (int i = 0; i < a.hang; i++)

                {
                    for (int j = 0; j < a.cot; j++)

                    {
                        c.a[i, j] = a.a[i, j] + b.a[i, j];
                    }
                }
                return(c);
            }
Пример #9
0
            public Matran chuyenvi(Matran a)
            {
                Matran m;
                int    hangC = a.h;
                int    cotC  = a.c;

                m = new Matran(hangC, cotC);
                for (int i = 0; i < hangC; i++)
                {
                    for (int j = 0; j < cotC; j++)
                    {
                        m.M[j, i] = a.M[i, j];
                    }
                }
                return(m);
            }
Пример #10
0
        static void Main(string[] args)
        {
            Matran a = new Matran();
            Matran b = new Matran();
            Matran c = new Matran();

            a.NhapMatran();
            b.NhapMatran();
            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine("Ma tran a da nhap");
            a.XuatMatran();
            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine("Ma tran b da nhap");
            b.XuatMatran();

            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine("Cong 2 ma tran");
            try
            {
                c = a + b;
                c.XuatMatran();
            }
            catch
            {
                Console.WriteLine("Khong the cong hai ma tran khong bang nhau");
            }

            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine("Nhan 2 ma tran");
            try
            {
                c = a * b;
                c.XuatMatran();
            }
            catch
            {
                Console.WriteLine("Khong the nhan hai ma tran khong bang nhau");
            }



            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine("Ma tran nghich dao");
            c = a.NghichDaoMaTran();
            c.XuatMatran();
            Console.ReadLine();
        }
Пример #11
0
 public Matran Hieu(Matran t2)
 {
     if (this.sh == t2.sh && this.sc == t2.sc)
     {
         Matran t1 = new Matran(this.sh, this.sc);
         for (int i = 0; i < sh; i++)
         {
             for (int j = 0; j < sc; j++)
             {
                 t1.a[i, j] = this.a[i, j] - t2.a[i, j];
             }
         }
         return(t1);
     }
     else
     {
         return(null);
     }
 }
Пример #12
0
 public Matran Tong(Matran t2)
 {
     if (this.sh == t2.sh && this.sc == t2.sc)
     {
         Matran t = new Matran(this.sh, this.sc);
         for (int i = 0; i < sh; i++)
         {
             for (int j = 0; j < sc; j++)
             {
                 t.a[i, j] = this.a[i, j] + t2.a[i, j];
             }
         }
         return(t);
     }
     else
     {
         return(null);
     }
 }
Пример #13
0
            public double Tongduongcheochinh(Matran a)
            {
                double sum = 0;
                int    kq;

                for (int i = 0; i < a.h; i++)
                {
                    for (int j = 0; j < a.c; j++)
                    {
                        if (i == j)
                        {
                            sum += a.M[i, j];
                        }
                    }
                    kq = (int)sum;
                }

                return(sum);
            }
Пример #14
0
            public static Matran operator +(Matran a, Matran b)
            {
                Matran m;
                int    hangC = a.h;
                int    cotC  = a.c;

                m = new Matran(hangC, cotC);
                if (a.h == b.h && a.c == b.c)
                {
                    for (int i = 0; i < hangC; i++)
                    {
                        for (int j = 0; j < cotC; j++)
                        {
                            m.M[i, j] = a.M[i, j] + b.M[i, j];
                        }
                    }
                }
                else
                {
                    Console.WriteLine("\n\t Khong cong duoc 2 ma tran!!! \n");
                }
                return(m);
            }
Пример #15
0
        static void Main(string[] args)
        {
            Matran m = new Matran();

            m.NhapMT();
            m.XuatMT();
            Matran n = new Matran();

            n.NhapMT();
            n.XuatMT();
            Console.WriteLine("\t\tTong 2 ma tran");
            Matran tong = m + n;

            tong.XuatMT();
            Console.WriteLine("\t\tHieu 2 ma tran");
            Matran hieu = m - n;

            hieu.XuatMT();
            Console.WriteLine("\t\tTich 2 ma tran");
            Matran tich = m * n;

            tich.XuatMT();
            Console.WriteLine("Nhap ma tran moi de chuyen vi!!!!");
            Matran k = new Matran();

            k.NhapMT();
            k.XuatMT();
            Console.WriteLine("\tChuyen vi ma tran vua nhap: ");
            Matran cv = new Matran();

            k = cv.chuyenvi(k);
            k.XuatMT();
            Console.WriteLine("So sanh 2 ma tran!!!!");
            Matran a = new Matran();

            m.NhapMT();
            m.XuatMT();
            Matran b = new Matran();

            n.NhapMT();
            n.XuatMT();
            Console.WriteLine("Hai ma tran bang nhau: {0}", a == b);
            Console.WriteLine("Hai ma tran khac nhau: {0}", a != b);
            Console.WriteLine("Nhap ma tran moi de tinh Tong duong cheo chinh !!!!");
            Matran h = new Matran();

            h.NhapMT();
            h.XuatMT();
            Console.WriteLine("\tTong duong cheo chinh cua ma tran vua nhap: ");
            Console.Write("Ket qua: {0}\n", h.Tongduongcheochinh(h));
            Console.WriteLine("\tKiem tra ma tran doi xung!!! ");
            Matran DX = new Matran();

            DX.NhapMT();
            DX.XuatMT();
            cv = cv.chuyenvi(DX);
            if (DX != cv)
            {
                Console.WriteLine("Ma Tran vua nhap khong phai ma tran vuong doi xung");
            }
            else
            {
                Console.WriteLine("Ma Tran vua nhap la ma tran vuong doi xung");
            }
            Console.ReadLine();
        }