Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.52 KB | None | 0 0
  1. // lab1_oop.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <math.h>
  7.  
  8.  
  9. class Complex {
  10. private:
  11.     double realPart;
  12.     double imaginaryPart;
  13.  
  14. public:
  15.     Complex(double realPart, double imaginaryPart)
  16.     {
  17.         this->realPart = realPart;
  18.         this->imaginaryPart = imaginaryPart;
  19.     }
  20.  
  21.     void display()
  22.     {
  23.         if (realPart != 0)
  24.         {
  25.             if (imaginaryPart > 0) {
  26.                 std::cout << realPart << " + " << imaginaryPart << "i";
  27.                 return;
  28.             }
  29.  
  30.             if (imaginaryPart < 0) {
  31.                 std::cout << realPart << " - " << abs(imaginaryPart) << "i";
  32.                 return;
  33.             }
  34.  
  35.             if (imaginaryPart == 0)
  36.             {
  37.                 std::cout << realPart;
  38.                 return;
  39.             }
  40.  
  41.         }
  42.  
  43.         else {
  44.             if (imaginaryPart > 0) {
  45.                 std::cout << imaginaryPart << "i";
  46.                 return;
  47.             }
  48.  
  49.             if (imaginaryPart < 0) {
  50.                 std::cout << "- " << abs(imaginaryPart) << "i";
  51.                 return;
  52.             }
  53.  
  54.             if (imaginaryPart == 0)
  55.             {
  56.                 std::cout << 0;
  57.                 return;
  58.             }
  59.         }
  60.  
  61.  
  62.     }
  63.  
  64.     Complex operator + (Complex c) const
  65.     {
  66.         return Complex(this->realPart + c.realPart, this->imaginaryPart + c.imaginaryPart);
  67.     }
  68.  
  69.     Complex operator - (Complex c)
  70.     {
  71.         return Complex(this->realPart - c.realPart, this->imaginaryPart - c.imaginaryPart);
  72.     }
  73.  
  74.     Complex operator * (Complex c)
  75.     {
  76.         return Complex(this->realPart * c.realPart - this->imaginaryPart * c.imaginaryPart, this->realPart*c.imaginaryPart + c.realPart * this->imaginaryPart);
  77.     }
  78.  
  79. };
  80.  
  81. void sum() {
  82.     double firstReal;
  83.     double secondReal;
  84.     double firstImag;
  85.     double secondImag;
  86.     std::cout << "Введите вещественную часть первого числа: ";
  87.     std::cin >> firstReal;
  88.     std::cout << "Введите мнимую часть первого числа: ";
  89.     std::cin >> firstImag;
  90.     std::cout << "Введите вещественную часть второго числа: ";
  91.     std::cin >> secondReal;
  92.     std::cout << "Введите мнимую часть второго числа: ";
  93.     std::cin >> secondImag;
  94.  
  95.     Complex c1(firstReal, firstImag);
  96.     Complex c2(secondReal, secondImag);
  97.  
  98.     Complex c3 = c1 + c2;
  99.  
  100.     std::cout << "Результат операции: ";
  101.     c3.display();
  102.     std::cout << std::endl;
  103. }
  104.  
  105. void subtract() {
  106.     double firstReal;
  107.     double secondReal;
  108.     double firstImag;
  109.     double secondImag;
  110.     std::cout << "Введите вещественную часть первого числа: ";
  111.     std::cin >> firstReal;
  112.     std::cout << "Введите мнимую часть первого числа: ";
  113.     std::cin >> firstImag;
  114.     std::cout << "Введите вещественную часть второго числа: ";
  115.     std::cin >> secondReal;
  116.     std::cout << "Введите мнимую часть второго числа: ";
  117.     std::cin >> secondImag;
  118.  
  119.     Complex c1(firstReal, firstImag);
  120.     Complex c2(secondReal, secondImag);
  121.  
  122.     Complex c3 = c1 - c2;
  123.  
  124.     std::cout << "Результат операции: ";
  125.     c3.display();
  126.     std::cout << std::endl;
  127. }
  128.  
  129. void multiplication() {
  130.     double firstReal;
  131.     double secondReal;
  132.     double firstImag;
  133.     double secondImag;
  134.     std::cout << "Введите вещественную часть первого числа: ";
  135.     std::cin >> firstReal;
  136.     std::cout << "Введите мнимую часть первого числа: ";
  137.     std::cin >> firstImag;
  138.     std::cout << "Введите вещественную часть второго числа: ";
  139.     std::cin >> secondReal;
  140.     std::cout << "Введите мнимую часть второго числа: ";
  141.     std::cin >> secondImag;
  142.  
  143.     Complex c1(firstReal, firstImag);
  144.     Complex c2(secondReal, secondImag);
  145.  
  146.     Complex c3 = c1 * c2;
  147.  
  148.     std::cout << "Результат операции: ";
  149.     c3.display();
  150.     std::cout << std::endl;
  151. }
  152.  
  153. int main()
  154. {
  155.     setlocale(LC_ALL, "Russian");
  156.     int flag = 0;
  157.     while (flag != 4)
  158.     {
  159.         std::cout << "1. Проверка функции сложения комплексных чисел" << std::endl;
  160.         std::cout << "2. Проверка функции вычитания комплексных чисел" << std::endl;
  161.         std::cout << "3. Проверка функции умножения комплексных чисел" << std::endl;
  162.         std::cout << "4. Выход" << std::endl;
  163.  
  164.  
  165.         std::cin >> flag;
  166.         system("cls");
  167.         switch (flag)
  168.         {
  169.         case 1: sum(); system("pause"); break;
  170.         case 2: subtract(); system("pause"); break;
  171.         case 3: multiplication(); system("pause"); break;
  172.         case 4: break;
  173.         default:
  174.             break;
  175.         }
  176.         system("cls");
  177.     }
  178.     return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement