#include
<
complex
>
using namespace std;
complex < double > avg_arr( const complex < double > a[], int size)
{
complex < double > tmp,t1;
double realSum = 0.0 ,imagSum = 0.0 ;
int i;
for (i = 0 ;i < size;i ++ )
{
realSum += a[i].real(); // real(a[i]);
imagSum += a[i].imag(); // imag(a[i]);
}
double avgReal = realSum / size;
double avgImage = imagSum / size;
tmp = complex < double > (avgReal, avgImage);
return tmp;
}
using namespace std;
complex < double > avg_arr( const complex < double > a[], int size)
{
complex < double > tmp,t1;
double realSum = 0.0 ,imagSum = 0.0 ;
int i;
for (i = 0 ;i < size;i ++ )
{
realSum += a[i].real(); // real(a[i]);
imagSum += a[i].imag(); // imag(a[i]);
}
double avgReal = realSum / size;
double avgImage = imagSum / size;
tmp = complex < double > (avgReal, avgImage);
return tmp;
}
标准库里的complex有个地方我真是想不通,为什么不能像javaBean那样提供getter/setter方法来设置/获取实部和虚部的值呢,比如想把一个complex的实部更改为3.0,就只有通过下面两种方法来做:
(1) std::complex<double> c1(3.0, c1.imag())
(2) c1+= 3.0 –c1real ();
这两种方法我认为都有局限性:
第一种 : 我如果要在构造以后的适当时机改变 , 那么就不适用 了。第二种 : 这种方法挺好 , 只是效率上还是不如直接设定的方式,因为这里包括了一个普通函数 , 两个运算符 (- 和 +=), 并且引入了一个临时对象。库的设计者到底是怎么考虑的呢?难道他们的想法 就是增加复数类型 —— 就象浮点数是一个类型一样(我们同样无法只修改浮点数的尾数或是阶码) , 这样就符合 Type-extensibility 的思想?