first git commit

This commit is contained in:
GandC 2024-05-02 23:19:51 +03:00
commit bc84588bd1
Signed by: gandc
GPG Key ID: 9F77B03D43C42CB4
3 changed files with 49 additions and 0 deletions

49
1.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <iostream>
class Complex {
private:
double real;
double imag;
public:
// Конструкторы
Complex(double r = 0.0, double im = 0.0) : real(r), imag(im) {}
// Метод для получения комплексного сопряжения (унарный плюс)
Complex operator+() const {
return Complex(real, -imag);
}
// Метод для умножения на действительное число
Complex operator*(double scalar) const {
return Complex(real * scalar, imag * scalar);
}
// Метод для умножения на комплексное число
Complex operator*(const Complex& other) const {
return Complex(real * other.real - imag * other.imag,
real * other.imag + imag * other.real);
}
// Метод для вывода комплексного числа
void display() const {
std::cout << "(" << real << " + " << imag << "i)" << std::endl;
}
// Дружественная функция для умножения действительного числа на комплексное число
friend Complex operator*(double scalar, const Complex& c);
};
// Реализация дружественной функции для умножения действительного числа на комплексное число
Complex operator*(double scalar, const Complex& c) {
return Complex(c.real * scalar, c.imag * scalar);
}
int main() {
// Пример использования операций
Complex a(2.0, 3.0);
Complex b = 2.5 * a; // Умножение действительного числа на комплексное число
b.display(); // Ожидаем (5 + 7.5i)
return 0;
}

BIN
a.out Executable file

Binary file not shown.

Binary file not shown.