1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
#ifndef COMPLETE_HPP
#define COMPLETE_HPP
#include <ostream>
#include <istream>
template<typename T>
T infinity() { }
template<typename T>
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) {
if (value == 0 && infinity == true) {
throw "Zero infinity? Die die die!";
}
}
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 {
return Complete<T>(- _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 {
if (_infinity) {
return *this;
} else if (other._infinity) {
return other;
} else {
return Complete(_value + other._value, false);
}
}
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 other < *this;
}
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<typename Z>
friend std::istream& operator<<(std::istream&, Complete<Z>&);
template<typename Z>
friend std::ostream& operator<<(std::ostream&, const Complete<Z>&);
private:
T _value;
bool _infinity;
};
template<typename Z>
std::istream& operator>>(std::istream& cin, Complete<Z>& num) {
Z value;
cin >> value;
num = Complete<Z>(value, false);
return cin;
}
template<typename Z>
std::ostream& operator<<(std::ostream& cout, const Complete<Z>& num) {
if (num._infinity) {
cout << (num._value > 0 ? "inf" : "-inf");
} else {
cout << num._value;
}
return cout;
}
template<>
Complete<int> infinity() {
return Complete<int>(1, true);
}
#endif
|