Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Complex {
- private:
- double re, im;
- public:
- // ham tao thiet lap tu hai so
- Complex(double r = 0, double i = 0) : re(r) , im(i) { }
- // ham copy tu mot so phuc
- Complex (Complex &C): re(C.re), im(C.im) { }
- public:
- Complex operator + (Complex c);
- Complex operator - (Complex c);
- Complex operator * (Complex c);
- Complex operator / (Complex c);
- public:
- friend ostream& operator << (ostream& out, Complex &C) {
- return (out << '(' << C.re << ", " << C.im << "i) " );
- }
- };
- // Thiet ke cac toan tu cong, tru, nhan, chia
- Complex Complex:: operator + (Complex C)
- {
- return Complex(this->re + C.re, this -> im + C.im );
- }
- Complex Complex:: operator - (Complex C)
- {
- return Complex(this -> re C.re, this -> im - C.im );
- }
- Complex Complex:: operator * (Complex C)
- {
- return Complex(this -> re * C.re - this -> im * C.im, this -> re * C.im + this -> im * C.re);
- }
- Complex Complex:: operator / (Complex C)
- {
- return Complex((this -> re * C.re + this -> im * C.im)/(this -> re * this -> re + this -> im * this -> im), (this->re * C.im - this->im * C.re)/(this -> re * this -> re + this -> im * this -> im));
- }
- int main() {
- // khai bao bien
- double a = 2, y, z;
- y = Complex::Complex(1,2);
- z = Complex::Complex(0,6);
- // tinh thu
- cout << y << " + " << z << = << y + z + a << endl;
- cout << y << " - " << z << = << y - z << endl;
- cout << y << " * " << z << = << y * z << endl;
- cout << y << " / " << z << = << y / z << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement