X-Git-Url: https://mattmccutchen.net/bigint/bigint.git/blobdiff_plain/b9e459aab5aff20564efc14602d214fd4f6a233a..cb2f0c288d4b7acfa37d7a9c8bc1024c3f332b5f:/BigIntegerUtils.cc diff --git a/BigIntegerUtils.cc b/BigIntegerUtils.cc index 87eb50b..44073af 100644 --- a/BigIntegerUtils.cc +++ b/BigIntegerUtils.cc @@ -1,32 +1,27 @@ #include "BigIntegerUtils.hh" #include "BigUnsignedInABase.hh" -/* - * This file includes: - * (1) `std::string <=> BigUnsigned/BigInteger' conversion routines easier than `BigUnsignedInABase' - * (2) << and >> operators for BigUnsigned/BigInteger, std::istream/std::ostream - */ - -std::string easyBUtoString(const BigUnsigned &x) { +std::string bigUnsignedToString(const BigUnsigned &x) { return std::string(BigUnsignedInABase(x, 10)); } -std::string easyBItoString(const BigInteger &x) { - return (x.getSign() == BigInteger::negative) ? - (std::string("-") + easyBUtoString(x)) : (easyBUtoString(x)); +std::string bigIntegerToString(const BigInteger &x) { + return (x.getSign() == BigInteger::negative) + ? (std::string("-") + bigUnsignedToString(x.getMagnitude())) + : (bigUnsignedToString(x.getMagnitude())); } -BigUnsigned easyStringToBU(const std::string &s) { +BigUnsigned stringToBigUnsigned(const std::string &s) { return BigUnsigned(BigUnsignedInABase(s, 10)); } -BigInteger easyStringToBI(const std::string &s) { - return (s[0] == '-') ? - BigInteger(easyStringToBU(s.substr(1, s.length() - 1)), BigInteger::negative) : - BigInteger(easyStringToBU(s)); +BigInteger stringToBigInteger(const std::string &s) { + // Recognize a sign followed by a BigUnsigned. + return (s[0] == '-') ? BigInteger(stringToBigUnsigned(s.substr(1, s.length() - 1)), BigInteger::negative) + : (s[0] == '+') ? BigInteger(stringToBigUnsigned(s.substr(1, s.length() - 1))) + : BigInteger(stringToBigUnsigned(s)); } -// Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'. std::ostream &operator <<(std::ostream &os, const BigUnsigned &x) { BigUnsignedInABase::Base base; long osFlags = os.flags(); @@ -46,11 +41,10 @@ std::ostream &operator <<(std::ostream &os, const BigUnsigned &x) { os << s; return os; } -// Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'. -// My somewhat arbitrary policy: a negative sign comes before a base indicator (like -0xFF). + std::ostream &operator <<(std::ostream &os, const BigInteger &x) { if (x.getSign() == BigInteger::negative) os << '-'; - os << (const BigUnsigned &)(x); + os << x.getMagnitude(); return os; }