// // This is example code from Chapter 24.9 "Complex numbers" of // "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup // #include #include #include using namespace std; //------------------------------------------------------------------------------ namespace my { template class complex { // a complex is a pair of scalar values, basically a coordinate pair Scalar re, im; public: complex(const Scalar & r, const Scalar & i) :re(r), im(i) { } complex(const Scalar & r) :re(r),im(Scalar ()) { } complex() :re(Scalar ()), im(Scalar ()) { } Scalar real() { return re; } // real part Scalar imag() { return im; } // imaginary part // operators: = += -= *= /= }; } //------------------------------------------------------------------------------ typedef complex dcmplx; // sometimes complex gets verbose //------------------------------------------------------------------------------ void f(dcmplx z, vector< complex >& vc) { dcmplx z2 = pow(z,2); dcmplx z3 = z2*9.0+vc[3]; dcmplx sum = accumulate(vc.begin(), vc.end(), dcmplx()); // ... //if (z2 v(d,d+10); f(d[0], v); } //------------------------------------------------------------------------------