1
0
Fork 0

Update number/fractional.cc

Signed-off-by: subcrip <contact@subc.rip>
This commit is contained in:
subcrip 2024-11-27 15:25:18 +00:00
parent 8fdd3c413f
commit 99e23be0e8
1 changed files with 16 additions and 0 deletions

View File

@ -26,4 +26,20 @@ struct fractional {
friend inline bool operator>(const fractional& lhs, const fractional& rhs) { return lhs >= rhs and lhs != rhs; } friend inline bool operator>(const fractional& lhs, const fractional& rhs) { return lhs >= rhs and lhs != rhs; }
friend inline bool operator<=(const fractional& lhs, const fractional& rhs) { return lhs < rhs or lhs == rhs; } friend inline bool operator<=(const fractional& lhs, const fractional& rhs) { return lhs < rhs or lhs == rhs; }
friend inline ostream& operator<<(ostream& out, const fractional& x) { return out << x.p << '/' << x.q; } friend inline ostream& operator<<(ostream& out, const fractional& x) { return out << x.p << '/' << x.q; }
friend inline istream& operator>>(istream& in, fractional& x) {
read(string, s);
auto point = find(s.begin(), s.end(), '.');
string left(s.begin(), point);
string right(point, s.end());
if (right.size()) right.erase(right.begin());
T divisor = 1;
for (auto&& _ : right) {
divisor *= 10;
}
T l, r;
stringstream(left) >> l;
stringstream(right) >> r;
x = l + fractional(r, divisor);
return in;
}
}; };