1 #include "BigIntegerUtils.hh"
2 #include "BigUnsignedInABase.hh"
6 * (1) `std::string <=> BigUnsigned/BigInteger' conversion routines easier than `BigUnsignedInABase'
7 * (2) << and >> operators for BigUnsigned/BigInteger, std::istream/std::ostream
10 std::string easyBUtoString(const BigUnsigned &x) {
11 return std::string(BigUnsignedInABase(x, 10));
14 std::string easyBItoString(const BigInteger &x) {
15 return (x.getSign() == BigInteger::negative) ?
16 (std::string("-") + easyBUtoString(x)) : (easyBUtoString(x));
19 BigUnsigned easyStringToBU(const std::string &s) {
20 return BigUnsigned(BigUnsignedInABase(s, 10));
23 BigInteger easyStringToBI(const std::string &s) {
24 return (s[0] == '-') ?
25 BigInteger(easyStringToBU(s.substr(1, s.length() - 1)), BigInteger::negative) :
26 BigInteger(easyStringToBU(s));
29 // Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
30 std::ostream &operator <<(std::ostream &os, const BigUnsigned &x) {
31 BigUnsignedInABase::Base base;
32 long osFlags = os.flags();
35 else if (osFlags & os.hex) {
37 if (osFlags & os.showbase)
39 } else if (osFlags & os.oct) {
41 if (osFlags & os.showbase)
44 throw "std::ostream << BigUnsigned: Could not determine the desired base from output-stream flags";
45 std::string s = std::string(BigUnsignedInABase(x, base));
49 // Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
50 // My somewhat arbitrary policy: a negative sign comes before a base indicator (like -0xFF).
51 std::ostream &operator <<(std::ostream &os, const BigInteger &x) {
52 if (x.getSign() == BigInteger::negative)
54 os << (const BigUnsigned &)(x);