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.getMagnitude()))
17 : (easyBUtoString(x.getMagnitude()));
20 BigUnsigned easyStringToBU(const std::string &s) {
21 return BigUnsigned(BigUnsignedInABase(s, 10));
24 BigInteger easyStringToBI(const std::string &s) {
25 return (s[0] == '-') ?
26 BigInteger(easyStringToBU(s.substr(1, s.length() - 1)), BigInteger::negative) :
27 BigInteger(easyStringToBU(s));
30 // Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
31 std::ostream &operator <<(std::ostream &os, const BigUnsigned &x) {
32 BigUnsignedInABase::Base base;
33 long osFlags = os.flags();
36 else if (osFlags & os.hex) {
38 if (osFlags & os.showbase)
40 } else if (osFlags & os.oct) {
42 if (osFlags & os.showbase)
45 throw "std::ostream << BigUnsigned: Could not determine the desired base from output-stream flags";
46 std::string s = std::string(BigUnsignedInABase(x, base));
50 // Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
51 // My somewhat arbitrary policy: a negative sign comes before a base indicator (like -0xFF).
52 std::ostream &operator <<(std::ostream &os, const BigInteger &x) {
53 if (x.getSign() == BigInteger::negative)
55 os << x.getMagnitude();