2 * Matt McCutchen's Big Integer Library
3 * http://mysite.verizon.net/mccutchen/bigint/
6 #include "BigIntegerUtils.hh"
7 #include "BigUnsignedInABase.hh"
11 * (1) `std::string <=> BigUnsigned/BigInteger' conversion routines easier than `BigUnsignedInABase'
12 * (2) << and >> operators for BigUnsigned/BigInteger, std::istream/std::ostream
15 std::string easyBUtoString(const BigUnsigned &x) {
16 return std::string(BigUnsignedInABase(x, 10));
19 std::string easyBItoString(const BigInteger &x) {
20 return (x.getSign() == BigInteger::negative) ?
21 (std::string("-") + easyBUtoString(x)) : (easyBUtoString(x));
24 BigUnsigned easyStringToBU(const std::string &s) {
25 return BigUnsigned(BigUnsignedInABase(s, 10));
28 BigInteger easyStringToBI(const std::string &s) {
29 return (s[0] == '-') ?
30 BigInteger(easyStringToBU(s.substr(1, s.length() - 1)), BigInteger::negative) :
31 BigInteger(easyStringToBU(s));
34 // Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
35 std::ostream &operator <<(std::ostream &os, const BigUnsigned &x) {
36 BigUnsignedInABase::Base base;
37 long osFlags = os.flags();
40 else if (osFlags & os.hex) {
42 if (osFlags & os.showbase)
44 } else if (osFlags & os.oct) {
46 if (osFlags & os.showbase)
49 throw "std::ostream << BigUnsigned: Could not determine the desired base from output-stream flags";
50 std::string s = std::string(BigUnsignedInABase(x, base));
54 // Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
55 // My somewhat arbitrary policy: a negative sign comes before a base indicator (like -0xFF).
56 std::ostream &operator <<(std::ostream &os, const BigInteger &x) {
57 if (x.getSign() == BigInteger::negative)
59 os << (const BigUnsigned &)(x);