fork download
  1. #include "VectorCpx.h"
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <limits>
  8. using namespace std;
  9.  
  10. ostream& operator<<(ostream& ostr, const Complex& cp)
  11. {
  12. ostr << "R:" << cp.real() << ", I:" << cp.imag();
  13. return ostr;
  14. }
  15.  
  16. // ------------------------------------------------------
  17. // Member function definition of Complex
  18. Complex::Complex(double _real, double _imag): mReal(_real), mImag(_imag)
  19. {
  20.  
  21. }
  22.  
  23. const double& Complex::real() const
  24. {
  25. return mReal;
  26. }
  27.  
  28. const double& Complex::imag() const
  29. {
  30. return mImag;
  31. }
  32.  
  33. void Complex::real(double value)
  34. {
  35. mReal = value;
  36. }
  37.  
  38. void Complex::imag(double value)
  39. {
  40. mImag = value;
  41. }
  42.  
  43. Complex Complex::operator + (const Complex& cpx)const
  44. {
  45. Complex cpOut;
  46. cpOut.real(mReal + cpx.real());
  47. cpOut.imag(mImag + cpx.imag());
  48. return cpOut;
  49. }
  50.  
  51. Complex Complex::operator - (const Complex& cpx)const
  52. {
  53. Complex cpOut;
  54. cpOut.real(mReal - cpx.real());
  55. cpOut.imag(mImag - cpx.imag());
  56. return cpOut;
  57. }
  58.  
  59. Complex Complex::operator * (const Complex& cpx)const
  60. {
  61. Complex cpOut;
  62. cpOut.real( mReal * cpx.real() - mImag * cpx.imag() );
  63. cpOut.imag( mReal * cpx.imag() + mImag * cpx.real() );
  64. return cpOut;
  65. }
  66.  
  67. Complex Complex::operator / (const Complex& cpx)const
  68. {
  69. Complex cpOut;
  70. double rDiv = cpx.real() * cpx.real() + cpx.imag() * cpx.imag();
  71. if( rDiv < numeric_limits<double>::min())
  72. return cpOut;
  73. cpOut.real( (mReal * cpx.real() + mImag * cpx.imag()) / rDiv );
  74. cpOut.imag( (mImag * cpx.real() - mReal * cpx.imag()) / rDiv );
  75. return cpOut;
  76. }
  77.  
  78. // -----------------------------------------------
  79. // member functio of VectorCpx
  80. VectorCpx::VectorCpx(unsigned _n, const Complex& cpx):pCpx(NULL), mSize(0), mCapacity(0)
  81. {
  82. resize(_n, cpx);
  83. }
  84.  
  85. VectorCpx::VectorCpx(const VectorCpx& v):pCpx(NULL), mSize(v.mSize), mCapacity(v.mCapacity)
  86. {
  87. if(mCapacity > 0)
  88. {
  89. pCpx = new Complex[mCapacity];
  90. for(unsigned i=0; i<mSize; ++i)
  91. pCpx[i] = v.pCpx[i];
  92. }
  93. }
  94.  
  95. VectorCpx::~VectorCpx()
  96. {
  97. if(pCpx != NULL)
  98. delete [] pCpx;
  99. }
  100.  
  101. unsigned VectorCpx::size() const
  102. {
  103. return mSize;
  104. }
  105.  
  106. unsigned VectorCpx::capacity() const
  107. {
  108. return mCapacity;
  109. }
  110.  
  111. const Complex& VectorCpx::operator[](int i) const
  112. {
  113. if(i<0)
  114. return pCpx[0];
  115. else if(static_cast<unsigned>(i) >= mSize)
  116. return pCpx[mSize-1];
  117. else return pCpx[i];
  118. }
  119.  
  120. Complex& VectorCpx::operator[](int i)
  121. {
  122. if(i<0)
  123. return pCpx[0];
  124. else if(static_cast<unsigned>(i) >= mSize)
  125. return pCpx[mSize-1];
  126. else return pCpx[i];
  127. }
  128.  
  129. VectorCpx& VectorCpx::push_back(const Complex& cpx)
  130. {
  131. resize(mSize+1, cpx);
  132. return *this;
  133. }
  134.  
  135. VectorCpx& VectorCpx::resize(unsigned _n, const Complex& cpx)
  136. {
  137. if(_n > mCapacity)
  138. {
  139. if(mCapacity < 1)
  140. mCapacity = 1;
  141. while (mCapacity < _n)
  142. mCapacity <<= 1;
  143. Complex *pNew = new Complex[mCapacity];
  144. // Restore original data
  145. if(mSize > 0)
  146. {
  147. for(unsigned i=0; i<mSize; ++i) pNew[i] = pCpx[i];
  148. delete [] pCpx;
  149. }
  150. pCpx = pNew;
  151. }
  152.  
  153. for(unsigned i=mSize; i<_n; pCpx[i++] = cpx);
  154. mSize = _n;
  155. return *this;
  156. }
  157.  
  158. VectorCpx& VectorCpx::operator=(const VectorCpx& v)
  159. {
  160. if(this != &v)
  161. {
  162. this->VectorCpx::~VectorCpx();
  163. mSize = v.mSize;
  164. if(mSize>0)
  165. {
  166. mCapacity = 1;
  167. while (mCapacity < mSize)
  168. mCapacity <<= 1;
  169. pCpx = new Complex[mCapacity];
  170. for(unsigned i=0; i<mSize; ++i)
  171. pCpx[i] = v.pCpx[i];
  172. }
  173. }
  174. return *this;
  175. }
  176.  
  177. VectorCpx VectorCpx::operator*(const Complex& cpx)const
  178. {
  179. VectorCpx vOut = *this;
  180. for(int i=0; i<mSize; ++i)
  181. vOut.pCpx[i] = vOut.pCpx[i] * cpx;
  182. return vOut;
  183. }
  184.  
  185. Complex VectorCpx::operator*(const VectorCpx& v)
  186. {
  187. Complex cpxDot = 0;
  188. unsigned nLoop = mSize<v.mSize?mSize:v.mSize;
  189. for(int i=0; i<nLoop; ++i)
  190. {
  191. cpxDot = cpxDot + pCpx[i] * v.pCpx[i];
  192. }
  193. return cpxDot;
  194. }
  195.  
  196. VectorCpx& VectorCpx::operator>>(fstream& file) //= =
  197. {
  198. cout << "^_^" << endl;
  199. return *this;
  200. }
  201.  
  202. VectorCpx& VectorCpx::operator<<(fstream& file) //......-.-
  203. {
  204. cout << "XD" << endl;
  205. return *this;
  206. }
  207.  
  208. VectorCpx operator*(const Complex& cpx, const VectorCpx& v)
  209. {
  210. VectorCpx vOut = v;
  211. unsigned n = v.size();
  212. for(int i=0; i<n; ++i)
  213. vOut[i] = vOut[i] * cpx;
  214. return vOut;
  215. }
  216.  
  217. ostream& operator<<(ostream& ostr, const VectorCpx& v)
  218. {
  219. unsigned n = v.size();
  220. for(int i=0; i<n;)
  221. {
  222. ostr << v[i];
  223. ++i;
  224. if(i<n)
  225. ostr << "; ";
  226. }
  227. ostr << endl;
  228. return ostr;
  229. }
  230.  
  231. //-------------------------------------------------------------
  232. //member function of AdvVectorO
  233.  
  234. AdvVectorO::AdvVectorO(unsigned number, const Complex& input):VectorCpx(number, input) //constructor
  235. {
  236.  
  237. }
  238.  
  239. AdvVectorO::AdvVectorO(const VectorCpx& vec):VectorCpx(vec) //copy constructor
  240. {
  241.  
  242. }
  243.  
  244. AdvVectorO& AdvVectorO::operator>>(fstream& file)
  245. {
  246. for(int i = 0; i < mSize; i++)
  247. {
  248. file << (*this)[i].real() << " " << (*this)[i].imag();
  249. file << endl;
  250. }
  251.  
  252. return *this;
  253. }
  254.  
  255. //-------------------------------------------------------------
  256. //member function of AdvVectorI
  257.  
  258. AdvVectorI::AdvVectorI(unsigned number, const Complex& input):VectorCpx(number, input) //constructor
  259. {
  260.  
  261. }
  262.  
  263. AdvVectorI::AdvVectorI(const VectorCpx& vec):VectorCpx(vec) //copy constructor
  264. {
  265.  
  266. }
  267.  
  268. AdvVectorI& AdvVectorI::operator<<(fstream& file)
  269. {
  270. double regR = 0.0, regI = 0.0;
  271.  
  272. while(file >> regR >> regI) //如果寫while(!file.eof()){file >> regR >> regI;...}的話,會造成最後一個地方讀兩遍,因為C++的eof()是條件「已發生」才會回傳true
  273. push_back(Complex(regR,regI)); //每從文件讀一組數字進來,就push_back這組數字到原本的VectorCpx後面
  274.  
  275. return *this;
  276. }
  277.  
  278. //-------------------------------------------------------------
  279. //member function of AdvVectorIO
  280.  
  281. //因為這裡要求以binary的形式操作程式,因此不能像前面用Complex的指標做爽爽,得先轉成char型態指標,再用fstream的函式去做
  282.  
  283. AdvVectorIO::AdvVectorIO(unsigned number, const Complex& input):VectorCpx(number, input) //constructor
  284. {
  285.  
  286. }
  287.  
  288. AdvVectorIO::AdvVectorIO(const VectorCpx& vec):VectorCpx(vec) //copy constructor
  289. {
  290.  
  291. }
  292.  
  293. AdvVectorIO& AdvVectorIO::operator>>(fstream& file)
  294. {
  295. int size = sizeof(Complex)*mSize;
  296.  
  297. file.write((char*)(&pCpx[0]), size); //write(const unsigned char buf,int num),從buf指向的暫存區寫入num長度的字元到文件中
  298. //由於pCpx[]是Complex型態,因此需要先在前面加"&"讓它成為指標,再用強制轉換變成char指標
  299. return *this; //到時候執行時,程式就會從pCpx[0]當下指著的VectorCpx上,一口氣把所有的數字都寫到文件中
  300. }
  301.  
  302. AdvVectorIO& AdvVectorIO::operator<<(fstream& file)
  303. {
  304. Complex reg;
  305.  
  306. while(file.read((char*)(&reg), sizeof(Complex))) //read(unsigned char buf,int num),從文件中讀取num長度的字元到buf指向的暫存區
  307. push_back(reg); //每從文件讀一組數字進來,就push_back這組數字到原本的VectorCpx後面
  308.  
  309. return *this;
  310. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:23: fatal error: VectorCpx.h: No such file or directory
compilation terminated.
stdout
Standard output is empty