Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. class Plural {
  2. private:
  3. double real = 0;
  4. double imaginary = 0;
  5. public:
  6. plural(double a, double b) {
  7. real = a;
  8. imaginary = b;
  9. }
  10.  
  11. double get_real() {
  12. return real;
  13. }
  14.  
  15. double get_imaginary() {
  16. return imaginary;
  17. }
  18.  
  19. double modulo() {
  20. return real * real + imaginary * imaginary;
  21. }
  22.  
  23.  
  24. friend plural operator+(plural first, plural second);
  25.  
  26. friend plural operator-(plural first, plural second);
  27.  
  28. friend plural operator*(plural first, plural second);
  29.  
  30. friend plural operator/(plural first, plural second);
  31. };
  32.  
  33. plural operator+(plural first, plural second) {
  34. double r = first.real + second.real;
  35. double i = first.imaginary + second.imaginary;
  36. return plural(r, i);
  37. }
  38.  
  39. plural operator-(plural first, plural second) {
  40. double r = first.real - second.real;
  41. double i = first.imaginary - second.imaginary;
  42. return plural(r, i);
  43. }
  44.  
  45. plural operator*(plural first, plural second) {
  46. //r=ac-bd
  47. double r = first.real * second.real - first.imaginary * second.imaginary;
  48. //i=bc+ad
  49. double i = first.real * second.imaginary + first.imaginary * second.real;
  50. return plural(r, i);
  51. }
  52.  
  53. plural operator/(plural first, plural second) {
  54. //r=(ac+bd)/(c*c+d*d)
  55. double r = (first.real * second.real + first.imaginary * second.imaginary) /
  56. (second.real * second.real + second.imaginary * second.imaginary);
  57. //i=(bc-ad)/(c*c+d*d)
  58. double i = (first.imaginary * second.real - first.real * second.imaginary) /
  59. (second.real * second.real + second.imaginary * second.imaginary);
  60. return plural(r, i);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement