HeatPulse

Kompleksni broevi

Apr 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. class ComplexNumber {
  6. private:
  7. double real;
  8. double imaginary;
  9. public:
  10. ComplexNumber(double real=0, double imaginary=0) {
  11. this->real = real;
  12. this->imaginary = imaginary;
  13. }
  14. ComplexNumber(const ComplexNumber &a) {
  15. this->real = a.real;
  16. this->imaginary = a.imaginary;
  17. }
  18. ComplexNumber &operator=(const ComplexNumber &a) {
  19. if(this == &a) {
  20. return *this;
  21. }
  22. this->real = a.real;
  23. this->imaginary = a.imaginary;
  24. return *this;
  25. }
  26.  
  27. double module() {
  28. return sqrt(pow(real, 2) + pow(imaginary, 2));
  29. }
  30.  
  31. friend ostream &operator<<(ostream &out, const ComplexNumber &a) {
  32. out<<a.real<<"+"<<a.imaginary<<"i"<<endl;
  33. return out;
  34. }
  35.  
  36. friend ComplexNumber operator+(const ComplexNumber &a, const ComplexNumber &b) {
  37. ComplexNumber c(a.real + b.real, a.imaginary + b.imaginary);
  38. return c;
  39. }
  40.  
  41. friend ComplexNumber operator-(const ComplexNumber &a, const ComplexNumber &b) {
  42. ComplexNumber c(a.real - b.real, a.imaginary - b.imaginary);
  43. return c;
  44. }
  45.  
  46. friend ComplexNumber operator*(const ComplexNumber &a, const ComplexNumber &b) {
  47. ComplexNumber c(a.real * b.real, a.imaginary * b.imaginary);
  48. return c;
  49. }
  50.  
  51. friend ComplexNumber operator/(const ComplexNumber &a, const ComplexNumber &b) {
  52. ComplexNumber c(a.real / b.real, a.imaginary / b.imaginary);
  53. return c;
  54. }
  55.  
  56. friend bool operator==(const ComplexNumber &a, const ComplexNumber &b) {
  57. if(a.real == b.real && a.imaginary == b.imaginary) {
  58. return true;
  59. }
  60. return false;
  61. }
  62.  
  63. friend bool operator>(const ComplexNumber &a, const ComplexNumber &b) {
  64. if(a.real > b.real) {
  65. return true;
  66. } else if(a.real == b.real) {
  67. if(a.imaginary > b.imaginary) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73.  
  74. friend bool operator<(const ComplexNumber &a, const ComplexNumber &b) {
  75. if(a.real < b.real) {
  76. return true;
  77. } else if(a.real == b.real) {
  78. if(a.imaginary < b.imaginary) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84.  
  85. };
  86.  
  87. class Equation {
  88. private:
  89. ComplexNumber *p;
  90. char *op;
  91. int n;
  92. public:
  93. Equation() {
  94. n = 0;
  95. p = NULL;
  96. op = NULL;
  97. }
  98.  
  99. friend istream &operator>>(istream &in, Equation &a) {
  100. double r, im;
  101. char rator;
  102. while(1) {
  103. in>>r>>im>>rator;
  104. ComplexNumber pom(r, im);
  105. ComplexNumber *tmp = new ComplexNumber[a.n+1];
  106. char *tmp2 = new char[a.n+1];
  107. for(int i=0; i<a.n; i++) {
  108. tmp2[i] = a.op[i];
  109. tmp[i] = a.p[i];
  110. }
  111. tmp[a.n] = pom;
  112. tmp2[a.n] = rator;
  113. a.n++;
  114. delete [] a.op;
  115. delete [] a.p;
  116. a.p = tmp;
  117. a.op = tmp2;
  118.  
  119. if(rator == '=') {
  120. break;
  121. }
  122. }
  123. return in;
  124. }
  125.  
  126. ~Equation() {
  127. delete [] p;
  128. delete [] op;
  129. }
  130. ComplexNumber result() {
  131. ComplexNumber res(p[0]);
  132. for(int i=1; i<n; i++) {
  133. if(op[i-1] == '+') {
  134. res = res + p[i];
  135. }
  136. if(op[i-1] == '-') {
  137. res = res - p[i];
  138. }
  139. if(op[i-1] == '*') {
  140. res = res * p[i];
  141. }
  142. if(op[i-1] == '/') {
  143. res = res / p[i];
  144. }
  145. }
  146. return res;
  147. }
  148.  
  149. double sumModules() {
  150. double sum = 0;
  151. for(int i=0; i<n; i++) {
  152. sum+= p[i].module();
  153. }
  154.  
  155. return sum;
  156. }
  157. };
Add Comment
Please, Sign In to add comment