Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. #include "Complex.h"
  2. #include <cmath>
  3.  
  4. Complex::Complex()
  5. {
  6. re = 0;
  7. im = 0;
  8. }
  9.  
  10. Complex::Complex(double re, double im)
  11. {
  12. this->re = re;
  13. this->im = im;
  14. }
  15.  
  16. double Complex::getRe()
  17. {
  18. return this->re;
  19. }
  20.  
  21. double Complex::getIm()
  22. {
  23. return this->im;
  24. }
  25.  
  26. void Complex::setRe(double re)
  27. {
  28. this->re = re;
  29. }
  30.  
  31. void Complex::setIm(double im)
  32. {
  33. this->im = im;
  34. }
  35.  
  36. istream & operator>>(istream &f, Complex & c)
  37. {
  38. std::cout<<endl;
  39. std::cout<<"Introduceti partea reala"<<' ';
  40. f >> c.re;
  41. std::cout<<"Introduceti partea imaginara"<<' ';
  42. f >> c.im;
  43. return f;
  44. }
  45.  
  46. ostream & operator<<(ostream & f, Complex c)
  47. {
  48. if(c.re==0)
  49. {
  50. if(c.im >= 0)
  51. {
  52. if(c.im==0)
  53. {
  54. cout<<'0';
  55. }
  56. if(c.im==1)
  57. {
  58. cout<<'i';
  59. }
  60. if(c.im!=0)
  61. {
  62. cout<<"i*"<<c.im;
  63. }
  64. }
  65. else cout<<"-i*"<<-c.im;
  66. }
  67. else
  68. {
  69. if(c.im >= 0)
  70. {
  71. if(c.im==0)
  72. {
  73. cout<<c.re;
  74. }
  75. if(c.im==1)
  76. {
  77. cout<<c.re<<"+i";
  78. }
  79. if(c.im > 0 && c.im !=0 && c.im != 1)
  80. {
  81. cout<<c.re<<"+i*"<<c.im;
  82. }
  83. }
  84. if(c.im < 0 && c.im != -1)
  85. cout<<c.re<<"-i*"<<-c.im;
  86. if(c.im == -1)
  87. {
  88. cout<<c.re<<"-i";
  89. }
  90. }
  91.  
  92. return f;
  93. }
  94.  
  95. Complex operator+(Complex a, Complex b)
  96. {
  97. return Complex(a.re + b.re, a.im + b.im);
  98. }
  99.  
  100. Complex operator+(Complex a, double b)
  101. {
  102. return Complex(a.re + b, a.im);
  103. }
  104.  
  105. Complex operator+(double a, Complex b)
  106. {
  107. return Complex(b.re + a, b.im);
  108. }
  109.  
  110. Complex operator+(Complex a)
  111. {
  112. return a;
  113. }
  114.  
  115. Complex operator-(Complex a, Complex b)
  116. {
  117. return Complex(a.re - b.re, a.im - b.im);
  118. }
  119.  
  120. Complex operator-(Complex a, double b)
  121. {
  122. return Complex(a.re - b, a.im);
  123. }
  124.  
  125. Complex operator-(double a, Complex b)
  126. {
  127. return Complex(b.re - a, b.im);
  128. }
  129.  
  130. Complex operator-(Complex a)
  131. {
  132. return Complex(-a.re, -a.re);
  133. }
  134.  
  135. Complex operator*(Complex a, Complex b)
  136. {
  137.  
  138. return Complex(a.re*b.re - a.im*b.im, a.re*b.im + a.im*b.re);
  139. }
  140.  
  141. Complex operator*(Complex a, double b)
  142. {
  143. return Complex(a.re*b, a.im*b);
  144. }
  145.  
  146. Complex operator*(double a, Complex b)
  147. {
  148. return Complex(a*b.re, a*b.im);
  149. }
  150.  
  151. Complex operator/(Complex a, Complex b)
  152. {
  153. if(b.im>0)
  154. {
  155. if( (a.re*b.re+a.im*b.im-a.im*b.re-a.re*b.im) != 0)
  156. {
  157. return Complex( (a.re*b.re+a.im*b.im)/(b.re*b.re+b.im*b.im), (a.im*b.re+a.re*b.im)/(b.re*b.re+b.im*b.im) );
  158. }
  159. else return Complex(0,0);
  160. }
  161. else
  162. {
  163. return Complex( (a.re*b.re+a.im*b.im)/(b.re*b.re+b.im*b.im), (a.im*b.re-a.re*b.im)/(b.im*b.re-b.im*b.re) );
  164. }
  165. }
  166.  
  167. Complex operator/(Complex a, double b)
  168. {
  169. if(a.im!=0 )
  170. return Complex( a.re/b, a.im/b);
  171. }
  172.  
  173. Complex operator/(double a, Complex b)
  174. {
  175. if(b.im>0)
  176. {
  177. return Complex( a*b.re/(b.re*b.re-b.im*b.im), a*b.im/(b.re*b.re-b.im*b.im) );
  178. }
  179. else
  180. {
  181. return Complex( a*b.re/(b.re*b.re+b.im*b.im), a*b.im/(b.re*b.re+b.im*b.im));
  182. }
  183. }
  184.  
  185. bool operator==(Complex a, Complex b)
  186. {
  187. if(a.re==b.re && a.im==b.im)
  188. return true;
  189. else return false;
  190. }
  191.  
  192. bool operator!=(Complex a, Complex b)
  193. {
  194. if(a.re!=b.re && a.im!=b.im)
  195. return true;
  196. else return false;
  197. }
  198.  
  199. Complex & operator+=(Complex &a, Complex b)
  200. {
  201. a=Complex(a.re+b.re, a.im+b.im);
  202. return a;
  203. }
  204.  
  205. Complex & operator+=(Complex &a, double b)
  206. {
  207. a=Complex(a.re+b, a.im);
  208. return a;
  209. }
  210.  
  211. Complex & operator-=(Complex &a, Complex b)
  212. {
  213. a=Complex(a.re-b.re, a.im-b.im);
  214. return a;
  215. }
  216.  
  217. Complex & operator-=(Complex &a, double b)
  218. {
  219. a=Complex(a.re-b, a.im);
  220. return a;
  221. }
  222.  
  223. Complex & operator*=(Complex &a, Complex b)
  224. {
  225. a=Complex(a.re*b.re, a.im*b.im);
  226. return a;
  227. }
  228.  
  229. Complex & operator*=(Complex &a, double b)
  230. {
  231. a=Complex(a.re*b, a.im*b);
  232. return a;
  233. }
  234.  
  235. Complex & operator/=(Complex &a, Complex b)
  236. {
  237. if(b.re==0 && b.im==0)
  238. {
  239. a=Complex(0,0);
  240. }
  241. else
  242. a=Complex(a.re/b.re, a.im/b.im);
  243. return a;
  244. }
  245.  
  246. Complex & operator/=(Complex &a, double b)
  247. {
  248. if(b==0)
  249. a=Complex(0,0);
  250. else
  251. a=Complex(a.re/b, a.im);
  252. return a;
  253. }
  254.  
  255. Complex operator^(Complex a, int b)
  256. {
  257. Complex x=a;
  258. if(b==0)
  259. {
  260. return Complex(1,0);
  261. }
  262. else
  263. for ( int i=2; i<=b; i++)
  264. x=x*a;
  265. return x;
  266. }
  267.  
  268. double absc(Complex c)
  269. {
  270. if(c.re!=0 && c.im!=0)
  271. return sqrt(c.re*c.re + c.im*c.im);
  272. else
  273. if(c.re == 0)
  274. return(c.im);
  275. else
  276. if(c.im == 0)
  277. return(c.re);
  278. }
  279.  
  280. Complex sqrtc(Complex c)
  281. {
  282. if(c.im!= 0 && c.re!= 0)
  283. {
  284. if( (c.re+absc(c)) > 0 )
  285. {
  286. if( (-c.re + absc(c)) > 0 )
  287. {
  288. return Complex(sqrt(((c.re+absc(c))/2)),
  289. (abs(c.im)/c.im ) * sqrt( ( (-c.re + absc(c)) / 2) ) );
  290. }
  291. if( (-c.re + absc(c)) < 0 )
  292. {
  293. return Complex(sqrt(((c.re+absc(c))/2)),
  294. abs(c.im)/c.im * sqrt(-((-c.re + absc(c)) / 2)));
  295. }
  296. }
  297. else if( ((c.re+absc(c))/2) < 0 )
  298. {
  299. if( ((-c.re + absc(c)) / 2) > 0 )
  300. {
  301. return Complex(sqrt(((c.re+absc(c))/2)),
  302. abs(c.im)/c.im * sqrt( -((-c.re + absc(c)) / 2) ));
  303. }
  304. if( ((-c.re + absc(c)) / 2) < 0 )
  305. {
  306. return Complex(sqrt(-((c.re+absc(c))/2)),
  307. abs(c.im)/c.im * sqrt( -((-c.re + absc(c)) / 2) ));
  308. }
  309. }
  310.  
  311. }
  312. else
  313. if(c.re==0)
  314. {
  315. if(c.im > 0)
  316. return Complex( 0, sqrt(c.im) );
  317. else if(c.im < 0)
  318. return Complex(0,sqrt(-c.im));
  319.  
  320. }
  321. else
  322. if(c.im==0)
  323. {
  324. if(c.re > 0)
  325. return Complex(sqrt(c.re),0);
  326. else if (c.re < 0)
  327. return Complex(sqrt(c.re),0);
  328. }
  329.  
  330.  
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement