Добавил:
okley
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:3 семестр / sr24
.cpp#include <iostream>
#include <math.h>
#include <tgmath.h>
using namespace std;
class Complex {
double Re, Im, Mod, Arg;
public:
Complex(double ReVal, double ImVal){ // конструктор для создания объектов
Re=ReVal;
Im=ImVal;
Mod=sqrt(ReVal*ReVal+ImVal*ImVal);
Arg=atan2(ImVal, ReVal);
}
double cRe(){ return(Re); }
double cIm(){ return(Im); }
double cMod(){ return(Mod); }
double cArg(){ return(Arg); }
Complex operator+(Complex c){ // сложение комплексных чисел
Complex a(Re+c.cRe(), Im+c.cIm());
return(a);
}
Complex operator-(Complex c){ // разность
Complex a(Re-c.cRe(), Im-c.cIm());
return(a);
}
Complex operator*(Complex c){ // произведение
Complex a(Re*c.cRe()-Im*c.cIm(), Im*c.cRe() + Re*c.cIm());
return(a);
}
Complex operator/(Complex c){ // деление
if((c.cRe()==0)&&(c.cIm()==0)){ // проверка на деление на ноль
cout << "\Error!\n";
Complex a(0.0, 0.0);
return a;
}
Complex a((Re*c.cRe() + Im*c.cIm())/(c.cMod()*c.cMod()), (Im*c.cRe() - c.cIm()*Re)/(c.cMod()*c.cMod()));
return(a);
}
Complex Sopr(Complex c){ // сопряженное
Complex a(c.cRe(), -c.cIm());
return a;
}
Complex Obr(Complex c){ // обратить знаки
Complex a(-c.cRe(), -c.cIm());
return a;
}
void cOutput(){ // вывод
cout << Re;
if (Im<0) cout << Im << "i";
else cout << "+" << Im << "i";
cout << "\t Module = " << Mod << " Angle = " << Arg << "\n";
}
void cInput1(){ // ввод декартовый
cout << "Input real part:\n";
cin >> Re;
cout << "Input imaginary part:\n";
cin >> Im;
Mod=sqrt(Re*Re+Im*Im);
Arg=atan2(Im, Re);
}
void cInput2(){ // ввод полярный
cout << "Input module:\n";
cin >> Mod;
cout << "Input angle:\n";
cin >> Arg;
Re = Mod*cos(Arg);
Im = Mod*sin(Arg);
}
};
int main() {
Complex c(1.2, 1.5), d(25.3, -12.1);
cout.precision(3);
c.cInput1();
d.cInput1();
c.cOutput();
d.cOutput();
cout << "c + d = ";
(c+d).cOutput();
cout << "c - d = ";
(c-d).cOutput();
cout << "c * d = ";
(c*d).cOutput();
cout << "c / d = ";
(c/d).cOutput();
}
