Advertisement
huutho_96

Phân Số 1

Apr 20th, 2015
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. class PhanSo
  4. {
  5. public:
  6. PhanSo(int TuSo = 0, int MauSo = 1)
  7. {
  8. this->MauSo = MauSo;
  9. this->TuSo = TuSo;
  10. }
  11. PhanSo(double a)
  12. {
  13. this->MauSo = 1;
  14.  
  15. while (a != int(a))
  16. {
  17. a = a * 10;
  18. this->MauSo *= 10;
  19. }
  20. this->TuSo = a;
  21. RutGon();
  22. }
  23. void RutGon();
  24. friend istream& operator >> (istream& in, PhanSo &p);
  25. friend ostream& operator << (ostream& out, PhanSo p);
  26. friend bool operator > (PhanSo a, PhanSo b);
  27. friend bool operator < (PhanSo a, PhanSo b);
  28. friend bool operator == (PhanSo a, PhanSo b);
  29. friend bool operator <= (PhanSo a, PhanSo b);
  30. friend bool operator >= (PhanSo a, PhanSo b);
  31. friend PhanSo operator +(PhanSo a, PhanSo b);
  32. friend PhanSo operator -(PhanSo a, PhanSo b);
  33. friend PhanSo operator *(PhanSo a, PhanSo b);
  34. friend PhanSo operator /(PhanSo a, PhanSo b);
  35. friend void operator += (PhanSo &a, PhanSo b);
  36. friend void operator -= (PhanSo &a, PhanSo b);
  37. friend void operator *= (PhanSo &a, PhanSo b);
  38. friend void operator /= (PhanSo &a, PhanSo b);
  39. private:
  40. int TuSo, MauSo;
  41. };
  42. //Rút Gọn 1 phân số
  43. void PhanSo::RutGon()
  44. {
  45. int iDau = 1;
  46. if (this->TuSo * this->MauSo < 0) iDau = -1;
  47. this->TuSo = abs(this->TuSo);
  48. this->MauSo = abs(this->MauSo);
  49. for (int i = this->TuSo; i > 1; i--)
  50. {
  51. if (this->TuSo % i == 0 && this->MauSo % i == 0)
  52. {
  53. this->TuSo /= i;
  54. this->MauSo /= i;
  55. break;
  56. }
  57. }
  58. this->TuSo *= iDau;
  59. }
  60. //Toán tử nhập
  61. istream& operator >> (istream& in, PhanSo &PS)
  62. {
  63. cout << "Nhap tu so : ";
  64. in >> PS.TuSo;
  65. cout << "Nhap mau so: ";
  66. in >> PS.MauSo;
  67. if (PS.MauSo == 0)
  68. {
  69. cout << "ERROR\n";
  70. cin >> PS;
  71. }
  72. return in;
  73. }
  74. //Toán tử xuất
  75. ostream& operator << (ostream& out, PhanSo PS)
  76. {
  77. if (PS.TuSo != 0)
  78. {
  79. out << PS.TuSo;
  80. if (PS.MauSo != 1)
  81. {
  82. cout << " / ";
  83. out << PS.MauSo;
  84. }
  85. }
  86. else
  87. out << "0";
  88. return out;
  89. }
  90. //So sánh >, <, ==, >=, <=
  91. bool operator > (PhanSo a, PhanSo b)
  92. {
  93. return (float(a.TuSo) / a.MauSo > float(b.TuSo) / b.MauSo);
  94. }
  95. bool operator < (PhanSo a, PhanSo b)
  96. {
  97. return (float(a.TuSo) / a.MauSo < float(b.TuSo) / b.MauSo);
  98. }
  99. bool operator == (PhanSo a, PhanSo b)
  100. {
  101. return (float(a.TuSo) / a.MauSo == float(b.TuSo) / b.MauSo);
  102. }
  103. bool operator >= (PhanSo a, PhanSo b)
  104. {
  105. return (float(a.TuSo) / a.MauSo >= float(b.TuSo) / b.MauSo);
  106. }
  107. bool operator <= (PhanSo a, PhanSo b)
  108. {
  109. return (float(a.TuSo) / a.MauSo <= float(b.TuSo) / b.MauSo);
  110. }
  111. //Toán tử +, -, *, /
  112. PhanSo operator + (PhanSo a, PhanSo b)
  113. {
  114. PhanSo Kq;
  115. Kq.MauSo = a.MauSo * b.MauSo;
  116. Kq.TuSo = a.TuSo * b.MauSo + a.MauSo * b.TuSo;
  117. Kq.RutGon();
  118. return Kq;
  119. }
  120. PhanSo operator - (PhanSo a, PhanSo b)
  121. {
  122. PhanSo Kq;
  123. Kq.MauSo = a.MauSo * b.MauSo;
  124. Kq.TuSo = a.TuSo * b.MauSo - a.MauSo * b.TuSo;
  125. Kq.RutGon();
  126. return Kq;
  127. }
  128. PhanSo operator * (PhanSo a, PhanSo b)
  129. {
  130. PhanSo Kq;
  131. Kq.MauSo = a.MauSo * b.MauSo;
  132. Kq.TuSo = a.TuSo * b.TuSo;
  133. Kq.RutGon();
  134. return Kq;
  135. }
  136. PhanSo operator / (PhanSo a, PhanSo b)
  137. {
  138. PhanSo Kq;
  139. Kq.MauSo = a.MauSo * b.TuSo;
  140. Kq.TuSo = a.TuSo * b.MauSo;
  141. Kq.RutGon();
  142. return Kq;
  143. }
  144. //Toán tử +=, -=; *=, /=
  145. void operator += (PhanSo &a, PhanSo b)
  146. {
  147. a = a + b;
  148. }
  149. void operator -= (PhanSo &a, PhanSo b)
  150. {
  151. a = a - b;
  152. }
  153. void operator *= (PhanSo &a, PhanSo b)
  154. {
  155. a = a * b;
  156. }
  157. void operator /= (PhanSo &a, PhanSo b)
  158. {
  159. a = a / b;
  160. }
  161.  
  162. void main()
  163. {
  164. PhanSo P(1, 1), a;
  165. a = P;
  166. float b = 1.1;
  167. cout << 1 + a + 1.1;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement