#ifndef COMPLETE_HPP #define COMPLETE_HPP #include #include template T infinity() { } template struct Complete { Complete() : _value(0), _infinity(false) { } Complete(const T& value) : _value(value), _infinity(false) { } Complete(const T& value, const bool& infinity) : _value(value), _infinity(infinity) { assert(value != 0 || infinity == false); } Complete(const Complete& other) : _value(other._value), _infinity(other._infinity) { } Complete& operator=(const Complete& other) { _value = other._value; _infinity = other._infinity; return *this; } Complete& operator+=(const Complete& other) { return (*this) = (*this) + other; } Complete& operator-=(const Complete& other) { return (*this) = (*this) - other; } Complete& operator*=(const Complete& other) { return (*this) = (*this) * other; } Complete operator-() const { return Complete(- _value, _infinity); } Complete operator+(const Complete& other) const { if (_infinity) { return *this; } else if (other._infinity) { return other; } else { return Complete(_value + other._value, false); } } Complete operator-(const Complete& other) const { return *this + (- other); } Complete operator*(const Complete& other) const { return Complete(_value * other._value, (_infinity || other._infinity)); } bool operator!() const { return _value == 0; } bool operator<(const Complete& other) const { if (*this == other) return false; if (_infinity) { return _value < 0; } else if (other._infinity) { return other._value > 0; } else { return _value < other._value; } } bool operator>=(const Complete& other) const { return !(*this < other); } bool operator>(const Complete& other) const { return other < *this; } bool operator<=(const Complete& other) const { return !(*this > other); } bool operator==(const Complete& other) const { if (_infinity) { return other._infinity && ((_value < 0 && other._value < 0) || (_value > 0 && other._value > 0)); } else { return !other._infinity && (_value == other._value); } } bool operator!=(const Complete& other) const { return !(*this == other); } template friend std::istream& operator<<(std::istream&, Complete&); template friend std::ostream& operator<<(std::ostream&, const Complete&); private: T _value; bool _infinity; }; template std::istream& operator>>(std::istream& cin, Complete& num) { Z value; cin >> value; num = Complete(value, false); return cin; } template std::ostream& operator<<(std::ostream& cout, const Complete& num) { if (num._infinity) { cout << (num._value > 0 ? "inf" : "-inf"); } else { cout << num._value; } return cout; } template<> Complete infinity() { return Complete(1, true); } #endif