przeładowanie operatorów - kalkulator liczb zespolonych

nie jest jakiś wyszukany, brakuje sporo funkcji, no ale ;p pomijam fakt że czasem potrafi wywalić dziwne rzeczy typu (0,-0) których nie chce mi się eliminować, ale w 70% spełnia swoje zadanie (pozostałe 30% to brak sin, cos, exp, ln itp).
klik:


Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <iostream>
  2. #include "math.h"
  3. #define M_PI 3.14159265358979323846
  4.  
  5.  
  6. using namespace std;
  7.  
  8. template<class T>
  9.  
  10. class zesp
  11. {
  12. public:
  13. zesp () { };
  14. T getX()const{ return real; }
  15. T getY()const{return imag;}
  16. void getX(T wartosc){
  17. real=wartosc;}
  18. void getY(T wartosc){
  19. imag=wartosc;}
  20.  
  21.  
  22. zesp(T r, T i)
  23. {
  24. real = r;
  25. imag = i;
  26. };
  27.  
  28. zesp operator=(const zesp& a);
  29.  
  30. zesp(const zesp& wartosc);
  31.  
  32. zesp operator+ (zesp); //dodawanie
  33.  
  34.  
  35. T abs() //modul
  36. {
  37. return(sqrt((real*real)+(imag*imag)));
  38. }
  39.  
  40. zesp conj()//liczba sprzezona
  41. {
  42. if(imag!=0)
  43. return(zesp(real, -imag));
  44. else return(zesp(real,imag));
  45. }
  46.  
  47. T faza()
  48. {
  49. if (real >= 0) return (atan2(imag, real));
  50. return (M_PI + atan2(imag, real));
  51. }
  52.  
  53. friend zesp operator* (const zesp o1, const zesp o2)
  54. {
  55. zesp o;
  56. o.real = (o1.real * o2.real) - (o1.imag * o2.imag);
  57. o.imag = (o1.real * o2.imag) + (o1.imag * o2.real);
  58. return o;
  59. }
  60. friend zesp operator/ (const zesp o1, const zesp o2) {
  61.  
  62. zesp o;
  63. o.real = ((o1.real * o2.real) + (o1.imag * o2.imag))/((o2.real * o2.real) + (o2.imag * o2.imag));
  64. o.imag = (((o1.real * o2.imag) - (o1.imag * o2.real))*(-1))/((o2.real * o2.real) + (o2.imag * o2.imag));
  65. return o;
  66.  
  67. }
  68.  
  69. friend ostream & operator << (ostream & s, const zesp & c)
  70. {
  71. s << "(" << c.real << "," << c.imag << ")";
  72. return s;
  73. }
  74.  
  75.  
  76.  
  77. private:
  78. T real;
  79. T imag;
  80.  
  81. };
  82.  
  83. template<class T>
  84. zesp<T> zesp<T>::operator+( zesp c) {
  85. zesp n;
  86. n.real = this->real + c.real;
  87. n.imag = this->imag + c.imag;
  88. return n;
  89. }
  90.  
  91. template<class T>
  92. zesp<T> zesp<T>::operator=(const zesp& a)
  93. {
  94. return zesp(a); }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. template<class T>
  102. zesp<T>::zesp(const zesp& wartosc)//kostruktor(inicjalizator) kopiujący(z funkcja typu const zz przezwiskiem klsy Complex na wartosc, po to by nie działac na oryginalnym obiekcie)
  103. {
  104. imag=wartosc.imag;
  105. real=wartosc.real;
  106.  
  107. }
  108.  
  109.  
  110.  
  111. int main()
  112. {
  113.  
  114.  
  115. int rea=0;
  116. int ima=0;
  117. int reb=0;
  118. int imb=0;
  119.  
  120.  
  121. cout<<"podaj czesc re liczby a: \n";
  122. cin>>rea;
  123. cout<<"podaj czesc im liczby a: \n";
  124. cin>>ima;
  125.  
  126. zesp<double>a(rea,ima);
  127. cout<<"liczba a: "<<a.getX()<<"+"<<a.getY()<<"i\n\n";
  128.  
  129. cout<<"podaj czesc re liczby b: \n";
  130. cin>>reb;
  131. cout<<"podaj czesc im liczby b: \n";
  132. cin>>imb;
  133.  
  134. zesp<double> b(reb,imb);
  135. cout<<"liczba b: "<<b.getX()<<"+"<<b.getY()<<"i\n\n";
  136.  
  137.  
  138. cout<<"\nmnozenie: \n";
  139. cout << a*b<<endl;
  140.  
  141. cout<<"\npodziel: \n";
  142.  
  143. cout << a/b << endl;
  144.  
  145. cout<<"\nmodul: \n";
  146. cout << a.abs() << endl;
  147. cout << b.abs() << endl;
  148. cout<<"\nliczba sprzezona: \n";
  149. cout << a.conj() << endl;
  150. cout << b.conj() << endl;
  151.  
  152. cout<<"\nfaza liczby zespolonej: \n";
  153. cout << a.faza() << endl;
  154. cout << b.faza() << endl;
  155.  
  156.  
  157. system("pause");
  158. return 0;
  159. }

Komentarze

Popularne posty z tego bloga

[c++] Lista wskaznikowa

[c++] słowniki - haszowanie - haszowanie otwarte

[ANSI C][LINUX] Wysyłanie i obsługa sygnałów