Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <numeric>
- #include <algorithm>
- #include <cmath>
- #include <iostream>
- #include <map>
- #include <set>
- #include <string>
- #include <utility>
- #include <vector>
- using namespace std;
- class Rational {
- public:
- Rational() = default;
- Rational(int numerator)
- : numerator_(numerator)
- , denominator_(1) {
- }
- Rational(int numerator, int denominator)
- : numerator_(numerator)
- , denominator_(denominator) {
- Normalize();
- }
- int Numerator() const {
- return numerator_;
- }
- int Denominator() const {
- return denominator_;
- }
- private:
- void Normalize() {
- if (denominator_ < 0) {
- numerator_ = -numerator_;
- denominator_ = -denominator_;
- }
- const int divisor = gcd(numerator_, denominator_);
- numerator_ /= divisor;
- denominator_ /= divisor;
- }
- int numerator_ = 0;
- int denominator_ = 1;
- };
- ostream& operator<<(ostream& output, Rational MyValue )
- {
- output<<MyValue.Numerator()<<"/"<<MyValue.Denominator();
- return output;
- }
- istream& operator>>(istream& in, Rational& rat){
- int numerator_, denominator_;
- char slash;
- in >> numerator_ >> slash >> denominator_;
- rat = Rational{numerator_, denominator_};
- return in;
- }
- int main() {
- Rational r10;
- cin >> r10;
- Rational r1();
- Rational r2(8);
- Rational r3(-8);
- Rational r4(1,2);
- Rational r5(-1,2);
- Rational r6(10,-20);
- Rational r7(-1,-2);
- Rational r8(10,15);
- Rational r9(0,-5);
- cout << r10<<endl;
- cout << r7<<endl;
- cout << r8<< endl;
- cout << r9 <<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement