LỜI NÓI ĐẦU Sách này trình bày các cấu trúc dữ liệu (CTDL) và thuật toán. Các kiến thức về CTDL và thuật toán đóng…
Xem thêm Sách cấu trúc dữ liệu và giải thuật [TS. Đinh Mạnh Tường]Tag: tập hợp
Code C# – Thực hiện các phép toán trên tập hợp
Lớp Set bên dưới sẽ cung cấp cho chúng ta các hàm thực hiện các phép toán trên tập hợp như: Giao, hợp, hiệu:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
class Set { private string m_Set = ""; #region Contructors public Set(string str_set) { this.m_Set = str_set; } #endregion #region Properties public string SET { get { return m_Set; } set { m_Set = value; } } public int SIZE { get { return this.m_Set.Split(new char[1] { ',' }).Length; } } #endregion #region Method /// <summary> /// Giao 2 tập hợp /// </summary> /// <param name="Set2"></param> /// <returns>Kết quả của phép giao 2 tập hợp</returns> public string Intersection(Set Set2) { string[] arr_Set1 = this.m_Set.Split(new char[1] { ',' }); string[] arr_Set2 = Set2.SET.Split(new char[1] { ',' }); string Resualt = ""; foreach (string item1 in arr_Set1) foreach (string item2 in arr_Set2) if (item1 == item2) Resualt += item1 + ","; if (Resualt.Length > 0) Resualt = Resualt.Substring(0, Resualt.Length - 1); return Resualt; } /// <summary> /// Hợp 2 tập hợp /// </summary> /// <param name="Set2"></param> /// <returns>Kết quả của phép hợp 2 tập hợp</returns> public string Union(Set Set2) { string[] arr_Set1 = this.m_Set.Split(new char[1] { ',' }); string[] arr_Set2 = Set2.SET.Split(new char[1] { ',' }); string Resualt = ""; if (this.IsNull()) Resualt = Set2.SET; else if (Set2.IsNull()) Resualt = this.SET; else { Resualt = this.SET + ","; foreach (string item2 in arr_Set2) { bool exits = false; foreach (string item1 in arr_Set1) { if (item1 == item2) { exits = true; break; } } if (exits == false) Resualt += item2 + ","; } if (Resualt.Length > 0) Resualt = Resualt.Substring(0, Resualt.Length - 1); } return Resualt; } /// <summary> /// Hiệu 2 tập hợp /// </summary> /// <param name="Set2"></param> /// <returns>Kết quả hiệu 2 tập hợp</returns> public string Minus(Set Set2) { string[] arr_Set1 = this.m_Set.Split(new char[1] { ',' }); string[] arr_Set2 = Set2.SET.Split(new char[1] { ',' }); string Resualt = ""; foreach (string item1 in arr_Set1) { bool exits = false; foreach (string item2 in arr_Set2) { if (item1 == item2) { exits = true; break; } } if (exits == false) Resualt += item1 + ","; } if (Resualt.Length > 0) Resualt = Resualt.Substring(0, Resualt.Length - 1); return Resualt; } /// <summary> /// Kiểm tra tập hợp có là tập rỗng hay không? /// </summary> /// <returns></returns> public bool IsNull() { bool resualt = true; if (this.m_Set.Length > 0) resualt = false; return resualt; } #endregion } |
…
Xem thêm Code C# – Thực hiện các phép toán trên tập hợp