Code bài tập xử lý ma trận trong C# - Lập trình hướng đối tượng. 
Bài tập này mình làm cho môn lập trình hướng đối tượng @.@ Nhắc đến lập trình là cũng hơi ngán rồi. Bài tập tính toán ma trận này gồm các hàm: Nhập ma trận, cập nhật phần tử ma trận, tính tổng cộng 2 ma trận, hiệu 2 ma trận, tích 2 ma trận, xuất ma trận. Ở Code C các bạn chuyển đổi lại theo C
Bạn nào có cần bài tập xử lý ma trận trong C# - Lập trình hướng đối tượng thì tham khảo code nhé !
Code:
Bài tập này mình làm cho môn lập trình hướng đối tượng @.@ Nhắc đến lập trình là cũng hơi ngán rồi. Bài tập tính toán ma trận này gồm các hàm: Nhập ma trận, cập nhật phần tử ma trận, tính tổng cộng 2 ma trận, hiệu 2 ma trận, tích 2 ma trận, xuất ma trận. Ở Code C các bạn chuyển đổi lại theo C
![]()  | 
| Ma trận trong C | 
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Matran
{
    // Cac thuoc tinh
    private int So_Hang;
    private int So_Cot;
    private int[,] MT;
    // Phuong thuc khoi tao
    public Matran(int sh, int sc)
    {
        this.So_Hang = sh;
        this.So_Cot = sc;
        this.MT = new int[sh, sc];
    }
    // Phuong thuc Nhap ma tran
    public void NhapMT()
    {
        Console.Write("\n\t Nhap so hang cua ma tran: ");
        this.So_Hang = int.Parse(Console.ReadLine());
        Console.Write("\n\t Nhap so cot cua ma tran: ");
        this.So_Cot = int.Parse(Console.ReadLine());
        for (int i = 1; i <= this.So_Hang; i++)
            for (int j = 1; j <= this.So_Cot; j++)
            {
                Console.Write("\n\t - Nhap phan tu thu A[" + i + "," + j + "]: ");
                this.MT[i, j] = int.Parse(Console.ReadLine());
            }
    }
    // Tim kiem phan tu ma tran
    public int Tim_Kiem(int x)
    {
        for (int i = 1; i <= this.So_Hang; i++)
            for (int j = 1; j <= this.So_Cot; j++)
                if (this.MT[i, j] == x)
                    return 1;
                return 0;
    }
    // Cap nhat phan tu
    public void CapNhat(int m, int n, int pos)
    {
        this.MT[m, n] = pos;
    }
    // Cong 2 ma tran
    public Matran Cong2MT(Matran a, Matran b)
    {
        Matran c = new Matran(this.So_Hang,this.So_Cot);
        if (a.So_Hang == b.So_Hang && a.So_Cot == b.So_Cot)
        {
            for (int i = 0; i < this.So_Hang; i++)
            {
                for (int j = 0; j < this.So_Cot; j++)
                {
                    c.MT[i, j] = a.MT[i, j] + b.MT[i, j];
                }
            }
        }
        else
        {
            Console.WriteLine("\n\t Khong cong duoc !!! \n");
        }
        return c;
    }
    // Tru 2 ma tran
    public Matran Hieu2MT(Matran a, Matran b)
    {
        Matran c = new Matran(this.So_Hang, this.So_Cot);
        if (a.So_Hang == b.So_Hang && a.So_Cot == b.So_Cot)
        {
            for (int i = 0; i < this.So_Hang; i++)
            {
                for (int j = 0; j < this.So_Cot; j++)
                {
                    c.MT[i, j] = a.MT[i, j] - b.MT[i, j];
                }
            }
        }
        else
        {
            Console.WriteLine("\n\t Khong tru duoc !!!");
        }
        return c;
    }
    // Tich 2 ma tran
    public Matran Tich2MT(Matran a, Matran b)
    {
        Matran c = new Matran(this.So_Hang, this.So_Cot);
        if (a.So_Cot != b.So_Hang)
        {
            Console.Write("\n\t ERROR: Khong thuc hien dc phep nhan !!!");
        }
        else
        {
            for (int i = 0; i < this.So_Hang; i++)
                for (int j = 0; j < this.So_Cot; j++)
                {
                    c.MT[i, j] = 0;
                    for (int k = 0; k < b.So_Hang; k++)
                    {
                        c.MT[i, j] += a.MT[i, k] * b.MT[k, j];
                    }
                }
        }
        return c;
    }
    // Xuat Ma tran
    public void XuatMT()
    {
        Console.WriteLine("\t Ma tran: \n");
        for (int i = 1; i <= this.So_Hang; i++)
        {
            for (int j = 1; j <= this.So_Cot; j++)
            {
                Console.Write("\t" + this.MT[i,j] +" ");
            }
            Console.WriteLine();
        }
    }
    // Menu 9
    public void Menu(out byte chon)
    {
        Console.Clear();
        Console.WriteLine("\n\t|| BAN DA LUA CHON 9 - BAI TOAN MA TRAN   || ");
        Console.WriteLine("\t||----------------------------------------||");
        Console.WriteLine("\t|| 1. Nhap Ma Tran                        ||");
        Console.WriteLine("\t|| 2. Cap nhap phan tu Ma Tran.           ||");
        Console.WriteLine("\t|| 3. Tong hai Ma tran                    ||");
        Console.WriteLine("\t|| 4. Hieu hai Ma tran                    ||");
        Console.WriteLine("\t|| 5. Tich hai Ma Tran                    ||");
        Console.WriteLine("\t|| 6. Xuat Ma Tran                        ||");
        Console.WriteLine("\t|| 0. Quay ve Menu chinh                  ||");
        Console.WriteLine("\t||________________________________________||\n");
        Console.Write("\n\tNHAP LUA CHON: ");
        chon = byte.Parse(Console.ReadLine());
    }
    // Main 9
    public void Main()
    {
        Matran M1 = new Matran(50,50);
        Matran M2 = new Matran(50,50);
        Matran M3 = new Matran(50,50);
        byte chon;
        int pos; 
        int h, c, n;
        //int[,] a;
        do
        {
            Menu9(out chon);
            switch(chon)
            {
                case 1:
                    M1.NhapMT();
                    break;
                case 2:
                    Console.Write("\n\t Nhap vi tri phan tu can cap nhat \n");
                    Console.Write("\t\tHang: ");
                    h = int.Parse(Console.ReadLine());
                    Console.Write("\n\t\t Cot: ");
                    c = int.Parse(Console.ReadLine());
                    Console.Write("\n\t\t Gia tri: ");
                    n = int.Parse(Console.ReadLine());
                    if (M1.Tim_Kiem(n) == -1)
                    {
                        Console.Write("\n\t KET QUA TIM KIEM KHONG HOP LE !!!");
                    }
                    else
                    {
                        Console.Write("\n\t Nhap gia tri moi: ");
                        pos = int.Parse(Console.ReadLine());
                        M1.CapNhat(h, c, pos);
                    }
                    Console.ReadLine();
                    break;
                case 3:
                    Console.WriteLine("\n\t Nhap ma tran 1: ");
                    M1.NhapMT();
                    Console.WriteLine("\n\t Nhap ma tran thu 2: ");
                    M2.NhapMT();
                    Console.WriteLine("\t Tong hai ma tran la: ");
                    M3.Cong2MT(M1,M2);
                    M3.XuatMT();
                    Console.ReadLine();
                    break;
                case 4:
                    Console.WriteLine("\n\t Nhap ma tran thu 1: ");
                    M1.NhapMT();
                    Console.WriteLine("\n\t Nhap ma tran thu 2: ");
                    M2.NhapMT();
                    Console.WriteLine("\t Hieu hai ma tran la: ");
                    M3.Hieu2MT(M1, M2);
                    M3.XuatMT();
                    Console.ReadLine();
                    break;
                case 5:
                    Console.WriteLine("\n\t Nhap ma tran thu 1: ");
                    M1.NhapMT();
                    Console.WriteLine("\n\t Nhap ma tran thu 2: ");
                    M2.NhapMT();
                    Console.WriteLine("\n\t Tich 2 Ma tran la: ");
                    M3.Tich2MT(M1,M2);
                    M3.XuatMT();
                    Console.ReadLine();
                    break;
                case 6:
                    M1.XuatMT();
                    Console.ReadLine();
                    break;
                case 0:
                    Console.Clear();
                    break;
                default:
                    Console.Write("\n KHONG CO LUA CHON NAY !!!");
                    break;
            }
        } while(chon!=0);
    }
}







0 nhận xét:
Post a Comment