Advertisement
huutho_96

Số Phức

Apr 21st, 2015
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. class SoPhuc
  4. {
  5. public:
  6. SoPhuc(double Thuc = 0, double Ao = 0)
  7. {
  8. this->Ao = Ao;
  9. this->Thuc = Thuc;
  10. }
  11. friend istream& operator >> (istream& in, SoPhuc &p);
  12. friend ostream& operator << (ostream& out, SoPhuc p);
  13. friend bool operator > (SoPhuc a, SoPhuc b);
  14. friend bool operator < (SoPhuc a, SoPhuc b);
  15. friend bool operator == (SoPhuc a, SoPhuc b);
  16. friend bool operator <= (SoPhuc a, SoPhuc b);
  17. friend bool operator >= (SoPhuc a, SoPhuc b);
  18. friend SoPhuc operator +(SoPhuc a, SoPhuc b);
  19. friend SoPhuc operator -(SoPhuc a, SoPhuc b);
  20. friend SoPhuc operator *(SoPhuc a, SoPhuc b);
  21. friend SoPhuc operator /(SoPhuc a, SoPhuc b);
  22. friend void operator += (SoPhuc &a, SoPhuc b);
  23. friend void operator -= (SoPhuc &a, SoPhuc b);
  24. friend void operator *= (SoPhuc &a, SoPhuc b);
  25. friend void operator /= (SoPhuc &a, SoPhuc b);
  26. private:
  27. double Thuc, Ao;
  28. };
  29.  
  30. //Toán tử nhập
  31. istream& operator >> (istream& in, SoPhuc &PS)
  32. {
  33. cout << "Nhap phuc thuc: ";
  34. in >> PS.Thuc;
  35. cout << "Nhap phan ao : ";
  36. in >> PS.Ao;
  37. return in;
  38. }
  39. //Toán tử xuất
  40. ostream& operator << (ostream& out, SoPhuc PS)
  41. {
  42. out << "z = ";
  43. if (PS.Ao != 0)
  44. {
  45. if(PS.Ao > 0 ) out << PS.Thuc << " + " << PS.Ao << "i";
  46. else out << PS.Thuc << " - " << -1 * PS.Ao << "i";
  47. }
  48. else out << "0";
  49. return out;
  50. }
  51. //So sánh ==,
  52.  
  53. bool operator == (SoPhuc a, SoPhuc b)
  54. {
  55. return (a.Thuc == b.Thuc && a.Ao == b.Ao);
  56. }
  57.  
  58. //Toán tử +, -, *, /
  59. SoPhuc operator + (SoPhuc a, SoPhuc b)
  60. {
  61. SoPhuc Kq;
  62. Kq.Ao = a.Ao + b.Ao;
  63. Kq.Thuc = a.Thuc + b.Thuc;
  64. return Kq;
  65. }
  66. SoPhuc operator - (SoPhuc a, SoPhuc b)
  67. {
  68. SoPhuc Kq;
  69. Kq.Ao = a.Ao - b.Ao;
  70. Kq.Thuc = a.Thuc - b.Thuc;
  71. return Kq;
  72. }
  73. SoPhuc operator * (SoPhuc a, SoPhuc b)
  74. {
  75. SoPhuc Kq;
  76. Kq.Thuc = a.Thuc * b.Thuc - a.Ao * b.Ao;
  77. Kq.Ao = a.Thuc * b.Ao + a.Ao * b.Thuc;
  78. return Kq;
  79. }
  80. SoPhuc operator / (SoPhuc a, SoPhuc b)
  81. {
  82. SoPhuc Kq;
  83. Kq.Ao = (-a.Thuc * b.Ao + a.Ao * b.Thuc) / (b.Thuc * b.Thuc + b.Ao * b.Ao);
  84. Kq.Thuc = (a.Thuc * b.Thuc + a.Ao * b.Ao) / (b.Thuc * b.Thuc + b.Ao * b.Ao);
  85. return Kq;
  86. }
  87. //Toán tử +=, -=; *=, /=
  88. void operator += (SoPhuc &a, SoPhuc b)
  89. {
  90. a = a + b;
  91. }
  92. void operator -= (SoPhuc &a, SoPhuc b)
  93. {
  94. a = a - b;
  95. }
  96. void operator *= (SoPhuc &a, SoPhuc b)
  97. {
  98. a = a * b;
  99. }
  100. void operator /= (SoPhuc &a, SoPhuc b)
  101. {
  102. a = a / b;
  103. }
  104.  
  105. void main()
  106. {
  107. SoPhuc P(5, -1), a(3, 2);
  108. float b = 1.1;
  109. cout << a + P << endl;
  110. cout << a - P << endl;
  111. cout << a * P << endl;
  112. cout << a / P << endl;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement