Advertisement
FaDaQ

Комплексные числа

Oct 14th, 2022
1,301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <format>
  3.  
  4. using std::cout; using std::cin; using std::endl; using std::format; using std::string;
  5.  
  6. class Complex {
  7. public:
  8.     Complex(double numReal, double numImaginary) {
  9.         real = numReal;
  10.         imaginary = numImaginary;
  11.     }
  12.  
  13.     double getReal() {
  14.         return real;
  15.     }
  16.     double getImaginary() {
  17.         return imaginary;
  18.     }
  19.  
  20.     void print() {
  21.         if (imaginary >= 0) {
  22.             cout << format("{0}+{1}i", real, imaginary) << endl;
  23.         }
  24.         else {
  25.             cout << format("{0}{1}i", real, imaginary) << endl;
  26.            
  27.         }
  28.     }
  29.  
  30.     string strForm() {
  31.         if (imaginary >= 0) {
  32.             return format("{0}+{1}i", real, imaginary);
  33.         }
  34.         else {
  35.             return format("{0}{1}i", real, imaginary);
  36.         }
  37.     }
  38.  
  39. private:
  40.     double real;
  41.     double imaginary;
  42.    
  43. };
  44.  
  45. //функция для возведения в любую степень
  46. double degr(double num, double N) {
  47.     int degrNum = num;
  48.     for (int i = 1; i < N; i++) {
  49.         degrNum *= num;
  50.     }
  51.     return degrNum;
  52. }
  53.  
  54. //Сложение комплексных чисел
  55. Complex complexSum(Complex num1, Complex num2) {
  56.     double real = num1.getReal() + num2.getReal();
  57.     double imaiginary = num1.getImaginary() + num2.getImaginary();
  58.     Complex result(real, imaiginary);
  59.     return result;
  60. }
  61.  
  62. //Вычитание комплексных чисел
  63. Complex complexSub(Complex num1, Complex num2) {
  64.     double real = num1.getReal() - num2.getReal();
  65.     double imaginary = num1.getImaginary() - num2.getImaginary();
  66.     Complex result(real, imaginary);
  67.     return result;
  68. }
  69.  
  70. //Умножение комплексных чисел
  71. Complex complexMult(Complex num1, Complex num2) {
  72.     double real = (num1.getReal() * num2.getReal()) - (num1.getImaginary() * num2.getImaginary());
  73.     double imaginary = (num1.getImaginary() * num2.getReal()) + (num1.getReal() * num2.getImaginary());
  74.     Complex result(real, imaginary);
  75.     return result;
  76. }
  77.  
  78. //Деление комплексных чисел
  79. Complex complexDiv(Complex num1, Complex num2) {
  80.     double real = ((num1.getReal() * num2.getReal()) + (num1.getImaginary() * num2.getImaginary())) /
  81.                   (degr(num2.getReal(), 2) + degr(num2.getImaginary(), 2));
  82.     double imaginary = (num1.getImaginary() * num2.getReal() - num1.getReal() * num2.getImaginary()) /
  83.                        (degr(num2.getReal(), 2) + degr(num2.getImaginary(), 2));
  84.  
  85.     Complex result(real, imaginary);
  86.     return result;
  87. }
  88.  
  89. int main()
  90. {
  91.     system("chcp 1251"); system("cls");
  92.  
  93.     cout << "Введите реальную и мнимую часть ПЕРВОГО числа через пробел(БЕЗ i): ";
  94.     double num1A, num1B; cin >> num1A >> num1B;
  95.    
  96.     cout << "Введите реальную и мнимую часть ВТОРОГО числа через пробел(БЕЗ i): ";
  97.     double num2A, num2B; cin >> num2A >> num2B;
  98.  
  99.     Complex num1(num1A, num1B);
  100.     Complex num2(num2A, num2B);
  101.  
  102.     Complex num3 = complexSum(num1, num2);
  103.     Complex num4 = complexSub(num1, num2);
  104.     Complex num5 = complexMult(num1, num2);
  105.     Complex num6 = complexDiv(num1, num2);
  106.    
  107.     cout << endl;
  108.  
  109.     cout << format("Результат сложения ({0}) + ({1}) \t= \t", num1.strForm(), num2.strForm()); num3.print();
  110.     cout << format("Результат вычитания ({0}) - ({1}) \t= \t", num1.strForm(), num2.strForm());  num4.print();
  111.     cout << format("Результат умножения ({0}) * ({1}) \t= \t", num1.strForm(), num2.strForm());  num5.print();
  112.     cout << format("Результат деления ({0}) * ({1}) \t= \t", num1.strForm(), num2.strForm());  num6.print();
  113.  
  114.    
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement