?? 9-2-1.cpp
字號:
#include <iostream.h>
class Complex {
private:
double real, image; // 復數的實部和虛部
public:
Complex() { real = image = 0.0; }
Complex(double r) {real = r; image = 0.0; }
Complex(double r, double i)
{ real = r; image = i; };
Complex operator + (const Complex &c);
Complex operator - (const Complex &c);
Complex operator * (const Complex &c);
Complex operator / (const Complex &c);
Complex operator - ();
friend void print(const Complex &c);
};
inline Complex Complex::operator + (const Complex &c)
{ return Complex(real + c.real, image + c.image);
}
inline Complex Complex::operator - (const Complex &c)
{ return Complex(real - c.real, image - c.image);
}
inline Complex Complex::operator * (const Complex &c)
{ return Complex(real * c.real - image * c.image,
real * c.image + image * c.real);
}
inline Complex Complex::operator / (const Complex &c)
{ return Complex((real * c.real +image * c.image) /
(c.real * c.real + c.image * c.image),
(image * c.real + real * c.image) /
(c.real * c.real + c.image * c.image));
}
inline Complex Complex::operator - ()
{ return Complex( -real, -image);
}
void print(const Complex &c)
{ if(c.image < 0) cout <<c.real<<" - "<< -c.image<<" i" ;
else cout <<c.real<<" + "<<c.image<<" i" ;
cout << endl;
}
void main()
{ Complex c1(2.5), c2(3.6, -1.2), c3;
c3 = c1 + c2;
cout << "c1+c2 = "; print(c3);
c3 = c1 - c2;
cout << "c1-c2 = "; print(c3);
c3 = c1 * c2;
cout << "c1*c2 = "; print(c3);
c3 = c1 / c2;
cout << "c1/c2 = "; print(c3);
c3 = -c2;
cout << " - c2 = "; print(c3);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -