Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class SoPhuc
- {
- public:
- SoPhuc(double Thuc = 0, double Ao = 0)
- {
- this->Ao = Ao;
- this->Thuc = Thuc;
- }
- friend istream& operator >> (istream& in, SoPhuc &p);
- friend ostream& operator << (ostream& out, SoPhuc p);
- friend bool operator > (SoPhuc a, SoPhuc b);
- friend bool operator < (SoPhuc a, SoPhuc b);
- friend bool operator == (SoPhuc a, SoPhuc b);
- friend bool operator <= (SoPhuc a, SoPhuc b);
- friend bool operator >= (SoPhuc a, SoPhuc b);
- friend SoPhuc operator +(SoPhuc a, SoPhuc b);
- friend SoPhuc operator -(SoPhuc a, SoPhuc b);
- friend SoPhuc operator *(SoPhuc a, SoPhuc b);
- friend SoPhuc operator /(SoPhuc a, SoPhuc b);
- friend void operator += (SoPhuc &a, SoPhuc b);
- friend void operator -= (SoPhuc &a, SoPhuc b);
- friend void operator *= (SoPhuc &a, SoPhuc b);
- friend void operator /= (SoPhuc &a, SoPhuc b);
- private:
- double Thuc, Ao;
- };
- //Toán tử nhập
- istream& operator >> (istream& in, SoPhuc &PS)
- {
- cout << "Nhap phuc thuc: ";
- in >> PS.Thuc;
- cout << "Nhap phan ao : ";
- in >> PS.Ao;
- return in;
- }
- //Toán tử xuất
- ostream& operator << (ostream& out, SoPhuc PS)
- {
- out << "z = ";
- if (PS.Ao != 0)
- {
- if(PS.Ao > 0 ) out << PS.Thuc << " + " << PS.Ao << "i";
- else out << PS.Thuc << " - " << -1 * PS.Ao << "i";
- }
- else out << "0";
- return out;
- }
- //So sánh ==,
- bool operator == (SoPhuc a, SoPhuc b)
- {
- return (a.Thuc == b.Thuc && a.Ao == b.Ao);
- }
- //Toán tử +, -, *, /
- SoPhuc operator + (SoPhuc a, SoPhuc b)
- {
- SoPhuc Kq;
- Kq.Ao = a.Ao + b.Ao;
- Kq.Thuc = a.Thuc + b.Thuc;
- return Kq;
- }
- SoPhuc operator - (SoPhuc a, SoPhuc b)
- {
- SoPhuc Kq;
- Kq.Ao = a.Ao - b.Ao;
- Kq.Thuc = a.Thuc - b.Thuc;
- return Kq;
- }
- SoPhuc operator * (SoPhuc a, SoPhuc b)
- {
- SoPhuc Kq;
- Kq.Thuc = a.Thuc * b.Thuc - a.Ao * b.Ao;
- Kq.Ao = a.Thuc * b.Ao + a.Ao * b.Thuc;
- return Kq;
- }
- SoPhuc operator / (SoPhuc a, SoPhuc b)
- {
- SoPhuc Kq;
- Kq.Ao = (-a.Thuc * b.Ao + a.Ao * b.Thuc) / (b.Thuc * b.Thuc + b.Ao * b.Ao);
- Kq.Thuc = (a.Thuc * b.Thuc + a.Ao * b.Ao) / (b.Thuc * b.Thuc + b.Ao * b.Ao);
- return Kq;
- }
- //Toán tử +=, -=; *=, /=
- void operator += (SoPhuc &a, SoPhuc b)
- {
- a = a + b;
- }
- void operator -= (SoPhuc &a, SoPhuc b)
- {
- a = a - b;
- }
- void operator *= (SoPhuc &a, SoPhuc b)
- {
- a = a * b;
- }
- void operator /= (SoPhuc &a, SoPhuc b)
- {
- a = a / b;
- }
- void main()
- {
- SoPhuc P(5, -1), a(3, 2);
- float b = 1.1;
- cout << a + P << endl;
- cout << a - P << endl;
- cout << a * P << endl;
- cout << a / P << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement