Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #define M_PI 3.1415926535
- using namespace std;
- class complex {
- public:
- double Re;
- double Im;
- friend complex operator+(const complex& left, const complex& right) {
- return complex(left.Re + right.Re, left.Im + right.Im);
- }
- friend const complex operator-(const complex& left, const complex& right) {
- return complex(left.Re - right.Re, left.Im - right.Im);
- }
- friend const complex operator*(const complex& left, const complex& right) {
- return complex(
- (right.Re * left.Re) - (right.Im * left.Im),
- (right.Im * left.Re) + (right.Re * left.Im)
- );
- }
- friend const complex operator/(const complex& left, const complex& right) {
- double mult = pow(right.Re, 2) + pow(right.Im, 2);
- return complex(
- ((right.Re * left.Re) + (right.Im * left.Im)) / mult,
- ((right.Re * left.Im) - (right.Im * left.Re)) / mult
- );
- }
- complex& operator+=(const complex& right) {
- Re += right.Re;
- Im += right.Im;
- return *this;
- }
- complex& operator-=(const complex& right) {
- Re -= right.Re;
- Im -= right.Im;
- return *this;
- }
- complex& operator*=(const complex& right) {
- Re = (right.Re * Re) - (right.Im * Im);
- Im = (right.Im * Re) + (right.Re * Im);
- return *this;
- }
- complex& operator/=(const complex& right) {
- double mult = (Re * Re) + (Im * Im);
- Re = ((right.Re * Re) + (right.Im * Im)) / mult;
- Im = ((right.Re * Im) - (right.Im * Re)) / mult;
- return *this;
- }
- const double arg();
- const double mod()
- {
- return sqrt((Re*Re) + (Im*Im));
- }
- complex(double _real = 0, double _noreal = 0):
- Re(_real),
- Im(_noreal) {}
- };
- const double complex::arg() {
- double result = 0;
- double temp = atan2(Im, Re);
- if (Re == 0)
- return Im > 0 ? M_PI / 2 : 3 * M_PI / 2;
- if (Re > 0)
- result = Im < 0 ? temp : (2 * M_PI) - abs(temp);
- else
- result = Im < 0 ? M_PI + abs(temp) :M_PI - abs(temp);
- while (result > 2 * M_PI)
- result -= 2 * M_PI;
- return result;
- }
- ostream& operator<< (ostream& out, const complex& num)
- {
- out << num.Re << " ";
- if (num.Im > 0) {
- out << '+';
- }
- else {
- out << '-';
- }
- out << " " << num.Im << 'i';
- return out;
- }
- int main()
- {
- complex test1(4, 2);
- complex test2(5, 1);
- cout << "First: " << test1 << " Second: " << test2 << endl;
- cout << "Binary + " << (test1 + test2) << endl;
- cout << "Binary - " << (test1 - test2) << endl;
- cout << "Binary * " << (test1 * test2) << endl;
- cout << "Binary / " << (test1 / test2) << endl;
- cout << "Binary first + with double 2: " << (test1 + 2.1) << endl;
- cout << "Binary first - with double 2: " << (test1 - 2.2) << endl;
- cout << "Binary first * with double 2: " << (test1 * 2.3) << endl;
- cout << "Binary first / with double 2: " << (test1 / 2.5) << endl;
- cout << "First mod = " << test1.mod() << endl;
- auto x = (test1 + test2) = 3;
- auto b = 2 + test1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement