Advertisement
Ansaid

Laba_5

Apr 10th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class Complex
  7. {
  8. private:
  9.     float x;
  10.     float y;
  11. public:
  12.     Complex()
  13.     {
  14.         x = 0;
  15.         y = 0;
  16.     }
  17.  
  18.     Complex &operator=(const Complex &obj)
  19.     {
  20.         x = obj.x;
  21.         y = obj.y;
  22.         return *this;
  23.     }
  24.  
  25.     Complex &operator++(int Val)
  26.     {
  27.         Complex temp(*this);
  28.         x++;
  29.         y++;
  30.         return temp;
  31.     }
  32.  
  33.     Complex &operator--(int Val)
  34.     {
  35.         Complex temp(*this);
  36.         x--;
  37.         y--;
  38.         return temp;
  39.     }
  40.  
  41.     bool operator==(const Complex & obj)
  42.     {
  43.         return (x == obj.x && y == obj.y);
  44.     }
  45.  
  46.     bool operator!=(const Complex & obj)
  47.     {
  48.         return !(x == obj.x && y == obj.y);
  49.     }
  50.  
  51.     friend ostream &operator<<(ostream &stream, Complex &obj);
  52.     friend istream &operator>>(istream &stream, Complex &obj);
  53. };
  54.  
  55. class Vector
  56. {
  57. private:
  58.     float x;
  59.     float y;
  60.     float z;
  61. public:
  62.     Vector()
  63.     {
  64.         x = 0;
  65.         y = 0;
  66.         z = 0;
  67.     }
  68.  
  69.     Vector &operator=(const Vector &obj)
  70.     {
  71.         x = obj.x;
  72.         y = obj.y;
  73.         z = obj.z;
  74.         return *this;
  75.     }
  76.  
  77.     bool operator==(const Vector & obj)
  78.     {
  79.         return (sqrt(x*x + y * y + z * z) == sqrt(obj.x*obj.x + obj.y * obj.y + obj.z * obj.z));
  80.     }
  81.  
  82.     bool operator!=(const Vector & obj)
  83.     {
  84.         return !(sqrt(x*x + y * y + z * z) == sqrt(obj.x*obj.x + obj.y * obj.y + obj.z * obj.z));
  85.     }
  86.  
  87.     friend ostream &operator<<(ostream &stream, Vector &obj);
  88.     friend istream &operator>>(istream &stream, Vector &obj);
  89. };
  90.  
  91. ostream &operator<<(ostream &stream, Complex &obj)
  92. {
  93.     cout << "Complex: ";
  94.     if (obj.y >= 0)
  95.         stream << obj.x << "+" << obj.y << "*i" << endl;
  96.     else
  97.         stream << obj.x << obj.y << "*i" << endl;
  98.  
  99.     return stream;
  100. }
  101.  
  102. istream &operator>>(istream &stream, Complex &obj)
  103. {
  104.     cout << "Введите Complex:\n";
  105.     cout << "Введите X: ";
  106.     stream >> obj.x;
  107.     cout << "Введите Y: ";
  108.     stream >> obj.y;
  109.     return stream;
  110. }
  111.  
  112. ostream &operator<<(ostream &stream, Vector &obj)
  113. {
  114.     cout << "Vector = (";
  115.     stream << obj.x << ", ";
  116.     stream << obj.y << ", ";
  117.     stream << obj.z << ")\n";
  118.     return stream;
  119. }
  120.  
  121. istream &operator>>(istream &stream, Vector &obj)
  122. {
  123.     cout << "Введите Vector:\n";
  124.     cout << "Введите X: ";
  125.     stream >> obj.x;
  126.     cout << "Введите Y: ";
  127.     stream >> obj.y;
  128.     cout << "Введите Z: ";
  129.     stream >> obj.z;
  130.     return stream;
  131. }
  132.  
  133. int main()
  134. {
  135.     setlocale(LC_ALL, "Russian");
  136.     //Part 1
  137.     Complex num1, num2;
  138.     cout << "Созданные комплекные числа:\n";
  139.     cout << num1 << num2 << endl;
  140.  
  141.     cout << "Введите значения этим числам:\n"
  142.         << "Первое число:\n";
  143.     cin >> num1;
  144.     cout << "Второе число:\n";
  145.     cin >> num2;
  146.  
  147.     cout << "\nВведенные числа:\n";
  148.     cout << "Первое число:\n"<< num1 << "Второе число:\n" << num2 << endl;
  149.  
  150.     num1++;
  151.     num2--;
  152.     cout << "1ое число после оператора ++:\n" << num1 << "2ое число после оператора --:\n" << num2 << endl;
  153.  
  154.     if (num1 == num2)
  155.         cout << "Комплексные числа равны" << endl;
  156.     if (num1 != num2)
  157.         cout << "Комплексные числа не равны" << endl;
  158.     //Part 2
  159.     Vector vec1, vec2;
  160.  
  161.     cout << "\n\nСозданные вектора:\n";
  162.     cout << vec1 << vec2 << endl;
  163.  
  164.     cout << "Введите значения этим векторам:\n"
  165.         << "Первый вектор:\n";
  166.     cin >> vec1;
  167.     cout << "Второй вектор:\n";
  168.     cin >> vec2;
  169.  
  170.     cout << "\nВведенные вектора:\n";
  171.     cout << "Первой вектор:\n" << vec1 << "Второй вектор:\n" << vec2 << endl;
  172.  
  173.     if (vec1 == vec2)
  174.         cout << "Вектора равны" << endl;
  175.     if (vec1 != vec2)
  176.         cout << "Вектора не равны" << endl;
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement