Bài 10. Dữ liệu có cấu trúc (STRUCT)

Giới thiệu về kiểu dữ liệu cấu trúc

Một cấu trúc (struct) là một kiểu do người dùng định nghĩa, nó tương tự như lớp nhưng đơn giản hơn lớp.

Khai báo cấu trúc:

[thuộc tính] [kiểu truy cập] struct <định danh> [: <danh sách các giao diện >]
{
     // Các thành viên của cấu trúc
}

 

Ví dụ:

public struct ToaDo{
   private int x;
   private int y;
   public ToaDo(int xCoordinate, int yCoordinate){
     x = xCoordinate;
     y = yCoordinate;
  }
  public int X{
     get{ return x; }
     set{ x = value; }
  }

  public int Y{
     get{ return y; }
     set{ y = value; }
  }
}

public static void Main( ) {
     ToaDo td1 = new ToaDo(20,30);
     Console.WriteLine("Toa do 1: {0}", td1);    
 }

Cách tạo cấu trúc:

Muốn tạo một thể hiện của cấu trúc ta dùng từ khoá new.

ToaDo td1 = new ToaDo(20,30);


Ví dụ:

Ví dụ 1: Viết chương trình thực hiện:

- Nhập vào tạo độ 3 điểm A,B,C trong không gian. 
- Tính độ dài 3 đoạn thẳng AB,AC,BC. 
- Kiểm tra xem A,B,C có thẳng hàng không?

namespace TBit_3diemKG_Struct
{
   public struct ToaDo {
      private int x, y, z;
      public ToaDo(int x, int y, int z) {
         this.x = x;
         this.y = y;
         this.z = z;
      }
      public int X {
         get { return x; }
         set { x = value; }
     }
     public int Y {
        get { return y; }
        set { y = value; }
     }
      public int Z {
         get { return z; }
         set { z = value; }
      }
}

class Program
{
   static void Main(string[] args) {
      // Khai bao bien ToaDo 
      ToaDo A, B, C;
      double d1; // AB 
      double d2; // AC 
      double d3; // BC 
      // Nhap toa do 3 diem
      Console.WriteLine("\n Nhap toa do 3 diem: ");
      A = new ToaDo(1, 2, 3);
      B = new ToaDo(1, 4, 5);
      C = new ToaDo(1, 5, 7);
      d1 = Math.Sqrt((A.X - B.X) * (A.X - B.X) + (A.Y - B.Y) * (A.Y - B.Y) + (A.Z - B.Z) * (A.Z - B.Z));
      d2 = Math.Sqrt((A.X - C.X) * (A.X - C.X) + (A.Y - C.Y) * (A.Y - C.Y) + (A.Z - C.Z) * (A.Z - C.Z));
      d3 = Math.Sqrt((C.X - B.X) * (C.X - B.X) + (C.Y - B.Y) * (C.Y - B.Y) + (C.Z - B.Z) * (C.Z - B.Z));
      // In do dai 3 doan thang 
      Console.WriteLine("\n Do dai 3 doan thang: \n");
      Console.WriteLine("\n AB : " + d1);
      Console.WriteLine("\n AC : " + d2);
      Console.WriteLine("\n BC : " + d3);
      // Kiem tra tinh thang hang 
      if (d1 + d2 == d3 || d1 + d3 == d2 || d2 + d3 == d1)
         Console.WriteLine("\n 3 diem A,B,C thang hang");
      else
        Console.WriteLine("\n 3 diem A,B,C khong thang hang");
     Console.ReadLine()
      }
   }
}

 

Ví dụ 2: Viết các hàm thực hiện

- Nhập thông tin SV gồm: họ tên, địa chỉ, tuổi, điểm toán, lý, hóa 
- Tính điểm tổng kết 
- In thông tin sinh viên

namespace TBit_QLSinhVien_Struct
{
struct SV{
   string ht,dc; 
   int tuoi;
   float dT,dL,dH;
   public SV(string ht, string dc, int tuoi, float dt, float dl, float dh) {
      this.ht = ht;
      this.dc = dc;
      this.tuoi = tuoi;
      this.dT = dt;
      this.dL = dl;
      this.dH = dh;
   }
   public string HT {
      get { return ht; }
      set { ht = value; }
   }
   public string DC {
      get { return dc; }
      set { dc = value; }
   }
   public int TUOI {
      get { return tuoi; }
      set { tuoi = value; }
   }
   public float DT {
      get { return dT; }
      set { dT = value; }
   }
   public float DL {
      get { return dL; }
      set { dL = value; }
   }
   public float DH {
      get { return dH; }
      set { dH = value; }
   }
   public float TinhDiem() {
      return (dT+dL+dH)/3;
   }
   public void XEPLOAI() {
      string xeploai;
      float dtk = TinhDiem();
      // Xep loai sinh vien
      if (dtk < 5) xeploai = "Truot";
      else if (dtk < 6) xeploai = "Trung Binh";
      else if (dtk < 7) xeploai = "TB Kha";
      else if (dtk < 8) xeploai = "Kha";
      else if (dtk < 9) xeploai = "Gioi";
      else if (dtk < 10) xeploai = "Xuat Sac";
      else xeploai = "";
      // in thong tin sinh vien 
      Console.Write("\n Thong tin sinh vien: ");
      Console.Write("\n - Ho ten: " + ht);
      Console.Write("\n - Dia chi: " +dc);
      Console.Write("\n - Tuoi: " +tuoi);
      Console.Write("\n - Diem tong ket: " + dtk);
      Console.Write("\n - Xep loai: " + xeploai);
   }
}
class Program
{
static void Main(string[] args) {
   SV sv1 = new SV("Nguyen Minh Hien", "Ha Noi", 20, 9, 9, 9);
   sv1.XEPLOAI();
   Console.ReadLine();
   }
}
}