Advertisement
Guest User

Комплексни броеви

a guest
Apr 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. #include<cstring>
  4. using namespace std;
  5. class ComplexNumber {
  6. protected:
  7. float real;
  8. float imaginary;
  9. public:
  10. //konst
  11. ComplexNumber() {}
  12. //konst so parametri
  13. ComplexNumber(float real,float imaginary) {
  14. this->real=real;
  15. this->imaginary=imaginary;
  16. }
  17. //formula
  18. double module() {
  19. return sqrt((pow(real,2)+pow(imaginary,2)));
  20. }
  21. //preoptovaruvanje na =
  22. ComplexNumber&operator=(const ComplexNumber &n) {
  23. if(this!=&n) {
  24. this->real=n.real;
  25. this->imaginary=n.imaginary;
  26. }
  27. return *this;
  28. }
  29. //preoptovaruvanje na <<
  30. friend ostream &operator<< (ostream &output, const ComplexNumber & n) {
  31. return output << n.real << "+" << n.imaginary<<"i"<<endl;
  32. }
  33. //preoptovaruvanje na +
  34. ComplexNumber operator + ( const ComplexNumber &n) {
  35. class ComplexNumber res;
  36. res.real = real + n.real;
  37. res.imaginary = imaginary + n.imaginary;
  38. return res;
  39. }
  40. //preoptovaruvanje na -
  41. ComplexNumber operator - ( const ComplexNumber &n) {
  42. class ComplexNumber sub;
  43. sub.real = real - n.real;
  44. sub.imaginary = imaginary - n.imaginary;
  45. return sub;
  46. }
  47. //preoptovaruvanje na *
  48. ComplexNumber operator * ( const ComplexNumber &n) {
  49. class ComplexNumber mul;
  50. mul.real = this->real*n.real ;
  51. mul.imaginary = this->imaginary * n.imaginary;
  52. return mul;
  53. }
  54. //preoptovaruvanje na /
  55. ComplexNumber operator / ( const ComplexNumber &n) {
  56. class ComplexNumber dev;
  57. dev.real = this->real/n.real ;
  58. dev.imaginary = this->imaginary / n.imaginary;
  59. return dev;
  60. }
  61. //preoptovaruvanje na ==
  62. bool operator==(const ComplexNumber &n) {
  63. if((this->real==n.real)&&(this->imaginary==n.imaginary)) {
  64. return true;
  65. } else {
  66. return false;
  67. }
  68. }
  69. //preoptovaruvanje na <
  70. bool operator <(const ComplexNumber &n) {
  71. if(this->imaginary<n.imaginary&&(this->imaginary<n.imaginary)){
  72. return true;}
  73. else {
  74. return false;
  75. }
  76. }
  77. //preoptovaruvanje na >
  78. bool operator >(const ComplexNumber &n) {
  79. if((this->real>n.real)&&(this->imaginary>n.imaginary)) {
  80. return true;
  81. } else {
  82. return false;
  83. }
  84. }
  85. };
  86. //----------------------------------------------------------------------------------------------------
  87. /*
  88. class Equation{
  89. private:
  90. ComplexNumber *c;
  91. int n;
  92. char *znaci;
  93. public:
  94. Equation(){
  95. c=new ComplexNumber[0];
  96. znaci=new char[0];
  97. n=0;
  98. }
  99. friend istream& operator>> (istream& is, Equation &c){
  100. ComplexNumber *temp=new ComplexNumber[n+1];
  101. for(int i=0;i<strlen(c);++i){
  102. while(*znaci!= '='){
  103. class ComplexNumber nova;
  104. is>>nova.real>>nova.imaginary;
  105. temp[i].real=nova.real;
  106. temp[i].imaginary=nova.imaginary;
  107.  
  108. }
  109. }
  110. delete [] c;
  111. c=temp;
  112. return is;
  113. }
  114. double sumModules(){
  115. double sum=0.0
  116. for(int i=0;i<strlen(c);++i){
  117. sum+=s[i].module();
  118.  
  119. }
  120. return sum;
  121. }
  122. };
  123. */
  124. //----------------------------------------------------------------------------------------------------
  125. int main() {
  126. int testCase;
  127. double real, imaginary;
  128. ComplexNumber first, second, result;
  129.  
  130. cin >> testCase;
  131.  
  132. if (testCase <= 8) {
  133. cin >> real;
  134. cin >> imaginary;
  135. first = ComplexNumber(real, imaginary);
  136. cin >> real;
  137. cin >> imaginary;
  138. second = ComplexNumber(real, imaginary);
  139. }
  140.  
  141. if (testCase == 1) {
  142. cout << "===== OPERATOR + =====" << endl;
  143. result = first + second;
  144. cout << result;
  145. } else if (testCase == 2) {
  146. cout << "===== OPERATOR - =====" << endl;
  147. result = first - second;
  148. cout << result;
  149. } else if (testCase == 3) {
  150. cout << "===== OPERATOR * =====" << endl;
  151. result = first * second;
  152. cout << result;
  153. } else if (testCase == 4) {
  154. cout << "===== OPERATOR / =====" << endl;
  155. result = first / second;
  156. cout << result;
  157. } else if (testCase == 5) {
  158. cout << "===== OPERATOR == =====" << endl;
  159. cout << first;
  160. cout << second;
  161. if (first == second)
  162. cout << "EQUAL" << endl;
  163. else
  164. cout << "NOT EQUAL" << endl;
  165. } else if (testCase == 6) {
  166. cout << "===== OPERATOR > =====" << endl;
  167. cout << first;
  168. cout << second;
  169. if (first > second)
  170. cout << "FIRST GREATER THAN SECOND" << endl;
  171. else
  172. cout << "FIRST LESSER THAN SECOND" << endl;
  173. } else if (testCase == 7) {
  174. cout << "===== OPERATOR < =====" << endl;
  175. cout << first;
  176. cout << second;
  177. if (first < second)
  178. cout << "FIRST LESSER THAN SECOND" << endl;
  179. else
  180. cout << "FIRST GREATER THAN SECOND" << endl;
  181. }/* else if (testCase == 8) {
  182. cout << "===== MODULE =====" << endl;
  183. double module = first.module();
  184. cout << first;
  185. cout << "Module: " << module << endl;
  186. cout << second;
  187. module = second.module();
  188. cout << "Module: " << module << endl;
  189. } else if (testCase == 9) {
  190. cout << "===== OPERATOR >> & SUM OF MODULES =====" << endl;
  191. Equation equation;
  192. cin >> equation;
  193. cout << equation.sumModules();
  194. } else if (testCase == 10) {
  195. cout << "===== EQUATION RESULT =====" << endl;
  196. Equation equation;
  197. cin >> equation;
  198. result = equation.result();
  199. cout << result;
  200. }*/
  201. return 0;
  202. }
  203. //za ova samo 5 poeni ce dobiete fala.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement