Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- class Complex {
- private:
- double re, im;
- public:
- Complex(double re_new = 0, double im_new = 0): re(re_new), im(im_new) {
- }
- double Re() const {
- return re;
- }
- double Im() const {
- return im;
- }
- };
- double abs(const Complex& c) {
- return sqrt(c.Re() * c.Re() + c.Im() * c.Im());
- }
- Complex operator + (const Complex& z) { // унарный плюс
- return Complex(+z.Re(), +z.Im());
- }
- Complex operator - (const Complex& z) { // унарный минус
- return Complex(-z.Re(), -z.Im());
- }
- Complex operator + (const Complex& a, const Complex& b) { // бинарный плюс
- return Complex(
- a.Re() + b.Re(),
- a.Im() + b.Im());
- }
- Complex operator - (const Complex& a, const Complex& b) { // бинарный минус
- return Complex( a.Re() - b.Re(), a.Im() - b.Im() );
- }
- Complex operator * (const Complex& a, const Complex& b) { // умножение
- return Complex(
- a.Re() * b.Re() - a.Im() * b.Im(),
- a.Re() * b.Im() + a.Im() * b.Re());
- }
- Complex operator / (Complex &c1, Complex &c2) {
- return Complex(
- (c1.Re() * c2.Re() + c1.Im() * c2.Im()) / (c2.Re() * c2.Re() + c2.Im() * c2.Im()),
- (c2.Re() * c1.Im() - c1.Re() * c2.Im()) / (c2.Re() * c2.Re() + c2.Im() * c2.Im()));
- }
- bool operator== (const Complex &c1, const Complex &c2) {
- return c1.Re() == c2.Re() && c1.Im() == c2.Im();
- }
- bool operator!= (const Complex &c1, const Complex &c2) {
- return c1.Re() != c2.Re() || c1.Im() != c2.Im();
- }
- int main() {
- Complex c1(1, 3);
- Complex c2(2, 4);
- cout << c1.Re() << " " << c1.Im() << endl;
- cout << c2.Re() << " " << c2.Im() << endl;
- if (c1 == c2) {
- cout << 1;
- }
- else {
- cout << "error" << endl;
- }
- c1 = c1 + c2;
- cout << c1.Re() << " " << c1.Im() << endl;
- cout << c2.Re() << " " << c2.Im() << endl;
- c1 = c1 - c2;
- c2 = c1 - c2;
- cout << c1.Re() << " " << c1.Im() << endl;
- cout << c2.Re() << " " << c2.Im() << endl;
- cout << abs(c1) << " " << abs(c2) << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment