Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

3 семестр / sr26

.cpp
Скачиваний:
0
Добавлен:
16.11.2025
Размер:
6.47 Кб
Скачать
#include <iostream>
#include <math.h>
#include <tgmath.h>
// перегрузка операций: инверсия, сопряжение, присваивание
// перегружены сложение, вычитание, умножение и деление (как с компл., так и с действ.)
// операции присваивания с сложением и пр. (+=, *=...) как с компл., так и с действ. числами
// возведение в степень, наличие мнимой единицы, ввод/вывод компл. чисел через cin/cout
using namespace std;
class Complex {
  double Re, Im;
  public:
      Complex(double ReVal, double ImVal){ // конструктор для создания объектов
          Re=ReVal;
          Im=ImVal;
      }
      Complex(){ // конструктор по умолчанию
          Re=0.0;
          Im=0.0;
      }
      Complex(double ReVal){ // конструктор по умолчанию
          Re=ReVal;
          Im=0.0;
      }
      double cRe() { return(Re); }
      double cIm() { return(Im); }
      double cMod() { return(sqrt(Re*Re+Im*Im)); }
      double cArg() { return(atan2(Im, Re)); }
      Complex operator-(){ // инверсия (обратное число)
          Complex a(-Re, -Im);
          return(a);
      }
      Complex operator~(){ // сопряжение
          Complex a(Re, -Im);
          return(a);
      }
      const Complex& operator=(const Complex& c) { // присваивание (компл.)
          Re = c.Re;
          Im = c.Im;
          return *this;
      }
      const Complex& operator=(const double& c) { // присваивание (вещ.)
          Re = c;
          Im = 0.0;
          return *this;
      }
      const Complex& operator+=(const Complex& c) { // плюс-присваивание (компл.)
          Re += c.Re;
          Im += c.Im;
          return *this;
       }
       const Complex& operator+=(const double& c) { // плюс-присваивание (вещ.)
          Re += c;
          return *this;
       }
       const Complex& operator-=(const Complex& c) { // минус-присваивание (компл.)
          Re -= c.Re;
          Im -= c.Im;
          return *this;
       }
       const Complex& operator-=(const double& c) { // минус-присваивание (вещ.)
          Re -= c;
          return *this;
       }
       const Complex& operator*=(const Complex& c) { // умножить-присваивание (компл.)
          Re = Re*c.Re-Im*c.Im;
          Im = Im*c.Re + Re*c.Im;
          return *this;
       }
       const Complex& operator*=(const double& c) { // умножить-присваивание (вещ.)
          Re*=c;
          Im*=c;
          return *this;
        }
        const Complex& operator/=(const Complex& c) { // делить-присваивание (компл.)
            if((c.Re==0)&&(c.Im==0)){ // проверка на деление на ноль
                cout << "\Error!\n";
                return *this;
            }
            Re=(Re*c.Re + Im*c.Im)/(c.Re*c.Re+c.Im*c.Im);
            Im=(Im*c.Re - c.Im*Re)/(c.Re*c.Re+c.Im*c.Im);
            return *this;
        }
        const Complex& operator/=(const double& c) { // делить-присваивание (вещ.)
            if(c==0){ // проверка на деление на ноль
                cout << "\Error!\n";
                return *this;
            }
            Re/=c;
            Im/=c;
            return *this;
        }
        friend ostream& operator << (ostream& stream, const Complex& object){ // перегрузка для вывода
          if(object.Im<0) return stream << object.Re << object.Im << "i";
          else if (object.Im>0) return stream << object.Re << "+" << object.Im << "i";
          else return stream << object.Re;
        }
      friend istream& operator >> (istream& stream, Complex& object){ // перегрузка для ввода
          cin >> object.Re;
          cin >> object.Im;
      }
      void cInput1(){ // ввод декартовый
              cin >> Re;
              cin >> Im;
      }
};
Complex operator+(Complex d, Complex c){ // сложение
          Complex a(d.cRe()+c.cRe(), d.cIm()+c.cIm());
          return(a);
}
Complex operator-(Complex d, Complex c){ // разность
    Complex a(d.cRe()-c.cRe(), d.cIm()-c.cIm());
    return(a);
}
Complex operator*(Complex d, Complex c){ // произведение
    Complex a(d.cRe()*c.cRe()-d.cIm()*c.cIm(), d.cIm()*c.cRe() + d.cRe()*c.cIm());
    return(a);
}
Complex operator/(Complex d, Complex c){ // деление
    if((c.cRe()==0)&&(c.cIm()==0)){ // проверка на деление на ноль
        cout << "\Error!\n";
        Complex a(0.0, 0.0);
        return a;
    }
    Complex a((d.cRe()*c.cRe() + d.cIm()*c.cIm())/(c.cMod()*c.cMod()), (d.cIm()*c.cRe() - c.cIm()*d.cRe())/(c.cMod()*c.cMod()));
    return(a);
}
Complex operator^(Complex c, int n){ // возвести в целую степень
    double newArg=c.cArg()*n;
    double newMod=pow(c.cMod(), n);
    Complex a(newMod*cos(newArg), newMod*sin(newArg));
    return(a);
}
const Complex i(0, 1);
int main() {
    setlocale(LC_ALL, "Russian");
    Complex c, d, e;
    cout.precision(3);
    cin >> c;
    cin >> d;
    cout << "Основные операции: " << endl;
    cout << "c = " << c << endl << "d = " << d << endl;
    cout << "c + d = " << c+d << endl;
    cout << "c - d = " << c-d << endl;
    cout << "c * d = " << c*d << endl;
    cout << "c / d = " << c/d << endl;
    cout << "Основные операции с вещественными числами: " << endl;
    cout << "1 + c = " << 1.0+c << endl;
    cout << "3 - c = " << 3.0-c << endl;
    cout << "9 * c = " << 9.0*c << endl;
    cout << "12 / c = " << 12.0/c << endl;
    cout << "Обратное и сопряжённое: " << endl;
    cout << "-c = " << -c << endl;
    cout << "~c = " << ~c << endl;
    cout << "Присваивание с комплексными и действительными: " << endl;
    e=d;
    cout << "e = d --> e = " << e << endl;
    e=1.0;
    cout << "e = 1.0 --> e = " << e << endl;
    cout << "Присваивание с суммой, умножением и пр. с комплексными: " << endl;
    cout << "e+2i = " << e+2*i << endl;
    e+=Complex(2, 4);
    cout << "e += 2+2i --> e = " << e << endl;
    e-=Complex(1, 7);
    cout << "e -= 1+7i --> e = " << e << endl;
    e*=Complex(7, 3);
    cout << "e *= 7+3i --> e = " << e << endl;
    e/=Complex(2, 2);
    cout << "e /= 2+2i --> e = " << e << endl;
    cout << "Присваивание с суммой, умножением и пр. с действительными: " << endl;
    e+=2;
    cout << "e += 2 --> e = " << e << endl;
    e-=9;
    cout << "e -= 9 --> e = " << e << endl;
    e*=4;
    cout << "e *= 4 --> e = " << e << endl;
    e/=20;
    cout << "e /= 20 --> e = " << e << endl;
    cout << "Возведение в степень: " << endl;
    cout << "e^3 = " << (e^3) << endl;
}
Соседние файлы в папке 3 семестр