Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int del(int a, int b) {
- if (!b) {
- return a;
- }
- return (b, a % b);
- }
- class Rational {
- public:
- int a,b;
- Rational& Add(Rational const & q) {
- int one = a * q.b + b * q.a;
- int two = b * q.b;
- a = a / del(one, two);
- b = b / del(one, two);
- return *this;
- }
- Rational& Substruct(Rational const &q) {
- int one = a * q.b - b * q.a;
- int two = b * q.b;
- a = a / del(one, two);
- b = b / del(one, two);
- return *this;
- }
- Rational& Multiply(Rational const &q) {
- int one = a * q.a;
- int two = b * q.b;
- a = one / del(one, two);
- b = two / del(one, two);
- return *this;
- }
- Rational& Divide(Rational const &q) {
- int one = a * q.b;
- int two = b * q.a;
- a = one / del(one, two);
- b = two / del(one, two);
- return *this;
- }
- Rational(int c, int d) {
- a = c / del(c, d);
- b = d / del(c, d);
- }
- };
- int main()
- {
- Rational a(1,2);
- Rational b(2,4);
- a.Add(b).Multiply(b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement