Amonin

Untitled

Dec 6th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. class Complex {
  5. private:
  6.     double re, im;
  7.  
  8. public:
  9.     Complex(double re_new = 0, double im_new = 0): re(re_new), im(im_new) {
  10.     }
  11.     double Re() const {
  12.         return re;
  13.     }
  14.     double Im() const {
  15.         return im;
  16.     }
  17. };
  18. double abs(const Complex& c) {
  19.     return sqrt(c.Re() * c.Re() + c.Im() * c.Im());
  20. }
  21. Complex operator + (const Complex& z) {  // унарный плюс
  22.     return Complex(+z.Re(), +z.Im());
  23. }
  24. Complex operator - (const Complex& z) {  // унарный минус
  25.     return Complex(-z.Re(), -z.Im());
  26. }
  27.  
  28. Complex operator + (const Complex& a, const Complex& b) {  // бинарный плюс
  29.     return Complex(
  30.         a.Re() + b.Re(),
  31.         a.Im() + b.Im());
  32. }
  33.  
  34. Complex operator - (const Complex& a, const Complex& b) {  // бинарный минус
  35.     return Complex( a.Re() - b.Re(), a.Im() - b.Im() );
  36. }
  37.  
  38. Complex operator * (const Complex& a, const Complex& b) {  // умножение
  39.     return Complex(
  40.         a.Re() * b.Re() - a.Im() * b.Im(),
  41.         a.Re() * b.Im() + a.Im() * b.Re());
  42. }
  43.  
  44. Complex operator / (Complex &c1, Complex &c2) {
  45.     return Complex(
  46.         (c1.Re() * c2.Re() + c1.Im() * c2.Im()) / (c2.Re() * c2.Re() + c2.Im() * c2.Im()),
  47.         (c2.Re() * c1.Im() - c1.Re() * c2.Im()) / (c2.Re() * c2.Re() + c2.Im() * c2.Im()));
  48. }
  49.  
  50. bool operator== (const Complex &c1, const Complex &c2) {
  51.     return c1.Re() == c2.Re() && c1.Im() == c2.Im();
  52. }
  53.  
  54. bool operator!= (const Complex &c1, const Complex &c2) {
  55.     return c1.Re() != c2.Re() || c1.Im() != c2.Im();
  56. }
  57. int main() {
  58.     Complex c1(1, 3);
  59.     Complex c2(2, 4);
  60.     cout << c1.Re() << " " << c1.Im() << endl;
  61.     cout << c2.Re() << " " << c2.Im() << endl;
  62.     if (c1 == c2) {
  63.         cout << 1;
  64.     }
  65.     else {
  66.         cout << "error" << endl;
  67.     }
  68.     c1 = c1 + c2;
  69.     cout << c1.Re() << " " << c1.Im() << endl;
  70.     cout << c2.Re() << " " << c2.Im() << endl;
  71.     c1 = c1 - c2;
  72.     c2 = c1 - c2;
  73.     cout << c1.Re() << " " << c1.Im() << endl;
  74.     cout << c2.Re() << " " << c2.Im() << endl;
  75.     cout << abs(c1) << " " << abs(c2) << endl;
  76.     system("pause");
  77. }
Advertisement
Add Comment
Please, Sign In to add comment