Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <cmath>
- namespace numbers {
- class complex {
- public:
- double imag, real;
- complex() {
- real = 0;
- imag = 0;
- }
- complex(double r, double i = 0.0) {
- real = r;
- imag = i;
- }
- explicit complex(const std::string str) {
- sscanf(str.c_str(), "(%lf,%lf)", &real, &imag);
- }
- double re() const
- {
- return real;
- }
- double im() const
- {
- return imag;
- }
- double abs2() const
- {
- return real * real + imag * imag;
- }
- double abs() const
- {
- return sqrt(abs2());
- }
- std::string to_string() const
- {
- char buf[100];
- snprintf(buf, 100, "(%.10g,%.10g)", real, imag);
- return (std::string) buf;
- }
- complex operator+=(complex a, complex b);
- complex operator-=(complex a, complex b);
- complex operator*=(complex a, complex b);
- complex operator/=(complex a, complex b);
- complex operator+(complex a, complex b) ;
- complex operator-(complex a, complex b) ;
- complex operator*(complex a, complex b) ;
- complex operator/(complex a, complex b) ;
- complex operator~(complex a) ;
- complex operator-(complex a) ;
- };
- complex operator+=(complex a, complex b)
- {
- a.real += b.re();
- a.imag += b.im();
- numbers::complex tmp(a.real, a.imag);
- return tmp;
- }
- complex operator-=(complex a, complex b)
- {
- a.real -= b.re();
- a.imag -= b.im();
- numbers::complex tmp(a.real, a.imag);
- return tmp;
- }
- complex operator*=(complex a, complex b)
- {
- //(a+bi)(c+di) = (ac-bd)+(ad+bc)i
- double tmp_real = a.real * b.re() - a.imag * b.im();
- double tmp_imag = a.real * b.im() + a.imag * b.re();
- a.real = tmp_real;
- a.imag = tmp_imag;
- numbers::complex tmp(a.real, a.imag);
- return tmp;
- }
- complex operator/=(complex a, complex b)
- {
- /*
- a - real
- b - imag
- c - b.re()
- d - b.im()
- */
- //(a+bi)(c+di) = ((ac+bd) / (c^2 + d^2)) + ((bc - ad) / (c^2 + d^2))i
- double tmp_real = (a.real * b.re() + a.imag * b.im()) / (b.re() * b.re() + b.im() * b.im());
- double tmp_imag = (a.imag * b.re() - a.real * b.im()) / (b.re() * b.re() + b.im() * b.im());
- a.real = tmp_real;
- a.imag = tmp_imag;
- numbers::complex tmp(a.real, a.imag);
- return tmp;
- }
- complex operator+(complex a, complex b)
- {
- numbers::complex c(a.real, a.imag);
- return c+=b;
- }
- complex operator-(complex a, complex b)
- {
- numbers::complex c(a.real, a.imag);
- return c-=b;
- }
- complex operator*(complex a, complex b)
- {
- numbers::complex c(a.real, a.imag);
- return c*=b;
- }
- complex operator/(complex a, complex b)
- {
- numbers::complex c(a.real, a.imag);
- return c/=b;
- }
- complex operator~(complex a)
- {
- numbers::complex c(a.real, -a.imag);
- return c;
- }
- complex operator-(complex a)
- {
- numbers::complex c(-a.real, -a.imag);
- return c;
- }
- }
- // int main()
- // {
- // numbers::complex c1("(1.5656, 2.213)"), c2("(56, 3)");
- // std::cout << (c1 + c2).im() <<" "<<c1.im();
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement