VC下使用复数示例(建议使用“用法一”) 用法一 #include#include using namespace std; typedef complex Complex; int main(int argc, char* argv[]) { Complex a,b,c; a = Complex::complex(1.,.2); //赋初值 printf("%f,%fn",real(a),imag(a)); //real()取实部,imag()取虚部 b = Complex::complex(1.,.2); //赋初值 c = Complex::complex(1.,.2); //赋初值 a = b + c; printf("%f,%fn",real(a),imag(a)); double k=2.0; //数乘的时候数的类型一定要和复数中
实部和虚部的类型相同,此处为double a = k * a; //所有需要和复数运算的实数都要定义为和实部虚部类型相同 printf("%f,%fn",real(a),imag(a)); return 0; } ****************************************************************** 用法二 #include#include int main(int argc, char* argv[]) { std::complex x,y,z; x = std::complex (1.,.02); printf("x = %f,j%fn",std::real(x),std::imag(x)); y = std::complex (1.,.02); z = std::complex (1.,.02); x = y * z; printf("x = %f,j%fn",std::real(x),std::imag(x)); return 0; } ****************************************************************** 用法三 #include int main(int argc, char* argv[]) { _complex c_val1,c_val2,c_val3; c_val1.x = 1.; c_val1.y = .2; c_val2.x = 1.; c_val2.y = .2; c_val3.x = c_val1.x + c_val2.x; c_val3.y = c_val1.y + c_val2.y; printf("sum = %f,j%fn",c_val3.x,c_val3.y); return 0; }
上一篇:源代码开放浏览器设计与实现
下一篇:基于DSP的过采样技术
- 热门资源推荐
- 热门放大器推荐