Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class PhanSo
- {
- public:
- PhanSo(int TuSo = 0, int MauSo = 1)
- {
- this->MauSo = MauSo;
- this->TuSo = TuSo;
- }
- PhanSo(double a)
- {
- this->MauSo = 1;
- while (a != int(a))
- {
- a = a * 10;
- this->MauSo *= 10;
- }
- this->TuSo = a;
- RutGon();
- }
- void RutGon();
- friend istream& operator >> (istream& in, PhanSo &p);
- friend ostream& operator << (ostream& out, PhanSo p);
- friend bool operator > (PhanSo a, PhanSo b);
- friend bool operator < (PhanSo a, PhanSo b);
- friend bool operator == (PhanSo a, PhanSo b);
- friend bool operator <= (PhanSo a, PhanSo b);
- friend bool operator >= (PhanSo a, PhanSo b);
- friend PhanSo operator +(PhanSo a, PhanSo b);
- friend PhanSo operator -(PhanSo a, PhanSo b);
- friend PhanSo operator *(PhanSo a, PhanSo b);
- friend PhanSo operator /(PhanSo a, PhanSo b);
- friend void operator += (PhanSo &a, PhanSo b);
- friend void operator -= (PhanSo &a, PhanSo b);
- friend void operator *= (PhanSo &a, PhanSo b);
- friend void operator /= (PhanSo &a, PhanSo b);
- private:
- int TuSo, MauSo;
- };
- //Rút Gọn 1 phân số
- void PhanSo::RutGon()
- {
- int iDau = 1;
- if (this->TuSo * this->MauSo < 0) iDau = -1;
- this->TuSo = abs(this->TuSo);
- this->MauSo = abs(this->MauSo);
- for (int i = this->TuSo; i > 1; i--)
- {
- if (this->TuSo % i == 0 && this->MauSo % i == 0)
- {
- this->TuSo /= i;
- this->MauSo /= i;
- break;
- }
- }
- this->TuSo *= iDau;
- }
- //Toán tử nhập
- istream& operator >> (istream& in, PhanSo &PS)
- {
- cout << "Nhap tu so : ";
- in >> PS.TuSo;
- cout << "Nhap mau so: ";
- in >> PS.MauSo;
- if (PS.MauSo == 0)
- {
- cout << "ERROR\n";
- cin >> PS;
- }
- return in;
- }
- //Toán tử xuất
- ostream& operator << (ostream& out, PhanSo PS)
- {
- if (PS.TuSo != 0)
- {
- out << PS.TuSo;
- if (PS.MauSo != 1)
- {
- cout << " / ";
- out << PS.MauSo;
- }
- }
- else
- out << "0";
- return out;
- }
- //So sánh >, <, ==, >=, <=
- bool operator > (PhanSo a, PhanSo b)
- {
- return (float(a.TuSo) / a.MauSo > float(b.TuSo) / b.MauSo);
- }
- bool operator < (PhanSo a, PhanSo b)
- {
- return (float(a.TuSo) / a.MauSo < float(b.TuSo) / b.MauSo);
- }
- bool operator == (PhanSo a, PhanSo b)
- {
- return (float(a.TuSo) / a.MauSo == float(b.TuSo) / b.MauSo);
- }
- bool operator >= (PhanSo a, PhanSo b)
- {
- return (float(a.TuSo) / a.MauSo >= float(b.TuSo) / b.MauSo);
- }
- bool operator <= (PhanSo a, PhanSo b)
- {
- return (float(a.TuSo) / a.MauSo <= float(b.TuSo) / b.MauSo);
- }
- //Toán tử +, -, *, /
- PhanSo operator + (PhanSo a, PhanSo b)
- {
- PhanSo Kq;
- Kq.MauSo = a.MauSo * b.MauSo;
- Kq.TuSo = a.TuSo * b.MauSo + a.MauSo * b.TuSo;
- Kq.RutGon();
- return Kq;
- }
- PhanSo operator - (PhanSo a, PhanSo b)
- {
- PhanSo Kq;
- Kq.MauSo = a.MauSo * b.MauSo;
- Kq.TuSo = a.TuSo * b.MauSo - a.MauSo * b.TuSo;
- Kq.RutGon();
- return Kq;
- }
- PhanSo operator * (PhanSo a, PhanSo b)
- {
- PhanSo Kq;
- Kq.MauSo = a.MauSo * b.MauSo;
- Kq.TuSo = a.TuSo * b.TuSo;
- Kq.RutGon();
- return Kq;
- }
- PhanSo operator / (PhanSo a, PhanSo b)
- {
- PhanSo Kq;
- Kq.MauSo = a.MauSo * b.TuSo;
- Kq.TuSo = a.TuSo * b.MauSo;
- Kq.RutGon();
- return Kq;
- }
- //Toán tử +=, -=; *=, /=
- void operator += (PhanSo &a, PhanSo b)
- {
- a = a + b;
- }
- void operator -= (PhanSo &a, PhanSo b)
- {
- a = a - b;
- }
- void operator *= (PhanSo &a, PhanSo b)
- {
- a = a * b;
- }
- void operator /= (PhanSo &a, PhanSo b)
- {
- a = a / b;
- }
- void main()
- {
- PhanSo P(1, 1), a;
- a = P;
- float b = 1.1;
- cout << 1 + a + 1.1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement