implemented ostream output op for integer.cpp
This commit is contained in:
parent
3b3470ed35
commit
01c0cbb0ec
1 changed files with 23 additions and 1 deletions
24
integer.cpp
24
integer.cpp
|
@ -2,6 +2,8 @@
|
|||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#define todo(msg) \
|
||||
std::cerr << msg << std::endl; \
|
||||
assert(false);
|
||||
|
@ -115,6 +117,12 @@ public:
|
|||
ret %= rhs;
|
||||
return ret;
|
||||
}
|
||||
friend Integer operator%(Integer const &lhs, byte rhs) {
|
||||
if (lhs.size == 0 || lhs.bytes == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
return lhs.bytes[0] % rhs;
|
||||
}
|
||||
friend Integer operator-(Integer const &val) {
|
||||
Integer ret = val;
|
||||
ret.isNegative = !ret.isNegative;
|
||||
|
@ -135,10 +143,24 @@ public:
|
|||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &lhs, Integer const &rhs) {
|
||||
Integer discard = rhs;
|
||||
if (rhs == 0) {
|
||||
lhs << 0;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
Integer discard = rhs;
|
||||
std::stringstream to_output{};
|
||||
while (discard != 0) {
|
||||
to_output << discard % 10;
|
||||
discard /= 10;
|
||||
}
|
||||
std::string out;
|
||||
to_output >> out;
|
||||
std::reverse(out.begin(), out.end());
|
||||
lhs << out;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
explicit operator unsigned long() {
|
||||
unsigned long ret = 0;
|
||||
for (std::size_t i = 0; i < std::min(size, sizeof(unsigned long)); i++) {
|
||||
|
|
Loading…
Reference in a new issue