Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int gcd (int a, int b) {
  4. if (b == 0) {
  5. return a;
  6. } else {
  7. return gcd (b, a % b);
  8. }
  9. }
  10.  
  11. class Rational {
  12. private:
  13. int x, y;
  14.  
  15. public:
  16. Rational(int a = 0, int b = 1): x(a), y(b) {
  17. standart();
  18. }
  19.  
  20. int numerator() const {
  21. return x;
  22. }
  23.  
  24. int denominator() const {
  25. return y;
  26. }
  27.  
  28. int& num() {
  29. return x;
  30. }
  31.  
  32. int& den() {
  33. return y;
  34. }
  35.  
  36. void standart() {
  37. if (y < 0) {
  38. y = -y;
  39. x = -x;
  40. }
  41. int g = gcd(std::abs(x), std::abs(y));
  42. x /= g;
  43. y /= g;
  44. }
  45.  
  46. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement