C++ 标准库中的复数类

参考:
http://www.cplusplus.com/reference/complex/complex/

#include
#include

using namespace :: std; 

int main(){

    std::complex<double> cnum1;
    std::complex<double> cnum2;
    complex<float> cnum3,cnum4,cnum5,cnum6,cnum7,cnum8,cnum9;
    
    cnum1= 5.;  // cnum1=5
    cnum2=complex<double>(3.,4);  // cnum2=3+4i

    cnum3=norm(cnum2);
    cnum4=cnum1+cnum2;
    cnum5=abs(cnum2);
    cnum6=sin(cnum2);
    cnum7=real(cnum2);
    cnum8=imag(cnum2);
    cout << "cnum1 = " << cnum1 << endl;
    cout << "cnum2 = " << cnum2 << endl;
    cout << "norm of cnum2 = " << cnum3 << endl;
    cout << "cnum1 + cnum2 = " << cnum4 << endl;
    cout << "abs of cnum2 = " << cnum5 << endl;
    cout << "sin cnum2 = " << cnum6 << endl;
    cout << "real cnum2 = " << cnum7 << endl;
    cout << "imag cnum2 = " << cnum8 << endl;

    return 0;
}

输出结果:

norm of cnum2 = (25,0)
cnum1+cnum2 = (8,4)
abs of cnum2 = (5,0)
bogon:cpp wjf$ c++ complex.cpp 
bogon:cpp wjf$ ./a.out
cnum1 = (5,0)
cnum2 = (3,4)
norm of cnum2 = (25,0)
cnum1 + cnum2 = (8,4)
abs of cnum2 = (5,0)
sin cnum2 = (3.85374,-27.0168)
real cnum2 = (3,0)
imag cnum2 = (4,0)

你可能感兴趣的:(C)