0% found this document useful (0 votes)
31 views

Class Private Int Private Int: Phanso

The document defines several classes for representing fractions (PHANSO), 3D points (Point3D), rectangles (HCN), squares (HinhVuong), and circles (HinhTron) in C#. The PHANSO class implements operators for fraction arithmetic. The Point3D class represents 3D coordinates and calculates distance between points. The HCN class uses Point3D to represent rectangle vertices and calculates perimeter and area. HinhVuong and HinhTron extend functionality to squares and circles.

Uploaded by

Oliver Pham
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Class Private Int Private Int: Phanso

The document defines several classes for representing fractions (PHANSO), 3D points (Point3D), rectangles (HCN), squares (HinhVuong), and circles (HinhTron) in C#. The PHANSO class implements operators for fraction arithmetic. The Point3D class represents 3D coordinates and calculates distance between points. The HCN class uses Point3D to represent rectangle vertices and calculates perimeter and area. HinhVuong and HinhTron extend functionality to squares and circles.

Uploaded by

Oliver Pham
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

class PHANSO { private int _tu; private int _mau; //Property get, set ^^ public int Tu { get { return

_tu; } set { _tu = value; } } public int Mau { get { return _mau; } set { _mau = value; } } //Phng thc khi to public PHANSO()//Khong tham so { //Gan tu mau = 1 de tranh TH 0/0 => loi~ this._tu = 1; this._mau = 1; } public PHANSO(int tu, int mau)//2 tham so { this._tu = tu; this._mau = mau; } public PHANSO(PHANSO ps) // 1 tham so (PT khoi tao sao chep) { this._tu = ps._tu; this._mau = ps._mau; } //Cau 1: a+=b: cau 1 zong zong cau 2 //Cau 2 : a+b , a-b, a*b , a/b public static PHANSO operator +(PHANSO ps1, PHANSO ps2) { //Khoi tao 1 doi tuong phan so kq de luu ket qua PHANSO kq = new PHANSO(); kq._tu = ps1._tu * ps2._mau + ps1._mau * ps2._tu; kq._mau = ps1._mau * ps2._mau; return kq; } public static PHANSO operator -(PHANSO ps1, PHANSO ps2) { PHANSO kq = new PHANSO(); kq._tu = ps1._tu * ps2._mau - ps1._mau * ps2._tu; kq._mau = ps1._mau * ps2._mau; return kq; } public static PHANSO operator *(PHANSO ps1, PHANSO ps2) { PHANSO kq = new PHANSO(); kq._tu = ps1._tu*ps2._tu;

kq._mau = ps1._mau * ps2._mau; return kq; } public static PHANSO operator /(PHANSO ps1, PHANSO ps2) { PHANSO kq = new PHANSO(); kq._tu = ps1._tu * ps2._mau; kq._mau = ps1._mau * ps2._tu; return kq; } //Cau 3 So sanh 2 phan so public static bool operator ==(PHANSO ps1, PHANSO ps2) { float kq1 = ps1._tu / ps1._mau;//chuyen phan so ve dang so thuc de so sanh float kq2 = ps2._tu / ps2._mau; if (kq1 == kq2) return true;//Tra ve true => 2 phan so = nhau return false;//Hai phan so ko bag nhau } public static bool operator !=(PHANSO ps1, PHANSO ps2) { float kq1 = ps1._tu / ps1._mau;//chuyen phan so ve dang so thuc de so sanh float kq2 = ps2._tu / ps2._mau; if (kq1 != kq2) return true;//Tra ve true => 2 phan so != nhau return false;//Hai phan so bag nhau } public static bool operator <(PHANSO ps1, PHANSO ps2) { float kq1 = ps1._tu / ps1._mau; float kq2 = ps2._tu / ps2._mau; if (kq1 < kq2) return true; return false; } public static bool operator <=(PHANSO ps1, PHANSO ps2) { float kq1 = ps1._tu / ps1._mau; float kq2 = ps2._tu / ps2._mau; if (kq1 <= kq2) return true; return false; } //Cau 4 public static bool operator >(PHANSO ps1, PHANSO ps2) { float kq1 = ps1._tu / ps1._mau; float kq2 = ps2._tu / ps2._mau; if (kq1 > kq2) return true; return false; }

//Cau 5 public static bool operator >=(PHANSO ps1, PHANSO ps2) { float kq1 = ps1._tu / ps1._mau; float kq2 = ps2._tu / ps2._mau; if (kq1 >= kq2) return true; return false; } //Cau 6: Cong voi 1 so nguyen public static PHANSO operator +(PHANSO ps, int n) { PHANSO kq = new PHANSO(); kq._tu = ps._tu * n + ps._mau * n;//1/2 + 3 = 1/2 +3/3 kq._mau = ps._mau * n; return kq; } //Cong 1 so nguyen voi 1 phan so public static PHANSO operator +( int n , PHANSO ps) { PHANSO kq = new PHANSO(); kq._tu = n * ps._tu + n * ps._mau; kq._mau = n * ps._mau; return kq; } // TInh luy thua 1 phan so public static PHANSO operator ^(PHANSO ps, int somu) { PHANSO kq = new PHANSO(); for (int i = 0; i < somu; i++) { kq = kq * ps;// a ^3 = a * a * a ! Mu bao nhieu thi nhan bay nhieu lan, Su dung lai toan tu * 2 phan so o tren } return kq; } //Cau 7 : public static PHANSO operator ++(PHANSO ps)//+ them 1/1 { ps._tu = ps._tu + ps._mau;// 1/2 + 1/1 = 1/2 + 2/2 ^^ return ps; } public static PHANSO operator --(PHANSO ps)//- 1/1 { ps._tu = ps._tu - ps._mau; return ps; } } Notes: S dng operator cho PHANSO tng t cho s thc, s nguyn Int a = 3+4; PHANSO ps = ps1 + ps2;

class Point3D { private int _x; private int _y; private int _z; //Property 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; } } //Phuong thuc khoi tao public Point3D() { this._x = 0; this._y = 0; this._z = 0; } public Point3D(int x, int y,int z) { this._x = x; this._y = y; this._z = z; } public Point3D(Point3D d) { this._x = d.X; this._y = d.Y; this._z = d.Z; } //Phuong thuc tinh toan //1 Tinh khoang cach 2 diem //Tinh khoang cach tu diem dang xet toi diem d =>> tuc la ta da co diem dang xet => goi la huong doi tuong :) public double TinhKhoangCach(Point3D d) { //(x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2 double kq = Math.Pow((d.X - this.X), 2) + Math.Pow((d.Y this.Y), 2) + Math.Pow((d.Z - this.Z), 2); //Can bac 2 => khoang? cach. kq = Math.Pow(kq, 2); return kq; } // ham tao vector : => google.

class HCN { //su dung lai lop Point3D private Point3D _a; private Point3D _b; private Point3D _c; private Point3D _d; // 4 dinh cua hinh chu nhat // get set public Point3D A { get { return _a; } set { _a = value; } } public Point3D B { get { return _b; } set { _b = value; } } public Point3D C { get { return _c; } set { _c = value; } } public Point3D D { get { return _d; } set { _d = value; } } public HCN() { Point3D d = new Point3D();//Thanh phan du lieu cua HCN la 4 diem nen phai khoi tao 1 diem de gan mac dinh cho 4 dinh this._a = this._b = this._c = this._d = d; } public HCN(Point3D d1, Point3D d2, Point3D d3,Point3D d4) { this._a = d1; this._b = d2; this._c = d3; this._d = d4; } public HCN(HCN h2) { this._a = h2._a; this._b = h2._b; this._c = h2._c; this._d = h2._d; } //Tinh chu vi public double TinhChuVi() { // tinh chieu dai 2 canh: double AB = this._a.TinhKhoangCach(this._b);//Chieu dai

double //Tinh double return

BC = this._b.TinhKhoangCach(this._c);//Chieu rong chu vi chuvi = (AB + BC) * 2; chuvi;

} public double TinhDienTich() { // tinh chieu dai 2 canh: double AB = this._a.TinhKhoangCach(this._b);//Chieu dai double BC = this._b.TinhKhoangCach(this._c);//Chieu rong //Tinh Dien tich double dientich = AB * BC; return dientich; } private int socanh() { return 4; }

class HinhVuong { Point3D _a; Point3D _b;//Hinh vuong chi can biet 1 canh(2 dinh) public Point3D A { get { return _a; } set { _a = value; } } public Point3D B { get { return _b; } set { _b = value; } } //Khoi tao public HinhVuong() { Point3D d = new Point3D(); this._a = d; this._b = d; } public HinhVuong(Point3D a, Point3D b) { this._a = a; this._b = b; } public HinhVuong(HinhVuong hv) { this._a = hv._a; this._b = hv._b; } //Tinh chu vi public double TinhChuVi() { double AB = this._a.TinhKhoangCach(this._b); double chuvi = (AB + AB) * 2; return chuvi; } public double TinhDienTich() { double AB = this._a.TinhKhoangCach(this._b); double dientich = AB * AB; return dientich; } public int SoCanh() { return 4; }

class HinhTron { private double _r; public double R { get { return _r; } set { _r = value; } } //Pthuc khoi tao public HinhTron() { this._r = 0; } public HinhTron(double r) { this._r = r; } public HinhTron(HinhTron ht) { this._r = ht._r; } //Tinh chu vi public double TinhChuVi() { double chuvi = this._r * 2 * 3.14; return chuvi; } public double TinhDienTich() { double dientich = this._r * this._r * 3.14; return dientich; } public int SoCanh() { return 0;//0 = khong biet :) } }

You might also like