Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int del(int a, int b) {
  5. if (!b) {
  6. return a;
  7. }
  8. return (b, a % b);
  9. }
  10. class Rational {
  11. public:
  12. int a,b;
  13. Rational& Add(Rational const & q) {
  14. int one = a * q.b + b * q.a;
  15. int two = b * q.b;
  16. a = a / del(one, two);
  17. b = b / del(one, two);
  18. return *this;
  19. }
  20. Rational& Substruct(Rational const &q) {
  21. int one = a * q.b - b * q.a;
  22. int two = b * q.b;
  23. a = a / del(one, two);
  24. b = b / del(one, two);
  25. return *this;
  26. }
  27. Rational& Multiply(Rational const &q) {
  28. int one = a * q.a;
  29. int two = b * q.b;
  30. a = one / del(one, two);
  31. b = two / del(one, two);
  32. return *this;
  33. }
  34. Rational& Divide(Rational const &q) {
  35. int one = a * q.b;
  36. int two = b * q.a;
  37. a = one / del(one, two);
  38. b = two / del(one, two);
  39. return *this;
  40. }
  41. Rational(int c, int d) {
  42. a = c / del(c, d);
  43. b = d / del(c, d);
  44. }
  45. };
  46. int main()
  47. {
  48. Rational a(1,2);
  49. Rational b(2,4);
  50. a.Add(b).Multiply(b);
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement