C语言实现数字信号处理算法附录A1 BC下复数类型的实现1、利用BC提供的复数支持//BC中使用复数类型使用示例(ComplexUse.Cpp文件)#include <iostream.h>#include <complex.h>int main(void){double x = 3.1, y = 4.2;complex z = complex(x,y);cout << "z = "<< z << "
";cout << " and imaginary real part = " << imag(z) << "
";cout << "z has complex conjugate = " << conj(z) << "
";return 0;}2、定义复数类,填写相应成员函数//C中的复数类型调用时可能不是非常好用,可自己定义复数类(ComplexUse.Cpp文件)class Complex{public:Complex(){}Complex( float re, float im );float r(){return real;};float i(){return imag;};float mod(){return sqrt(real*real+imag*imag);};Complex operator+( Complex &other );Complex operator-( Complex &other );Complex operator*( Complex &other );Complex operator/( Complex &other );private:float real, imag;};// Operator overloaded using a member functionComplex::Complex(float re,float im){real=re;imag=im;};Complex Complex::operator+( Complex &other ){return Complex( real + other.real, imag + other.imag );};Complex Complex::operator-( Complex &other ){return Complex( real - other.real, imag - other.imag );};Complex Complex::operator*( Complex &other ){float x,y;x=real*other.real-imag*other.imag;y=real*other.imag+imag*other.real;return Complex( x,y );};Complex Complex::operator/( Complex &other ){float x,y,l;l=other.real*other.real+other.imag*other.imag;x=real*other.real+imag*other.imag;y=other.real*imag-real*other.imag;x=x/l;y=y/l;return Complex(x,y);};
猜您喜欢
推荐内容
开源项目推荐 更多
热门活动
热门器件
用户搜过
随便看看
热门下载
热门文章
评论