#include <complex>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

// Compilation:
// $ g++ -I. -o main stl00.cpp


//
// Brief:  Looking at STL complex number class
//

#define SHOW(x) #x << '=' << (x)


void test01()
{
  complex<double> x(1.0,2.0);
  cout << SHOW(x) << endl;

  int k=35;
  cout << SHOW(k) << k << endl;
  
  complex<double> y(3.0,-1.0);
  cout << SHOW(y) << endl;
  cout << "multiply x and y" << endl;
  cout << SHOW(x*y) << endl;

  cout << "Operations on complex numbers" << endl;
  cout << SHOW(arg(x))   << endl;
  cout << SHOW(conj(x))  << endl;
  cout << SHOW(x.real()) << endl;
  cout << SHOW(x.imag()) << endl;
  cout << SHOW(abs(x))   << endl;
  cout << SHOW(norm(x))  << endl;
  cout << SHOW(sqrt(x))  << endl;


  cout << endl;
  cout << "Converting from polar to complex number" << endl;
  cout << SHOW(complex<float>(polar(4.2,0.75))) << endl;
  cout << endl;
   
  cout << SHOW(pow(x,1.5)) << endl;
  cout << SHOW(log(x))     << endl;
  cout << SHOW(log10(x))   << endl;
  cout << SHOW(exp(x))     << endl;
  cout << SHOW(sinh(x))    << endl;
  cout << SHOW(cosh(x))    << endl;
  cout << SHOW(cos(x))     << endl;
  cout << SHOW(sin(x))     << endl;

  cout << endl;
  cout << "Displaying complex and polar representations" << endl;
  complex<double> z(0.0,1.0);
  cout << SHOW(z) << "  " << "(" << abs(z) << ",";
  cout << arg(z) << ")" << endl;
}


void test02()
{
  complex< long double > x(1.0,2.0);

  cout << endl << x << endl;
  complex< long double > y(1.0/3.0,2.0);
  cout << setprecision(30) << y << endl;

  y += 3;
  cout << y << endl;
  y *= 5.2;
  cout << y << endl;

  {
    ofstream f1("temp.txt");
    f1 << x << endl;
    f1 << y << endl;
  }

  {
    ifstream f2("temp.txt");
    complex<long double> a,b;
    f2 >> a;
    f2 >> b;
    cout << SHOW(a) << endl;
    cout << SHOW(b) << endl;
  }

  
}


int main()
{
  test02();

  return 0;
}

